예제 #1
0
 def GET(self):
     key = self.arguments[0]
     try:
         value, expire = getattr(self.datastore, key)
         if expire is not None and expire <= int(time.time()):
             self.DEL(key)
             raise Exception
         else:
             return (0, serialize_bulk_string('{}'.format(value)))
     except:
         return (0, serialize_bulk_string(None))
예제 #2
0
 def SPOP(self):
     key = self.arguments[0]
     array = getattr(self.datastore, key, None)
     if array is None:
         return (0, serialize_bulk_string(None))
     try:
         result = array.pop()
     except KeyError:
         return (0, serialize_bulk_string(None))
     setattr(self.datastore, key, array)
     return (0, serialize_bulk_string(result))
예제 #3
0
 def RPOP(self):
     key = self.arguments[0]
     array = getattr(self.datastore, key, list())
     try:
         result = array.pop()
     except IndexError:
         result = None
     return (0, serialize_bulk_string(result))
예제 #4
0
 def ZRANK(self):
     key = self.arguments[0]
     member = self.arguments[1]
     array = getattr(self.datastore, key, list())
     if not [
             position
             for position, value in enumerate(array) if value[1] == member
     ]:
         return (0, serialize_bulk_string(None))
     else:
         return (0,
                 serialize_integer('{}'.format([
                     position for position, value in enumerate(array)
                     if value[1] == member
                 ][0])))
예제 #5
0
 def ECHO(self):
     return (0,
             serialize_bulk_string('{}'.format(' '.join(self.arguments))))