No project description provided
Project description
yapcache
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.3.tar.gz
(6.2 kB
view details)
Built Distribution
File details
Details for the file yapcache-0.1.3.tar.gz
.
File metadata
- Download URL: yapcache-0.1.3.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a84df212543e164cc0fe71bb6839efa9d8366a5b8bc9019c5a232fd34c596204 |
|
MD5 | ee7a5495daddfba11349394402144b62 |
|
BLAKE2b-256 | f34a5ce4fb1ad1d56cc7b86a70d51d36593ca15ee2183bd443c6322e76f19cf1 |
File details
Details for the file yapcache-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: yapcache-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cbc850b7da0bb05c6c9a5c351408ba630ad91bae2e950af75d4d589b2097621 |
|
MD5 | 4fa3a6c6a016e917a2082b8a670c3956 |
|
BLAKE2b-256 | c04e21e02e9164801fd61df9d6b561af9fb8a2dae3c428d3ee4ebac94e497c4b |