Example #1
0
 def desc_float(self, key, amount=1.0):
     """
     减去指定值
     :param key:
     :param amount:
     :return:
     """
     if not isinstance(amount, float):
         raise TypeException(u'类型错误')
     self.database.hincrbyfloat(self.cache_key, key, -amount)
Example #2
0
    def popitem(self):
        """
        弹出任意的键值对
        :return:
        """
        if not self:
            raise TypeException(u'empty hash')

        rand_key, rand_val = next(self.iteritems())
        return rand_key, self.pop(rand_key)
Example #3
0
 def desc(self, member, amount=1):
     """
     减去指定值
     :param member:
     :param amount:
     :return:
     """
     if not isinstance(amount, int):
         raise TypeException(u'类型错误')
     self.database.zincrby(self.cache_key, -amount, member)
Example #4
0
 def incr(self, key, amount=1):
     """
     增加指定值
     :param key:
     :param amount:
     :return:
     """
     if not isinstance(amount, int):
         raise TypeException(u'类型错误')
     self.database.hincrby(self.cache_key, key, amount)
Example #5
0
 def update(self, other):
     """
     并集
     :param other:
     :return:
     """
     if isinstance(other, Set):
         self.union_store(self.cache_key, other)
     elif isinstance(other, Iterable):
         self.database.sadd(self.cache_key, *other)
     else:
         raise TypeException(u'类型错误')
Example #6
0
 def __setitem__(self, item, value):
     """
     设置值
     :param item:
     :param value:
     :return:
     """
     if not isinstance(item, int):
         raise TypeException(u'类型错误')
     ll = len(self)
     if not (0 <= item < ll):
         raise IndexErrorException(u'越界错误')
     return self.database.lset(self.cache_key, item, value)
Example #7
0
 def update(self, other):
     """
     更新键值对
     :param other:
     :return:
     """
     if not other:
         return
     if isinstance(other, Hash):
         self.database.hmset(self.cache_key, other.data())
     elif isinstance(other, dict):
         self.database.hmset(self.cache_key, other)
     else:
         raise TypeException(u'类型错误')
Example #8
0
    def intersection(self, *args):
        """
        取交集
        :param args: set对象
        :return:
        """
        keys = [self.cache_key]
        for obj in args:
            if isinstance(obj, Set):
                keys.append(obj.cache_key)
            else:
                raise TypeException(u'类型错误')

        return self.database.sinter(*keys)
Example #9
0
    def difference(self, *args):
        """
        取差集,取集合中第一个key存在而其他key不存在的
        :param args:
        :return:
        """
        keys = [self.cache_key]
        for obj in args:
            if isinstance(obj, Set):
                keys.append(obj.cache_key)
            else:
                raise TypeException(u'类型错误')

        return self.database.sdiff(*keys)
Example #10
0
 def append(self, mapping=None, **kwargs):
     """
     添加成员分值,成员到有序集合中
     :param mapping:
     :param kwargs:
     :return:
     """
     if not mapping and not kwargs:
         raise TypeException(u'类型错误')
     if not kwargs:
         _mapping = mapping
     else:
         _mapping = mapping.copy() if mapping else {}
         _mapping.update(kwargs)
     self.database.zadd(self.cache_key, _mapping)
Example #11
0
    def __iadd__(self, other):
        """
        累加
        :param other:
        :return:
        """
        if isinstance(other, List):
            values = other.data()
        elif isinstance(other, list):
            values = other
        else:
            raise TypeException(u'类型错误')

        self.extend(values)
        return self
Example #12
0
 def __getitem__(self, item):
     """
     cur[index]
     """
     if isinstance(item, slice):
         start = item.start or 0
         stop = item.stop or len(self)
         return self.database.lrange(self.cache_key, start, stop)
     elif isinstance(item, int):
         result = self.database.lrange(self.cache_key, item, item)
         if not result:
             raise IndexErrorException(u'越界错误')
         return result[0]
     else:
         raise TypeException(u'类型错误')
Example #13
0
 def __iadd__(self, other):
     """
     累加
     :param other:
     :return:
     """
     if isinstance(other, SortedSet):
         self.append(dict(other.data()))
     elif isinstance(other, Iterable):
         self.append(dict(other))
     elif isinstance(other, dict):
         self.append(other)
     else:
         raise TypeException(u'类型错误')
     return self
Example #14
0
    def union_store(self, dest_key, *args):
        """
        将并集的结果存储到新的key中
        :param dest_key:
        :param args:
        :return:
        """
        keys = [self.cache_key]
        for obj in args:
            if isinstance(obj, Set):
                keys.append(obj.cache_key)
            else:
                raise TypeException(u'类型错误')

        return self.database.zunionstore(dest_key, keys)
Example #15
0
    def pop(self, key, default=None):
        """
        弹出指定的key
        :param key:
        :param default:
        :return:
        """
        if key not in self:
            if default:
                return default
            raise TypeException(u'not found the key')

        value = self.get(key)
        self._del_key(key)
        return value
Example #16
0
 def __getitem__(self, item):
     """
     如果是切分则切分,否则返回具体排名的分值
     :param item:
     :return:
     """
     if isinstance(item, slice):
         start = item.start or 0
         stop = item.stop or len(self)
         step = item.step or 1
         if step > 0:
             return self.range(start, stop)
         else:
             return self.range(start, stop, True)
     elif isinstance(item, int):
         return self.database.zscore(self.cache_key, item)
     else:
         raise TypeException(u'类型错误')
Example #17
0
 def __delitem__(self, item):
     if not isinstance(item, int):
         raise TypeException(u'类型错误')
     self.pop(item)