Skip to main content

pypi

A small python package for persistent caching of function results. Main features:

  • Supports both sync and async functions.
  • Properly typed, so your decorated functions will have the same type hints as the original function.
  • Customizable serializers and storage backends.

Unsupported, todo:

  • TTL/expiry
  • Class/instance methods

Examples

Sync

from miniperscache import cached

@cached(tag="expensive_calculation")
def expensive_calculation(x: int, y: int) -> float:
    return x * y

Async

from miniperscache import cached_async

@cached_async(tag="expensive_calculation")
async def expensive_calculation(x: int, y: int) -> float:
    result = await fetch_expensive_data(x, y)
    return result

Custom serializers and storage

from miniperscache import cached, FileStorage, JsonSerializer

@cached(tag="expensive_calculation", storage=FileStorage(), value_serializer=JsonSerializer())
def expensive_calculation(x: int, y: int) -> float:
    ...

Skipping unhashable arguments

The cache works by hashing the arguments of the function call. If some of the arguments, e.g. random Python objects, are unhashable, you can skip them by passing a custom arg hasher set to DefaultArgHasher(skip_args=["arg_names", ...]):

from miniperscache import cached_async, DefaultArgHasher

@cached_async(tag="expensive_calculation", arg_hasher=DefaultArgHasher(skip_args=["client"]))
async def model_call(client: ClientObject, prompt: str) -> float:
    res = await client.messages.create(
        prompt=prompt
    )
    return res.content

Or, you can provide your own arg hasher

from miniperscache import cached_async, default_raw_arg_hasher

def model_call_arg_hasher(client: ClientObject, prompt: str) -> bytes:
    return default_raw_arg_hasher(client.model, prompt)

@cached_async(tag="expensive_calculation", arg_hasher=model_call_arg_hasher)
async def model_call(client: ClientObject, prompt: str) -> float:
    res = await client.messages.create(
        ...
        prompt=prompt
    )
    return res.content

Deletion

TTL / more systematic expiration isn't supported yet, but what you can do is:

from miniperscache import SqliteStorage
SqliteStorage().delete_tag("expensive_calculation")

Customization

Serializers

Currently:

  • DillSerializer (default)
  • PickleSerializer
  • JsonSerializer

Storage

  • SqliteStorage (default) -- Note: serializing to/from disk should be very fast so we don't even provide AsyncSqliteStorage - if writing to disk becomes a bottleneck for you, you can implement it using aiosqlite.
  • FileStorage
  • AsyncFileStorage

Release files for miniperscache 0.1.3

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

Source distribution (sdist)

Source distribution for miniperscache 0.1.3
File Size Uploaded
miniperscache-0.1.3.tar.gz 21.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for miniperscache 0.1.3
File Interpreter ABI Platform
miniperscache-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 35.6 kB

Release files / miniperscache-0.1.3.tar.gz

Download URL miniperscache-0.1.3.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6a4cb1972f3cc189d72c8e292948483492503fa25462e022919e490a9392f4c5
BLAKE2b-256 checksum
How to use checksums
b9eaf0f103812f66323a56aa0da7aa204b3aff2cfe0b693b8c93155bf878a315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.5

Release files / miniperscache-0.1.3-py3-none-any.whl

Download URL miniperscache-0.1.3-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10a7d5f7bbd4c02373af3f17f7b0a7b954ded661ee8ff239717f1a03ba36d4cb
BLAKE2b-256 checksum
How to use checksums
e5a2077612343a5784e3c64bd486b058e19069ee55f4a38615c03253172f9e2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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