Skip to main content

Python cache decorator and other itils with support for Redis or KeyDB

Project description

redis-cached

Python cache decorator that uses Redis or KeyDB as storage. This is very handy for replicated apps (e.g. Kubernetes), AWS Lambda functions, and other stateless apps.

Features:

  • Writing to the cache happens asynchronously, so that you get the function result immediately.
  • Function result and kwarg values are pickled, so you can work with complex structures like pydantic's BaseModel
  • Cache invalidation is available

Limitations:

  • Only async functions are supported.
  • Only keyword arguments are supported. It will raise an error if you pass non-kwargs while calling your function.

Basic usage:

import asyncio
from redis_cached import cached, invalidate_cache

@cached(5)
async def add_one(x):
    return x + 1

async def main():
    result = await add_one(x=2)  # result is cached for 5 seconds
    
    # Pass the same kwargs to this func to invalidate the cache
    await invalidate_cache('add_one', x=2)

asyncio.run(main())

Optionally, add salt to the decorator to avoid clashing with the same-named functions in other modules or other apps that use the same Redis or KeyDB database:

import asyncio
from redis_cached import cached, invalidate_cache

CACHE_SALT = 'XnsJ-7C9PIU0qhDwh9YhJQ'

@cached(5, cache_key_salt=CACHE_SALT)
async def add_one(x):
    return x + 1

async def invalidate_add_one(**kwargs):
    await invalidate_cache('add_one', cache_key_salt=CACHE_SALT, **kwargs)

async def main():
    result = await add_one(x=2)  # cached
    await invalidate_add_one(x=2)  # invalidated

asyncio.run(main())

Contributing

Contributions are welcome. Please refer to maintenance readme for more details.

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_cached-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

redis_cached-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file redis_cached-0.1.0.tar.gz.

File metadata

  • Download URL: redis_cached-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for redis_cached-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dbb553ba4bea52c757db699e834f1b6df9c6ef00a67e01eed3728cf818319c52
MD5 e1a1bc4c9511861f4dd2cb80756156f2
BLAKE2b-256 bba49ce0e1ce9ffe2b8a9a6c7bb7b39943239f5055deb658402c8ef87612d004

See more details on using hashes here.

File details

Details for the file redis_cached-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: redis_cached-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for redis_cached-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19a07a3109f148d41bdae62d59c42fa30e400648a05f582d36819005113ba873
MD5 0dde9a8ef4e40b727bc114a3d4b66d48
BLAKE2b-256 c46f078e2a66def1d688495faa308d0b5c7b5afc2cdf004e3445a3a37c610c7c

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