Esempio n. 1
0
def redis_py(
        client, key_prefix=None, expire=None, coder=None, ignorable_keys=None):
    miss_value = None

    def get_value(client, key):
        value = client.get(key)
        return value

    def set_value(client, key, value):
        client.set(key, value, expire)

    def del_value(client, key):
        client.delete(key)

    def touch_value(client, key):
        if expire is None:
            raise TypeError("'touch' is requested for persistant cache")
        client.expire(key, expire)

    return futil.factory(
        client, key_prefix=key_prefix, wrapper_class=wrapper_class,
        get_value=get_value, set_value=set_value, del_value=del_value,
        touch_value=touch_value,
        miss_value=miss_value, coder=coder,
        ignorable_keys=ignorable_keys)
Esempio n. 2
0
def aiomcache(client,
              key_prefix,
              time=0,
              coder=None,
              ignorable_keys=None,
              key_encoding='utf-8'):

    miss_value = None

    def get_value(client, key):
        return client.get(key)

    def set_value(client, key, value):
        return client.set(key, value, time)

    def del_value(client, key):
        return client.delete(key)

    def touch_value(client, key):
        return client.touch(key, time)

    return futil.factory(client,
                         key_prefix=key_prefix,
                         wrapper_class=wrapper_class,
                         get_value=get_value,
                         set_value=set_value,
                         del_value=del_value,
                         touch_value=touch_value,
                         miss_value=miss_value,
                         coder=coder,
                         ignorable_keys=ignorable_keys,
                         key_encoding=key_encoding)
Esempio n. 3
0
def dict(
        obj, key_prefix='', expire=None, coder=None, ignorable_keys=None,
        now=time.time):

    miss_value = None

    def get_value(obj, key):
        if now is None:
            _now = time.time()
        else:
            _now = now
        try:
            expired_time, value = obj[key]
        except KeyError:
            return miss_value
        if expired_time is not None and expired_time < _now:
            return miss_value
        return value

    def set_value(obj, key, value):
        if now is None:
            _now = time.time()
        else:
            _now = now
        if expire is None:
            expired_time = None
        else:
            expired_time = _now + expire
        obj[key] = expired_time, value

    def del_value(obj, key):
        try:
            del obj[key]
        except KeyError:
            pass

    def touch_value(obj, key):
        if now is None:
            _now = time.time()
        else:
            _now = now
        try:
            expired_time, value = obj[key]
        except KeyError:
            return
        if expire is None:
            expired_time = None
        else:
            expired_time = _now + expire
        obj[key] = expired_time, value

    return futil.factory(
        obj, key_prefix=key_prefix, wrapper_class=wrapper_class,
        get_value=get_value, set_value=set_value, del_value=del_value,
        touch_value=touch_value,
        miss_value=miss_value, coder=coder,
        ignorable_keys=ignorable_keys)
Esempio n. 4
0
def aioredis(pool, key_prefix, expire, coder=None, ignorable_keys=None):
    miss_value = None

    @asyncio.coroutine
    def get_value(pool, key):
        client = yield from pool.acquire()
        try:
            value = yield from client.get(key)
        finally:
            pool.release(client)
        return value

    @asyncio.coroutine
    def set_value(pool, key, value):
        client = yield from pool.acquire()
        try:
            yield from client.set(key, value, expire=expire)
        finally:
            pool.release(client)

    @asyncio.coroutine
    def del_value(pool, key):
        client = yield from pool.acquire()
        try:
            yield from client.delete(key)
        finally:
            pool.release(client)

    @asyncio.coroutine
    def touch_value(pool, key):
        client = yield from pool.acquire()
        try:
            client.expire(key, expire)
        finally:
            pool.release(client)

    return futil.factory(pool,
                         key_prefix=key_prefix,
                         wrapper_class=wrapper_class,
                         get_value=get_value,
                         set_value=set_value,
                         del_value=del_value,
                         touch_value=touch_value,
                         miss_value=miss_value,
                         coder=coder,
                         ignorable_keys=ignorable_keys)
Esempio n. 5
0
def memcache(client, key_prefix=None, time=0, coder=None, ignorable_keys=None):
    import re
    import hashlib
    miss_value = None

    def get_value(client, key):
        value = client.get(key)
        return value

    def set_value(client, key, value):
        client.set(key, value, time)

    def del_value(client, key):
        client.delete(key)

    def touch_value(client, key):
        client.touch(key, time)

    rule = re.compile(r'[!-~]+')

    def key_refactor(key):
        if len(key) < 250 and rule.match(key).group(0) == key:
            return key
        try:
            hashed = hashlib.sha1(key).hexdigest()
        except TypeError:
            # FIXME: ensure key is bytes before key_refactor
            key = key.encode('utf-8')
            hashed = hashlib.sha1(key).hexdigest()
        return 'ring-sha1:' + hashed

    return futil.factory(
        client, key_prefix=key_prefix, wrapper_class=wrapper_class,
        get_value=get_value, set_value=set_value, del_value=del_value,
        touch_value=touch_value,
        miss_value=miss_value, coder=coder,
        ignorable_keys=ignorable_keys,
        key_refactor=key_refactor)
Esempio n. 6
0
def kazoo_py(client, key_prefix=None, expire=0, coder=None, ignorable_keys=None, now=time.time()):
    import kazoo as _kazoo

    miss_value = None

    def get_value(client, key):
        # FIXME: get_async()
        try:
            ret = client.get(key)
        except _kazoo.exceptions.NoNodeError:
            return miss_value

        if now is None:
            _now = time.time()
        else:
            _now = now

        if ret is not None and ret[0] is not None:
            x = ret[0].find('\x01')
            expired_time, value = float(ret[0][:x]), ret[0][x + 1:]
            if expired_time < _now:
                return miss_value
            else:
                return value
        else:
            return miss_value

    def set_value(client, key, value):
        if now is None:
            _now = time.time()
        else:
            _now = now
        if expire == 0:
            expired_time = 0
        else:
            expired_time = _now + expire
        if client.exists(key) is None:
            client.create(key, str(expired_time) + '\x01' + value)
        else:
            client.set(key, str(expired_time) + '\x01' + value)

    def del_value(client, key):
        client.delete(key)

    def touch_value(client, key):
        if now is None:
            _now = time.time()
        else:
            _now = now
        ret = client.get(key)
        if ret is not None and ret[0] is not None:
            x = ret[0].find('\x01')
            expired_time, value = float(ret[0][:x]), ret[0][x + 1:]
            if expire == 0:
                expired_time = 0
            else:
                expired_time = _now + expire
            value = client.set(key, (expired_time, value))

    return futil.factory(
        client, key_prefix=key_prefix, wrapper_class=wrapper_class,
        get_value=get_value, set_value=set_value, del_value=del_value,
        touch_value=touch_value,
        miss_value=miss_value, coder=coder,
        ignorable_keys=ignorable_keys)