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.

Forked package with python 3 support. Original package is located here: https://github.com/vivekn/redis-simple-cache

Requirements:

redis 4.0.0

Installation:

pip install redis-simple-cache-3k

or to get the latest version

git clone git://github.com/ohanetz/redis-simple-cache-3k.git
cd redis-simple-cache-3k
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

CONTRIBUTORS:

Flávio Juvenal

Sam Zaydel

David Ng

DJ Gilcrease

Johannes Maximilian Toball

Robert Marshall

Ben Hayden

Python 3 and Redis 4 support added by Omer Hanetz

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-3k-0.0.7.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redis_simple_cache_3k-0.0.7-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file redis-simple-cache-3k-0.0.7.tar.gz.

File metadata

  • Download URL: redis-simple-cache-3k-0.0.7.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for redis-simple-cache-3k-0.0.7.tar.gz
Algorithm Hash digest
SHA256 3e26c3a99881d598c3ba8d2e91e9caebbe75ceb0d507048958082587d6ffd219
MD5 581ef7b7fcb1ff5d7473fa813f68f21b
BLAKE2b-256 02859aa0e50a8f4a72d073c2d7f33d0dcdac46fee9f4fc1b7e9af78543201852

See more details on using hashes here.

File details

Details for the file redis_simple_cache_3k-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: redis_simple_cache_3k-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for redis_simple_cache_3k-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 36cfee984fac52ad9b46f3aee718e8aa43e3ebe84e29820abcb0afba626e6acc
MD5 013c93a9cfa05a73a9d57f089e148a84
BLAKE2b-256 7f069f7ffca0318aa6dc920bbcb6d6f9a2ae923375f1eaf4eeb1e29f5a26b724

See more details on using hashes here.

Supported by

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