Skip to main content

A collection of helpful utilities for coredis.

Project description

PyPI Downloads Release

coredis-utils

A collection of helpful utilities for coredis.

Features

  • Caching decorator with thundering herd protection and error caching
  • Idempotency keys
  • Fixed-window rate limiting

Installation

$ pip install coredis-utils

Getting started

First, create a CoredisUtils object wrapping a coredis.Redis instance:

from coredis import Redis
from coredis_utils import CoredisUtils

client = Redis(...)
utils = CoredisUtils(client)

Caching is implemented with a decorator:

@utils.cached(ttl=60)
async def my_task() -> int: ...

Idempotency uses a simple check:

if await utils.idempotent("my-key", ttl=60):
    ...  # code in this block can only run once

Rate limiting is similar:

for _ in range(10):
    if await utils.limit("my-ip-addr", 5, 1):  # limit to 5/second
        print("success")
success
success
success
success
success

Advanced caching

Cache keys are generated using a SHA256 hash of pickled arguments. You can exclude non-serializable arguments from cache key construction:

from sqlalchemy.ext.asyncio import AsyncSession

@utils.cached(ttl=60, exclude={"session"})
async def my_task(session: AsyncSession) -> int: ...

You can also customize which parts of arguments get hashed:

@utils.cached(
    ttl=60,
    key_fns={
        # hash just the ID, not the entire model
        "user": lambda u: u.id,
        # hash a couple relevant fields
        "message": lambda m: (m.type, m.timestamp),
    },
)
async def my_task(user: User, message: Message) -> int: ...

Errors can be cached and propagated just like normal responses:

@utils.cached(ttl=60, error_ttl=5)
async def my_task() -> int:
    raise Exception("Oh no!")

You can easily invalidate keys by passing the same arguments:

@utils.cached(ttl=60)
async def my_task(time: int) -> int: ...

await my_task.invalidate(3)

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

coredis_utils-0.3.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

coredis_utils-0.3.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file coredis_utils-0.3.0.tar.gz.

File metadata

  • Download URL: coredis_utils-0.3.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for coredis_utils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 be94a47455edf00abc85c9468076dfb6b299c11a7a19cbe5d9b9e14a6552efb6
MD5 5c49f1e7cff21a111fef87d70e7b95f7
BLAKE2b-256 ff3fd6cae3465caffd959ee3a0b946ee44b8a83c25fe9eaaf7b21684ca971085

See more details on using hashes here.

File details

Details for the file coredis_utils-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: coredis_utils-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for coredis_utils-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 106678c977111812494b7675bf99b915d75809204d29b97fc3301095e4700d6d
MD5 518648ef012f7ecad1890a02553f80f4
BLAKE2b-256 5d5076bd463170a240f2513e63bee5bd5abe1e19efbab4331175dce6e91502bc

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