uf.caching#
Result caching for uf.
Provides caching decorators and backends to cache function results, improving performance for expensive operations.
Functions
|
Clear all cache entries. |
|
Invalidate a specific cache entry. |
|
Decorator to cache function results. |
Get the global cache backend. |
|
|
Create a cache key from function call. |
|
Set the global cache backend. |
Classes
Base class for cache backends. |
|
Track cache hit/miss statistics. |
|
|
Disk-based cache backend using pickle. |
|
In-memory cache backend. |
- class uf.caching.CacheBackend[source]#
Bases:
objectBase class for cache backends.
- class uf.caching.CacheStats[source]#
Bases:
objectTrack cache hit/miss statistics.
- class uf.caching.DiskCache(cache_dir='.uf_cache', default_ttl=3600)[source]#
Bases:
CacheBackendDisk-based cache backend using pickle.
Persists cache to disk, suitable for larger datasets or persistence across restarts.
Example
>>> cache = DiskCache(cache_dir='/tmp/uf_cache') >>> cache.set('expensive_result', big_data)
- class uf.caching.MemoryCache(default_ttl=3600, max_size=1000)[source]#
Bases:
CacheBackendIn-memory cache backend.
Simple dictionary-based caching suitable for single-process applications.
Example
>>> cache = MemoryCache(default_ttl=3600) >>> cache.set('key', 'value', ttl=60) >>> value = cache.get('key')
- uf.caching.cache_clear_all(backend=None)[source]#
Clear all cache entries.
- Parameters:
backend (
Optional[CacheBackend]) – Cache backend (uses global if None)- Return type:
- uf.caching.cache_invalidate(cache_key, backend=None)[source]#
Invalidate a specific cache entry.
- Parameters:
cache_key (
str) – Cache key to invalidatebackend (
Optional[CacheBackend]) – Cache backend (uses global if None)
- Return type:
- Returns:
True if invalidated
- uf.caching.cached(ttl=3600, backend=None, key_func=None)[source]#
Decorator to cache function results.
- Parameters:
ttl (
int) – Time to live in secondsbackend (
Optional[CacheBackend]) – Cache backend (uses global MemoryCache if None)key_func (
Optional[Callable]) – Optional function to generate cache key
- Returns:
Decorator function
Example
>>> @cached(ttl=3600) ... def expensive_calculation(x: int, y: int) -> int: ... # Only runs once per unique (x, y) combination ... return heavy_computation(x, y) >>> >>> result = expensive_calculation(10, 20) # Computes >>> result2 = expensive_calculation(10, 20) # From cache