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.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

yapcache-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yapcache-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for yapcache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f149373b7b7810b4fce99c076e896a9f4f67c62dd8fd20a36e58c207b9f02c2e
MD5 596cecfa49a67979ecbd0fac5f357d4d
BLAKE2b-256 c464ae8dbfebf6bfb1b012e2ae6b6b6956a6a987bc260219a423ebb4a8fcb2da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yapcache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for yapcache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cfe99e7a7b6f0a6f62c74b95fc0a960546e5cc91a767496d0d01dbda5dea0f4c
MD5 c18ee9f4140de35a2a891e31d67b9898
BLAKE2b-256 6a81fc5937a0e4aa4194c720000f8dada512e7ce5c7746494156022613e696e0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page