예제 #1
0
    async def smembers(self, key, *, encoding=_NOTSET):
        """Get all the members in a set.

        Warning: order is not consistent with aioredis
        """
        if encoding == _NOTSET:
            encoding = self._encoding

        items = self._redis.smembers(key)
        return list(_decode_items(items, encoding))
예제 #2
0
    async def spop(self, key, count=None, *, encoding=_NOTSET):
        """Remove and return one or multiple random members from a set."""
        if encoding == _NOTSET:
            encoding = self._encoding

        if count is None:
            value = self._redis.spop(key)
            return _decode_item(value, encoding)

        if count == 0:
            return []
        if count < 0:
            raise ReplyError("ERR index out of range")

        total = len(self._redis.smembers(key))
        count = min(count, total)
        values = (self._redis.spop(key) for _ in range(count))
        return list(_decode_items(values, encoding))
예제 #3
0
    async def srandmember(self, key, count=None, *, encoding=_NOTSET):
        """Get one or multiple random members from a set."""
        if encoding == _NOTSET:
            encoding = self._encoding

        if count is None:
            value = self._redis.srandmember(key)
            return _decode_item(value, encoding)

        if count == 0:
            return []

        if count < 0:
            # Allow duplicates
            count = -count
            output = [self._redis.srandmember(key) for _ in range(count)]
        else:
            # Don't allow duplicates
            original = self._redis.smembers(key)
            count = min(count, len(original))
            output = [self._redis.spop(key) for _ in range(count)]
            self._redis.sadd(key, *original)

        return list(_decode_items(output, encoding))