Пример #1
0
class RedisCache(BaseCache):
    def __init__(self, connection=None, pool=None):
        if isinstance(pool, BaseConnectionPool):
            self.pool = pool
        else:
            # fake pool
            self.pool = OnceConnectionPool(
                size=1, connection=connection)

    def connection_installer(self, method, *args, **kwargs):
        index, self.conn = self.pool.use_connection()
        try:
            result = method(*args, **kwargs)
        except Exception as err:
            raise err
        finally:
            self.conn = None
            self.pool.free(index)

        return result

    def _iface_get(self, cache_key):
        data = self.conn.get(cache_key)
        if data:
            data = pickle.loads(data)
        return data

    def _iface_pipeline_get(self, cache_key_list):
        if cache_key_list:
            pipe = self.conn.pipeline()
            for key in cache_key_list:
                pipe.get(key)
            data = pipe.execute()
            if data:
                return [pickle.loads(d) for d in data if d]
        return None

    def _iface_pipeline_delete(self, cache_key_list):
        if isinstance(cache_key_list, list) and len(cache_key_list) > 0:
            pipe = self.conn.pipeline()
            for key in cache_key_list:
                pipe.delete(key)
            data = pipe.execute()
            if data:
                return data
        return None

    def _iface_delete(self, cache_key):
        return self.conn.delete(cache_key)

    def _iface_set(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        if not self.conn:
            return
        assert isinstance(timeout, int)
        pickled_data = pickle.dumps(data)
        if timeout > 0:
            self.conn.setex(cache_key, pickled_data, timeout)
        else:
            self.conn.set(cache_key, pickled_data)

    def _iface_set_int(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        assert isinstance(timeout, int)
        self.conn.setex(cache_key, data, timeout)

    def _iface_get_int(self, cache_key):
        try:
            return int(self.conn.get(cache_key))
        except (AttributeError, TypeError, ValueError):
            return

    def _iface_incr(self, name, amount=1):
        self.conn.incr(name, amount)

    def _iface_flushall(self):
        if self.conn:
            self.conn.flushdb()
            return True
        return False

    def _iface_append_to_list(self, list_cache_key, data):
        self.conn.rpush(list_cache_key, data)

    def _iface_get_all_list(self, list_cache_key):
        return  self.conn.lrange(list_cache_key, 0, -1)

    def get(self, cache_key):
        return self.connection_installer(
            self._iface_get, cache_key)

    def pipeline_get(self, cache_key_list):
        return self.connection_installer(
            self._iface_pipeline_get, cache_key_list)

    def pipeline_delete(self, cache_key_list):
        return self.connection_installer(
            self._iface_pipeline_delete, cache_key_list)

    def delete(self, cache_key):
        return self.connection_installer(self._iface_delete, cache_key)

    def set(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        return self.connection_installer(
            self._iface_set, cache_key, data, timeout)

    def set_int(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        return self.connection_installer(
            self._iface_set_int, cache_key, data, timeout)

    def get_int(self, cache_key):
        return self.connection_installer(
            self._iface_get_int, cache_key)

    def incr(self, name, amount=1):
        return self.connection_installer(
            self._iface_incr, name, amount)

    def flushall(self):
        return self.connection_installer(
            self._iface_flushall)

    def append_to_list(self, list_cache_key, data):
        return self.connection_installer(
            self._iface_append_to_list, list_cache_key, data)

    def get_all_list(self, list_cache_key):
        return self.connection_installer(
            self._iface_get_all_list, list_cache_key)
Пример #2
0
class RedisCache(BaseCache):
    def __init__(self, connection=None, pool=None):
        if isinstance(pool, BaseConnectionPool):
            self.pool = pool
        else:
            # fake pool
            self.pool = OnceConnectionPool(size=1, connection=connection)

    def connection_installer(self, method, *args, **kwargs):
        index, self.conn = self.pool.use_connection()
        try:
            result = method(*args, **kwargs)
        except Exception as err:
            raise err
        finally:
            self.conn = None
            self.pool.free(index)

        return result

    def _iface_get(self, cache_key):
        data = self.conn.get(cache_key)
        if data:
            data = pickle.loads(data)
        return data

    def _iface_pipeline_get(self, cache_key_list):
        if cache_key_list:
            pipe = self.conn.pipeline()
            for key in cache_key_list:
                pipe.get(key)
            data = pipe.execute()
            if data:
                return [pickle.loads(d) for d in data if d]
        return None

    def _iface_pipeline_delete(self, cache_key_list):
        if isinstance(cache_key_list, list) and len(cache_key_list) > 0:
            pipe = self.conn.pipeline()
            for key in cache_key_list:
                pipe.delete(key)
            data = pipe.execute()
            if data:
                return data
        return None

    def _iface_delete(self, cache_key):
        return self.conn.delete(cache_key)

    def _iface_set(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        if not self.conn:
            return
        assert isinstance(timeout, int)
        pickled_data = pickle.dumps(data)
        if timeout > 0:
            self.conn.setex(cache_key, pickled_data, timeout)
        else:
            self.conn.set(cache_key, pickled_data)

    def _iface_set_int(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        assert isinstance(timeout, int)
        self.conn.setex(cache_key, data, timeout)

    def _iface_get_int(self, cache_key):
        try:
            return int(self.conn.get(cache_key))
        except (AttributeError, TypeError, ValueError):
            return

    def _iface_incr(self, name, amount=1):
        self.conn.incr(name, amount)

    def _iface_flushall(self):
        if self.conn:
            self.conn.flushdb()
            return True
        return False

    def _iface_append_to_list(self, list_cache_key, data):
        self.conn.rpush(list_cache_key, data)

    def _iface_get_all_list(self, list_cache_key):
        return self.conn.lrange(list_cache_key, 0, -1)

    def get(self, cache_key):
        return self.connection_installer(self._iface_get, cache_key)

    def pipeline_get(self, cache_key_list):
        return self.connection_installer(self._iface_pipeline_get,
                                         cache_key_list)

    def pipeline_delete(self, cache_key_list):
        return self.connection_installer(self._iface_pipeline_delete,
                                         cache_key_list)

    def delete(self, cache_key):
        return self.connection_installer(self._iface_delete, cache_key)

    def set(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        return self.connection_installer(self._iface_set, cache_key, data,
                                         timeout)

    def set_int(self, cache_key, data, timeout=DEFAULT_TIMEOUT):
        return self.connection_installer(self._iface_set_int, cache_key, data,
                                         timeout)

    def get_int(self, cache_key):
        return self.connection_installer(self._iface_get_int, cache_key)

    def incr(self, name, amount=1):
        return self.connection_installer(self._iface_incr, name, amount)

    def flushall(self):
        return self.connection_installer(self._iface_flushall)

    def append_to_list(self, list_cache_key, data):
        return self.connection_installer(self._iface_append_to_list,
                                         list_cache_key, data)

    def get_all_list(self, list_cache_key):
        return self.connection_installer(self._iface_get_all_list,
                                         list_cache_key)