Skip to main content

miniperscache is a small Python library for persistent caching of function results.

Project description

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

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

miniperscache-0.1.3.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

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

miniperscache-0.1.3-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file miniperscache-0.1.3.tar.gz.

File metadata

  • Download URL: miniperscache-0.1.3.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for miniperscache-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6a4cb1972f3cc189d72c8e292948483492503fa25462e022919e490a9392f4c5
MD5 bb31ac4420fca0d85fe22643ad2e032f
BLAKE2b-256 b9eaf0f103812f66323a56aa0da7aa204b3aff2cfe0b693b8c93155bf878a315

See more details on using hashes here.

File details

Details for the file miniperscache-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for miniperscache-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10a7d5f7bbd4c02373af3f17f7b0a7b954ded661ee8ff239717f1a03ba36d4cb
MD5 042b268f65eae522c7e91ff832ae31df
BLAKE2b-256 e5a2077612343a5784e3c64bd486b058e19069ee55f4a38615c03253172f9e2a

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