Skip to main content

Lightweight and extensible caching framework for Python applications. It uses Redis as its storage backend.

Project description

RedCache is a lightweight and extensible caching framework for Python applications. It uses Redis as its storage backend.

Quickstart

Suppose we have a function that loads data from a remote source:

def load_something_from_somewhere():
    # Load the data...
    return data

Now let’s use it in a Flask view:

@app.route('/something/')
def get_something():
    data = load_something_from_somewhere()
    # Do something with the data...
    return jsonify(data=data)

The problem here is that the view will wait until the data is fetched. If the view is called often it’ll generate significant traffic between our server and the remote source. This isn’t pretty.

What if we cached the loaded data for some time and refresh it only when the cache expires? That would be pretty.

There are many caching solutions, e.g. Beaker. RedCache is similar to (and inspired by) Beaker but provides only one storage backend - Redis.

So, how do you cache load_something_from_somewhere using Redis and RedCache?

First setup Redis connection before starting the app:

from redcache import use_connection
use_connection()

Then make the function cached:

from redcache import default_cache

@default_cache.cache(ttl=5)
def load_something_from_somewhere():
    # Load the data...
    return data

Next time load_something_from_somewhere is called RedCache will try to load its last return value from Redis. If it’s not found then the function will be executed and its return value stored in Redis for 5 seconds.

redcache.default_cache is an instance of redcache.cache_manager.DefaultCacheManager which gives convenient access to caching mechanism which uses cPickle behind the scenes. By default the keys will be stored infinitely. Use ttl keyword argument in the decorator to define different TTL.

Extending and advanced use

RedCache can be easily extended to utilize Redis’ datatypes and features.

By overriding redcache.cache_manager.CacheManager.after_load and redcache.cache_manager.CacheManager.before_save you can perform additional operations on data. This way you can e.g. store JSON strings instead of pickled objects.

By overriding redcache.cache_manager.CacheManager.load and redcache.cache_manager.CacheManager.save you can change the way data is loaded and saved. This way you can e.g. store lists of individual objects and retrieve them according to pagination options.

Consult examples to see how to integrate RedCache with SQLAlchemy and to see how to use JSON instead of cPickle.

Author, credits and license

RedCache is developed by BTHLabs.

RedCache is licensed under BSD License. See LICENSE for more details.

This project uses Redis connection management code from RQ.

This project uses context-local objects code from Werkzeug.

Source code

Source code is available from project repository on GitHub.

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

RedCache-0.3.tar.gz (19.5 kB view details)

Uploaded Source

File details

Details for the file RedCache-0.3.tar.gz.

File metadata

  • Download URL: RedCache-0.3.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for RedCache-0.3.tar.gz
Algorithm Hash digest
SHA256 2e6fe5e744c3b060b31b05209dc8bdacc95b774028ce220285246825e43f0828
MD5 56b3109ead2e733b79a4e536d09cea7d
BLAKE2b-256 7259d4863bf190b55872e65e654d1ecceed49819895b738bf4cd28eae74e9cb8

See more details on using hashes here.

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