def _set(self, key, value, ttl, not_exists=False): with lockutils.lock(key): if not_exists and self._exists_unlocked(key): return False self._set_unlocked(key, value, ttl) return True
def _set(self, key, value, ttl=0, not_exists=False): with lockutils.lock(key): # NOTE(flaper87): This is needed just in `set` # calls, hence it's not in `_set_unlocked` if not_exists and self._exists_unlocked(key): return False self._set_unlocked(key, value, ttl) return True
def _incr_append(self, key, other): with lockutils.lock(key): timeout, value = self._get_unlocked(key) if value is None: return None ttl = timeutils.utcnow_ts() - timeout new_value = value + other self._set_unlocked(key, new_value, ttl) return new_value
def set(self, key, value, ttl=0): key = self._prepare_key(key) with lockutils.lock(key): expires_at = 0 if ttl != 0: expires_at = timeutils.utcnow_ts() + ttl self._cache[key] = (expires_at, value) if expires_at: self._keys_expires.setdefault(expires_at, set()).add(key) return True
def get(self, key, default=None): key = self._prepare_key(key) with lockutils.lock(key): now = timeutils.utcnow_ts() try: timeout, value = self._cache[key] if timeout and now >= timeout: del self._cache[key] return default return value except KeyError: return default
def _incr_append(self, key, other, transform=int): client = self._client with lockutils.lock(key): redis_key = gen_key(key) timeout, value = self._get_unlocked(key) if value is None: return None if isinstance(value, list): for element in other[0]: client.rpush(redis_key, element) return client.lrange(redis_key, 0, -1) else: client.incrby(redis_key, other) return client.get(redis_key)
def __contains__(self, key): with lockutils.lock(key): return self._exists_unlocked(key)
def _get(self, key, default=None): with lockutils.lock(key): return self._get_unlocked(key, default)[1]
def __delitem__(self, key): with lockutils.lock(key): redis_key = gen_key(key) self._client.delete(redis_key)