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:

  • PickleSerializer (default)
  • 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.1.tar.gz (20.4 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.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for miniperscache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 91eab39117fb6fc64ed5040ad0bebf37e68a91423be1dea46bdaf08ce152464b
MD5 baf6611fd1fea092a66653b143fcd0ed
BLAKE2b-256 2ea7e3c42b6c1d3066a86cb97c69c6aaef421dac38d8d5e503d6d5dcf5b79811

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for miniperscache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ea35ae0e0837e1301da25938159926455457e1dc6730860350b21d470d5fbee
MD5 b3e9cc315ddbd227dac08f708a092705
BLAKE2b-256 3f6bb6a2ca8b9a0ad729d2b9f00c5f1aa110d498f3a8802add5dd959e8b9bd02

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