vayu.cache¶
cache
¶
Serializer
¶
Bases: ABC
Strategy for converting cached values to/from bytes.
Pickler
¶
Bases: Serializer
Serializer that uses pickle with the highest protocol.
Supports arbitrary Python objects but is not safe to read from untrusted sources.
Jsoner
¶
Bases: Serializer
Serializer that uses JSON.
Uses orjson when installed (faster), otherwise falls back to the
standard-library json. Only handles JSON-serializable values.
CacheMissError
¶
Bases: Exception
Raised on cache miss or when a cached entry has expired.
Cache
¶
Cache(serializer: Serializer)
Bases: ABC
Abstract TTL cache with sync and async @cached decorators.
Subclasses implement the backend (_read / _write / _delete, each
in sync and async form). Concrete implementations include FileCache and
MemoryCache.
Source code in vayu/cache.py
cached
¶
cached(
ttl: Union[timedelta, float],
prefix: str = None,
key: Union[str, Callable] = None,
serializer: Serializer = None,
log: bool = True,
)
Used as decorator to cache function calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ttl
|
Union[timedelta, float]
|
cache expiry ttl |
required |
prefix
|
str
|
If set, gets prepended to the rest of the cache key, otherwise uses function, class, and module name to create prefix. |
None
|
key
|
Union[str, Callable]
|
|
None
|
serializer
|
Serializer
|
If not specified, default pickler serializer is used |
None
|
log
|
bool
|
If True, logs reports such as cache hit, miss etc. |
True
|
Source code in vayu/cache.py
FileCache
¶
FileCache(path: str, serializer: Serializer = None)
Bases: Cache
Persistent cache backed by files under a directory.
One file per key. Writes are atomic (write to a temp file in the same
directory, then replace). Note: cache keys become filenames, so custom
key= callables must return filesystem-legal strings (avoid /, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory to store cache files. Created if it doesn't exist. |
required |
serializer
|
Serializer
|
How to encode values. Defaults to |
None
|
Source code in vayu/cache.py
MemoryCache
¶
Bases: Cache
In-process cache backed by a plain dict.
Uses a no-op serializer — values are stored by reference, not copied.
Volatile: everything is lost when the process exits. The module-level
mem_cache is an instance of this class, and mem_cached is a
shortcut to mem_cache.cached.
Source code in vayu/cache.py
auto_adapt_to_methods
¶
Allows you to use the same decorator on methods and functions, hiding the self argument from the decorator.