async def multi_set(self, pairs, ttl=None, dumps_fn=None, namespace=None): """ Stores multiple values in the given keys. :param pairs: list of two element iterables. First is key and second is value :param ttl: int the expiration time of the keys in seconds :param dumps_fn: callable alternative to use as dumps function :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: True :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() dumps = dumps_fn or self._serializer.dumps tmp_pairs = [] for key, value in pairs: tmp_pairs.append( (self._build_key(key, namespace=namespace), dumps(value))) await self._multi_set(tmp_pairs, ttl=ttl) logger.debug("MULTI_SET %s %d (%.4f)s", [key for key, value in tmp_pairs], len(pairs), time.time() - start) return True
async def add(self, key, value, ttl=None, dumps_fn=None, namespace=None): """ Stores the value in the given key with ttl if specified. Raises an error if the key already exists. :param key: str :param value: obj :param ttl: int the expiration time in seconds :param dumps_fn: callable alternative to use as dumps function :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: True if key is inserted :raises: - ValueError if key already exists - :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() dumps = dumps_fn or self._serializer.dumps ns_key = self._build_key(key, namespace=namespace) await self._add(ns_key, dumps(value), ttl) logger.debug("ADD %s %s (%.4f)s", ns_key, True, time.time() - start) return True
async def raw(self, command, *args, **kwargs): """ Send the raw command to the underlying client. :param command: str with the command. :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: whatever the underlying client returns :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() ret = await self._raw(command, *args, **kwargs) logger.debug("%s (%.4f)s", command, time.time() - start) return ret
async def clear(self, namespace=None): """ Clears the cache in the cache namespace. If an alternative namespace is given, it will clear those ones instead. :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: True :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() ret = await self._clear(namespace) logger.debug("CLEAR %s %d (%.4f)s", namespace, ret, time.time() - start) return ret
async def expire(self, key, ttl, namespace=None): """ Set the ttl to the given key. By setting it to 0, it will disable it :param key: str key to expire :param ttl: int number of seconds for expiration. If 0, ttl is disabled :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: True if set, False if key is not found """ start = time.time() ns_key = self._build_key(key, namespace=namespace) ret = await self._expire(ns_key, ttl) logger.debug("EXPIRE %s %d (%.4f)s", ns_key, ret, time.time() - start) return ret
async def exists(self, key, namespace=None): """ Check key exists in the cache. :param key: str key to check :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: True if key exists otherwise False :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() ns_key = self._build_key(key, namespace=namespace) ret = await self._exists(ns_key) logger.debug("EXISTS %s %d (%.4f)s", ns_key, ret, time.time() - start) return ret
async def delete(self, key, namespace=None): """ Deletes the given key. :param key: Key to be deleted :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: int number of deleted keys :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() ns_key = self._build_key(key, namespace=namespace) ret = await self._delete(ns_key) logger.debug("DELETE %s %d (%.4f)s", ns_key, ret, time.time() - start) return ret
async def increment(self, key, delta=1, namespace=None): """ Increments value stored in key by delta (can be negative). If key doesn't exist, it creates the key with delta as value. :param key: str key to check :param delta: int amount to increment/decrement :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: Value of the key once incremented. -1 if key is not found. :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout :raises: :class:`TypeError` if value is not incrementable """ start = time.time() ns_key = self._build_key(key, namespace=namespace) ret = await self._increment(ns_key, delta) logger.debug("INCREMENT %s %d (%.4f)s", ns_key, ret, time.time() - start) return ret
async def get(self, key, default=None, loads_fn=None, namespace=None): """ Get a value from the cache. Returns default if not found. :param key: str :param default: obj to return when key is not found :param loads_fn: callable alternative to use as loads function :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: obj loaded :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() loads = loads_fn or self._serializer.loads ns_key = self._build_key(key, namespace=namespace) value = loads(await self._get(ns_key)) logger.debug("GET %s %s (%.4f)s", ns_key, value is not None, time.time() - start) return value or default
async def multi_get(self, keys, loads_fn=None, namespace=None): """ Get multiple values from the cache, values not found are Nones. :param keys: list of str :param loads_fn: callable alternative to use as loads function :param namespace: str alternative namespace to use :param timeout: int or float in seconds specifying maximum timeout for the operations to last. None by default :returns: list of objs :raises: :class:`asyncio.TimeoutError` if it lasts more than self.timeout """ start = time.time() loads = loads_fn or self._serializer.loads ns_keys = [self._build_key(key, namespace=namespace) for key in keys] values = [loads(value) for value in await self._multi_get(ns_keys)] logger.debug("MULTI_GET %s %d (%.4f)s", ns_keys, len([value for value in values if value is not None]), time.time() - start) return values