Skip to main content

Sync and async cache with TTL and LRU support.

Project description

cachium

Sync and async cache

Features

  • Both synchronous and asynchronous API
  • Type-safe with full typing support
  • Customizable key builders and serializers
  • Thread-safe and coroutine-safe with dog-piling prevention

Installation

pip install cachium

Quick Start

from cachium import cache

# Simple function caching
@cache()
def expensive_calculation(x: int, y: int) -> int:
    print(f"Calculating {x} + {y}")
    return x + y

# First call performs the calculation
result1 = expensive_calculation(1, 2)  # Output: Calculating 1 + 2
print(result1)  # Output: 3

# Second call uses cached result
result2 = expensive_calculation(1, 2)  # No calculation performed
print(result2)  # Output: 3

# Async function caching works too
@cache()
async def async_expensive_calculation(x: int, y: int) -> int:
    print(f"Calculating {x} + {y} asynchronously")
    return x + y

# Usage with async functions
import asyncio

async def main():
    # First call performs the calculation
    result1 = await async_expensive_calculation(1, 2)
    # Second call uses cached result
    result2 = await async_expensive_calculation(1, 2)

    print(result1, result2)  # Output: 3 3

asyncio.run(main())

Advanced Usage

Custom TTL and Cache Size

from datetime import timedelta
from cachium import cache
from cachium._storages import TTLMapStorage

@cache(storage=lambda: TTLMapStorage(max_size=100, ttl=timedelta(hours=1)))
def long_lived_cache_function(x):
    return x * 2

Custom Key Builders

from cachium import cache, DefaultKeyBuilder

# Create a custom key builder
key_builder = DefaultKeyBuilder(
    prefix="custom_prefix",
    func=lambda x, y: x + y,
    delimiter=":"
)

@cache(key_builder=key_builder)
def my_function(x, y):
    return x + y

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

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

cachium-0.0.1a1.tar.gz (83.0 kB view details)

Uploaded Source

Built Distribution

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

cachium-0.0.1a1-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file cachium-0.0.1a1.tar.gz.

File metadata

  • Download URL: cachium-0.0.1a1.tar.gz
  • Upload date:
  • Size: 83.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.12

File hashes

Hashes for cachium-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 96626cc3cac19922251b3353035b49143116260477f77eb17c9824a61763033a
MD5 a1c3c908866e1aee1ca068804b2a88bd
BLAKE2b-256 8a3a026c2f4f1d7206ed517245a283f44011c6761153972fd6cb5e266d45939a

See more details on using hashes here.

File details

Details for the file cachium-0.0.1a1-py3-none-any.whl.

File metadata

  • Download URL: cachium-0.0.1a1-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.12

File hashes

Hashes for cachium-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 6fac50cb9c96fc949c6328be09fdb6739e7cab05a8c71357a9457ce4220da5f0
MD5 dab6e7793b845bd488ba9f918f8be1be
BLAKE2b-256 7a8a9fdaff8f6430e29a7049aa4275a884b7f3091429b0dd8a02d571c4aa4648

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