Skip to main content

Redis caching of functions evolving over time

Project description

RedisCache

Presentation

There are already quite a few Python decorators to cache functions in a Redis database:

But none I could find allows to set two expiration times as we do it here. The first given time is how long before we should update the value stored in the cache. The second given time, longer of course, is how long the data stored in the cache is still good enough to be sent back to the caller. The refreshing of the cache is only done when the function is called. And by default it is done asynchronously, so the caller doesn't have to wait. When the data in the cache becomes too old, it disappear automatically.

This is a great caching mechanism for functions that will give a consistent output according to their parameters and at a given time. A purely random function should not be cached. And a function that is independent of the time should be cached with a different mechanism like the LRU cache in the functools standard module.

Installation

Simply install the PyPi package rediscache:

pip install rediscache

Requirements

Of course you need a Redis server installed. By default, the decorator will connect to localhost:6379 with no password, using the database number 0. This can be changed with parameters given to the RedisCache object.

Usage of the RedisCache class

RedisCache class

To avoid having too many connections to the Redis server, it is best to create only one instance of this class.

rediscache = RedisCache()

All the parameters for the RedisCache constructor are optional. Their default value are in [].

  • host: IP or host name of the Redis server. ['localhost']
  • port: Port number of the Redis server. [6379]
  • db: Database number in the Redis server. [0]
  • password: Password required to read and write on the Redis server. [None]
  • decode: Decode the data stored in the cache as byte string. For example, it should not be done if you actually want to cache byte strings. [True]
  • enabled: When False it allows to programmatically disable the cache. It can be useful for unit tests. [True]

Environment variables

In the case of a cloud deployment, for example, it might be easier to use environment variables to set the Redis server details:

  • REDIS_SERVICE_HOST: IP or host name of the Redis server.
  • REDIS_SERVICE_PORT: Port number of the Redis server.
  • REDIS_SERVICE_DB: Database number in the Redis server.
  • REDIS_SERVICE_PASSWORD: Password required to read and write on the Redis server.

The order of priority is the natural parameter > environment variable > default value.

cache decorator

This is the main decorator. All the parameters are available. The mandatory ones do not have a default value:

  • refresh: The amount of seconds before it would be a good idea to refresh the cached value.
  • expire: How many seconds that the value in the cache is still considered good enough to be sent back to the caller.
  • default: If we do not have the value in the cache and we do not want to wait, what shall we send back to the caller? It has to be serializable because it will also be stored in the cache. ['']
  • retry: While a value is being refreshed, we want to avoid to refresh it in parallel. But if it is taking too long, after the number of seconds provided here, we may want to try our luck again. If not specified, we will take the refresh value.
  • wait: If the value is not in the cache, do we wait for the return of the function? [False]
  • use_args: This is the list of positional parameters (a list of integers) to be taken into account to generate the key that will be used in Redis. If None, they will all be used. [None]
  • use_kwargs: This is the list of named parameters (a list of names) to be taken into account to generate the key that will be used in Redis. If None, they will all be used. [None]

Example:

from rediscache import RedisCache
REDISCACHE = RedisCache()
@REDISCACHE.cache(10, 60) # Keep the value up to 1mn but ready to be refreshed every 10s.
def my_function(...) {
    ...
}

See test_rediscache.py for more examples.

Note: when you choose to wait for the value, you do not have an absolute guarantee that you will not get the default value. For example if it takes more than the retry time to get an answer from the function, the decorator will give up.

get_stats(delete=False)

This will get the stats stored when using the cache. The delete option is to reset the counters after read. The output is a dictionary with the following keys and values:

  • Refresh: Number of times the cached function was actually called.
  • Wait: Number of times we waited for the result when executing the function.
  • Sleep: Number of 1 seconds we waited for the results to be found in the cache.
  • Failed: Number of times the cached function raised an exception when called.
  • Missed: Number of times the functions result was not found in the cache.
  • Success: Number of times the function's result was found in the cache.
  • Default: Number of times the default value was used because nothing is in the cache or the function failed.

The function property

The decorator and its aliases add a new property to the decorated function to be able to bypass the cache as it may be required in some cases.

from rediscache import RedisCache
REDISCACHE = RedisCache()
@REDISCACHE.cache(2, 10)
def myfunc():
    return "Hello"
# Invoke the function without caching it.
print(myfunc.function())

The decorate decorator

In the tools submodule, the decorate decorator is a little helper to transform a serializer or deserializer function into a decorator. The transformation function is expected to take a single argument of a certain type and transform it into another type. For example, a typical serializer would transform a dictionary into a string.

from json import dumps

from rediscache.tools import decorate

@decorate(dumps)
def myfunc():
    return {"toto": 42}

assert isinstance(myfunc(), str)

You may use partial functions if your transformation function requires extra parameters.

from datetime import date
from functools import partial
from json import dumps

from rediscache.tools import decorate

@decorate(partial(dumps, skipkeys=True))
def func_with_id():
    """My func_with_id"""
    return {"name": "Toto", "age": 25, date.today(): "today"}

assert func_with_id() == '{"name": "Toto", "age": 25}'

Development

Poetry

My development environment is handled by Poetry. I use Python 3.11.7.

Testing

To make sure we use Redis properly, we do not mock it in the unit tess. So you will need a localhost default instance of Redis server without a password. This means that the unit tests are more like integration tests.

The execution of the tests including coverage result is done with pytest:

poetry run pytest --cov=rediscache

CI/CD

Workflow

We use the GitHub workflow to check each new commit. See .github/workflows/python-package.yaml.

We get help from re-usable actions. Here is the Marketplace.

Publish to PyPI

For the moment the publish to PyPI is done manually with the publish.sh script. You will need a PyPI API token in PYPI_API_TOKEN, stored in a secrets.sh.

Demo application

In the demo directory you will find a web application to test RedisCache.

poetry run webapp

Entry points:

  • Call to long function with parameter value 20 and using the cache but waiting for a result: link
  • Call to long function with parameter value 20 without using the cache: link
  • Get the stats stored in Redis database: link

There is also a Nginx configuration file to further test if with a load balancing of workers. It is useful to demonstrate that many workers can share efficiently the same instance of Redis.

Finally a Gatling configuration file can be used to test the performance.

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

rediscache-1.0.5.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

rediscache-1.0.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file rediscache-1.0.5.tar.gz.

File metadata

  • Download URL: rediscache-1.0.5.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for rediscache-1.0.5.tar.gz
Algorithm Hash digest
SHA256 b69cf0824d11360134f398b961038b5c47612dbc316eb30a813d59cd0634a815
MD5 f5fe873b4543253edba0864f84aa613d
BLAKE2b-256 13c2864d017e273a73e14628a116eb57424315b5f2b6c1d51d0b79230af49742

See more details on using hashes here.

File details

Details for the file rediscache-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: rediscache-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for rediscache-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6962e91452474880df5ab01de33b1225ae224458d66715273edfbc683317907c
MD5 19fc3342737033e4b062c7d4dff24905
BLAKE2b-256 879330cee692581df80026c742a31599c83f86c2570202c3acfa3bf22220d750

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page