Skip to main content

No project description provided

Project description

yapcache

PyPI - Version PyPI - Python Version

Yet another Python cache library.

Table of Contents

Installation

pip install yapcache

Usage

In memory cache:

import asyncio
from yapcache import memoize
from yapcache.caches.memory import InMemoryCache

cache = InMemoryCache(maxsize=1_000, key_prefix=f"example:")


@memoize(cache, ttl=60, cache_key=lambda n: f"slow-{n}")
async def slow_fn(n: int):
    await asyncio.sleep(n)


async def example():
    await slow_fn(3)
    await slow_fn(3)  # cached!


asyncio.run(example())

Redis cache:

# ...
from redis.asyncio.client import Redis
from yapcache.caches.redis import RedisCache


redis_client = Redis()
cache = RedisCache(redis_client, key_prefix=f"example:")

Multi-layer cache:

# ...
from yapcache.caches import MultiLayerCache

redis_client = Redis()

cache = MultiLayerCache(
    [InMemoryCache(maxsize=2_000), RedisCache(redis_client)],
    key_prefix=f"example:",
)

Use lock parameter to protect against thundering herd (only one coroutine/thread will do the work and update the cache):

# ...
from yapcache.distlock import RedisDistLock

@memoize(
    cache,
    ttl=60,
    cache_key=lambda n: f"slow-{n}",
    lock=lambda key: RedisDistLock(redis_client, key),
)
async def slow_fn(n: int):
    # ...

Use best_before to serve stale data (update the cache in background):

@memoize(
    cache,
    ttl=60,
    cache_key=lambda n: f"slow-{n}",
    best_before=lambda n: time.time() + 30
)
async def slow_fn(n: int):
    # ...

License

yapcache is distributed under the terms of the MIT license.

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

yapcache-0.1.22.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

yapcache-0.1.22-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file yapcache-0.1.22.tar.gz.

File metadata

  • Download URL: yapcache-0.1.22.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yapcache-0.1.22.tar.gz
Algorithm Hash digest
SHA256 fadd05a4ddf2ee19888a7bea7175d77077752b30ef124c6bfd4da0c871cc35b8
MD5 d698218b26d943741643badf797ce5d7
BLAKE2b-256 6d186589ab38a0de36cfc3ffdd382c7ddbd9db8f56d4b429a78bcbff20ebd2d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for yapcache-0.1.22.tar.gz:

Publisher: build.yml on ecarrara/yapcache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yapcache-0.1.22-py3-none-any.whl.

File metadata

  • Download URL: yapcache-0.1.22-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yapcache-0.1.22-py3-none-any.whl
Algorithm Hash digest
SHA256 30e8649a8947e8840df4bb44bf4265675744205830acbf89fd31241d875684c4
MD5 ce82051b3535daee85f3b2f5113a75ed
BLAKE2b-256 5309128a7c1e432bb1c57b9f5b5585ccf5dfab728d09cbf4f4758554e8a52d2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for yapcache-0.1.22-py3-none-any.whl:

Publisher: build.yml on ecarrara/yapcache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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