Skip to main content

redis-simple-cache is a pythonic interface for creating a cache over redis. It provides simple decorators that can be added to any function to cache its return values.

Project description

Warning: This is not a major bug, since the decorators doesn't use the flush method of SimpleCache,
but in this 0.0.2 release the flush method doesn't work properly.
The method flushes the redis keys set used by SimpleCache, but doesn't flush the real keys on redis-server.
If you want to use the flush method, please use the 0.0.3 or above version: http://pypi.python.org/pypi/redis-simple-cache/0.0.3

# redis-simple-cache
redis-simple-cache is a pythonic interface for creating a cache over redis.
It provides simple decorators that can be added to any function to cache its return values.

Requirements:
-------------
redis 2.6.2
redis-py 2.7.1 (see requirements.txt file)

Installation:
-------------

pip install redis-simple-cache

or

git clone git://github.com/fjsj/redis-simple-cache.git
cd redis-simple-cache
python setup.py install

Usage:
------

from redis_cache import cache_it

@cache_it(limit=1000, expire=60 * 60 * 24)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)

`limit` is the maximum number of keys, `expire` is the expire time in seconds.
It is always recommended to specify a expire time, since by default redis-server will only remove keys with an expire time set. But if you wish your keys to never expire, set `expire` to `None`.
**Note that function arguments and result must be pickleable, since cache_it uses the pickle module.**

It is also possible to use redis-simple-cache as a object-oriented cache:

>> from redis_cache import SimpleCache
>> c = SimpleCache(10) # cache that has a maximum limit of 10 keys
>> c.store("foo", "bar")
>> c.get("foo")
'bar'
>> "foo" in c # efficient membership test, time-complexity O(1)
True
>> len(c) # efficient cardinality calculation, time-complexity O(1)
1
>> c.keys() # returns all keys, time-complexity O(N) with N being the cache c cardinality
set(['foo'])
>> c.flush() # flushes the cache, time-complexity O(N) with N being the cache c cardinality
>> "foo" in c
False
>> len(c)
0

Check out more examples in the tests.py file.

AUTHOR: Vivek Narayanan
FORKED AND IMPROVED BY: Flávio Juvenal
LICENSE: BSD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redis-simple-cache-0.0.2.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

redis_simple_cache-0.0.2-py2.7.egg (6.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page