Skip to main content

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

Project description

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.0.tar.gz (14.7 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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for miniperscache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 324f63d5a5d3b9f619ed9c6832f3533866c9d227b5c971d847203d53f94bffd2
MD5 2700b16763caa2c4b54dbe970f2eddc2
BLAKE2b-256 13012515ab41765b6175c2619a538ada8026cfe76686fb8d17fb1f523023b173

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for miniperscache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b95053ee0ec12b4663cdce393ad845f56e3d2f45db7bc437becc7a4ed8a231b3
MD5 7dd4989bd04b389eaf4b1d6a706cf9fd
BLAKE2b-256 d59b5d3da5e7b537a4c3b615dbfcbaef3e7b96cf75a1d611d2dd996bcc72f470

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