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.2.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.2-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for miniperscache-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a13f4db22381a3ea45bbe807e701d10dabbfa9034d35845dc1c6c6e0ac83164
MD5 d0ef2ac1c93ef7e44df96e3edd1ced8f
BLAKE2b-256 b0f659d2e3a213797d5b9328b308b8933a6932c2834ae7489c48549fa5a6a4dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for miniperscache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 056e8c80e88590a8ed3e2a55ed47968915a5b78489b182f9b7e40e7b84e4ebd8
MD5 d6c6b9956c400017343cf8dcecb2de1d
BLAKE2b-256 795483e8a513f9af88e2bcf592040e6c636315caf815e818b142a0d80909f165

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