Skip to main content

Cache Money

Build Status codecov Version Python Version

Async cache library for memoization using Redis. Inspired by Walrus and implemented with aioredis.

Cache Money is used through a decorator you can add to your function that needs to be cached. When the decorator gets executed, Cache Money will make a unique key from the name of the function and the params received and look up in redis if there is a result for this key. If there is a result it will be used as the output of the function and the execution of the function will be skipped.

You can add a timeout in the declaration of the decorator, you can find constants for common timeout duration in cache_money/constants.py. When the timeout is reached, Redis remove the entry itself.

It's also possible to clear the cache early by using the method bust that gets added to a function decorated by Cache Money. An example is provided below.

This library is available on PyPI under the name cache-money. You can install with pip by running pip install cache-money.

Requirements

You need a redis instance running to use this library. This library was tested to run on version of Redis >= 4.0.0. If you have docker set up you can create a redis instance like this:

make redis-start

Usage

Basic usage

First thing is initializing Cache Money and decorating a function that you want to cache

from cache_money import cache_money, init_cache_money
from cache_money.constants import CACHE_HOUR, CACHE_WEEK

init_cache_money(host="localhost")

@cache_money.cached(timeout=CACHE_HOUR)
async def addition(x: int, y: int) -> int:
    return x + y

@cache_money.cached(timeout=CACHE_WEEK)
async def multiplication(x: int, y: int) -> int:
    return x * y

If you run the following calls to the function addition consecutively:

  >>> await addition(3, 4)
  7
  
  >>> await addition(3, 7)
  10
 
  >>> await addition(3, 4)
  7

The first and second call would be executed, but the third call would have used the cache in redis instead, as long as the third call was done within one hour of when the first call was made, as the function addition is caching results for one hour.

In Redis you would see two entries like this:

# redis-cli 

127.0.0.1:6379> KEYS *
1) "__main__:addition:ea53056bad64a599c84efdfd4f4cbb64"
2) "__main__:addition:bb6b7afb6a6cf3191f6d7fd35d976d42"

127.0.0.1:6379> TTL addition:ea53056bad64a599c84efdfd4f4cbb64
(integer) 3403

Busting cache for a specific function call

You can force expire (bust) the cache for a specific function call

>> await addition(3, 4)
>> await addition(3, 7)
>> await addition.bust(3, 4)

In Redis you would see one entry as the other one has been busted

127.0.0.1:6379> KEYS *
1) "__main__:addition:bb6b7afb6a6cf3191f6d7fd35d976d42"

Busting cache for all function calls of a specific function

You can bust the cache for all instance of a function call

>> await addition(3, 4)
>> await addition(3, 7)
>> await multiplication(2, 4)
>> await addition.bust_all()

In Redis you would see no entries for the function addition which has been busted, you would see one entry for multiplication

127.0.0.1:6379> KEYS *
1) "__main__:multiplication:bc3b7afc6a7cf3191f6d1fd31d810d55"

Busting cache for all function calls of all functions

You can bust the cache of all entries made by Cache Money as well

>> await addition(3, 4)
>> await addition(3, 7)
>> await multiplication(2, 4)
>> cache_money.bust()

In Redis you would see no entries

127.0.0.1:6379> KEYS *
(empty array)

Contributing and getting set up for local development

To set yourself up for development on Cache Money, make sure you are using poetry and simply run the following commands from the root directory:

make sys-deps
make install

Metadata

Release files for cache-money 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cache-money 1.3.0
File Size Uploaded Provenance Actions
cache_money-1.3.0.tar.gz 7.5 kB Without provenance Details for cache_money-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cache-money 1.3.0
File Interpreter ABI Platform Actions
cache_money-1.3.0-py3-none-any.whl Python 3 none any Details for cache_money-1.3.0-py3-none-any.whl

Total release size: 15.9 kB

Release files / cache_money-1.3.0.tar.gz

Download URL cache_money-1.3.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
04c65be96afa413db6f67cd45fc110e3ae729df4c02dddd1cd5431dfeba4a0b6
BLAKE2b-256 checksum
How to use checksums
612dada8fb8182cb3575767a4c8d22bf0081da2704fa62c5aa814431fdaffe9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.9.3-3-MANJARO

Release files / cache_money-1.3.0-py3-none-any.whl

Download URL cache_money-1.3.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a83e7dd5fd1de75d908ff5fbb1e7c8a3f6096fdfc126370e42c002d499420b95
BLAKE2b-256 checksum
How to use checksums
1a9ea1046b65c98610d3c3bb9be20604f1669e7e7015480958237a76fea0afc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.9.3-3-MANJARO

Security for cache-money 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page