def save(self,callback): if self.id == -1: creation = True self.id = yield self._get_unused_id() else: creation = False key = self._key cli = data.get_client() pipe = cli.pipeline() pipe.hmset(self._key,self._field_dict()) if creation: # don't add us every save pipe.lpush("pastes",self.id) #global pastes # user posts pipe.lpush("user:{}:pastes".format(self.author),self.author) def cb(data): callback(True) pipe.execute([cb])
def by_id(cls,_id,callback=None): key = "Snippet:{}".format(_id) cli = data.get_client() fields = yield cli.async.hmget(key,cls.fields) fields["_id"] = _id callback(cls(**fields))
def _get_unused_id(callback=None): """ gets an unused id """ cli = data.get_client() callback((yield cli.async.incr("counter:snippet")))
def count_view(self): """ Counts a pageview for this snippet """ cli = data.get_client() yield cli.async.hincrby(self._key,"views",1)
def get_recent_pastes(cls,stop,start=0,callback=None): cli = data.get_client() ids = map(int,(yield cli.async.lrange("pastes",start,stop))) callback((yield cls.by_ids(ids)))