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

# 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/vivekn/redis-simple-cache.git
cd redis-simple-cache
python setup.py install

Usage:
------

from redis_cache import cache_it_json

@cache_it_json(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 in a event of full memory. 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 test_rediscache.py file.

Advanced:
---------
Advanced users can customize the decorators even more by passing a SimpleCache object. For example:

my_cache = SimpleCache(limit=100, expire=60 * 60, hashkeys=True, host='localhost', port=6379, db=1, namespace='Fibonacci')
@cache_it(cache=my_cache)
def fib(n):
# ...

`hashkeys` parameter makes the SimpleCache to store keys in md5 hash. It is `True` by default in decorators, but `False` by default in a new SimpleCache object.
`host`, `port` and `db` are the same redis config params used in StrictRedis class of redis-py.
By default, the `namespace` is the name of the module from which the decorated function is called, but it can be overridden with the `namespace` parameter.

AUTHOR: Vivek Narayanan
FORKED AND IMPROVED BY: Flávio Juvenal and Sam Zaydel
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.5.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

redis_simple_cache-0.0.5-py2.7.egg (12.6 kB view details)

Uploaded Source

File details

Details for the file redis-simple-cache-0.0.5.tar.gz.

File metadata

File hashes

Hashes for redis-simple-cache-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a98cb374656bc53457323367c18bcb33ae84db2e6657c164f5f8c1b2febc2088
MD5 d8c8bacc7452408dcd0f3d1767427b9d
BLAKE2b-256 fe6fc854b4472fd8b75e2c8ca0f3f871c3f163497ea44cee309d7b4c718e68d9

See more details on using hashes here.

Provenance

File details

Details for the file redis_simple_cache-0.0.5-py2.7.egg.

File metadata

File hashes

Hashes for redis_simple_cache-0.0.5-py2.7.egg
Algorithm Hash digest
SHA256 941eb88d82435c0161a78950a419040c030ab732a5e9511a3f707585dea88e77
MD5 6b8dc5a08e02dc780cb52e1fbc3b86da
BLAKE2b-256 328043ad881fcdf0c59b380c0e38e2cce875f2735eb85865b38f35e4450bc44f

See more details on using hashes here.

Provenance

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