예제 #1
0
    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])
예제 #2
0
 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))
예제 #3
0
    def _get_unused_id(callback=None):
        """ gets an unused id """
	cli = data.get_client()
        callback((yield cli.async.incr("counter:snippet")))
예제 #4
0
 def count_view(self):
     """ Counts a pageview for this snippet """
     cli = data.get_client()
     yield cli.async.hincrby(self._key,"views",1)
예제 #5
0
 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)))