glazing.utils.cache¶
Caching utilities.
cache
¶
Caching utilities for the glazing package.
This module provides caching mechanisms for query results and cross-reference resolution to improve performance when working with large linguistic datasets.
| CLASS | DESCRIPTION |
|---|---|
LRUCache |
Thread-safe Least Recently Used cache implementation. |
TTLCache |
Time-To-Live cache with automatic expiration. |
QueryCache |
Specialized cache for dataset queries. |
PersistentCache |
Optional file-based persistent cache. |
| FUNCTION | DESCRIPTION |
|---|---|
generate_cache_key |
Generate a unique cache key from function arguments. |
cached_method |
Decorator for caching method results. |
clear_all_caches |
Clear all active caches in the application. |
Notes
The caching system is designed to be thread-safe and can handle concurrent access from multiple threads. All caches can be disabled globally for testing or debugging purposes.
Classes¶
CacheBase()
¶
Base class for all cache implementations.
Provides common interface and registration for cache management.
Initialize and register the cache.
| METHOD | DESCRIPTION |
|---|---|
clear |
Clear all entries from the cache. |
is_enabled |
Check if caching is enabled. |
size |
Get the number of entries in the cache. |
Source code in src/glazing/utils/cache.py
LRUCache(max_size: int = 128)
¶
Bases: CacheBase
Thread-safe Least Recently Used cache.
| PARAMETER | DESCRIPTION |
|---|---|
max_size
|
Maximum number of entries to store.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
hits |
Number of cache hits.
TYPE:
|
misses |
Number of cache misses.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
get |
Get a value from the cache. |
put |
Store a value in the cache. |
clear |
Clear all entries. |
get_stats |
Get cache statistics. |
Examples:
>>> cache = LRUCache[str](max_size=100)
>>> cache.put("key1", "value1")
>>> value = cache.get("key1")
>>> print(cache.get_stats())
Initialize the LRU cache.
Source code in src/glazing/utils/cache.py
Functions¶
clear() -> None
¶
get(key: Hashable, default: T | None = None) -> T | None
¶
Get a value from the cache.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
default
|
Default value if key not found.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T | None
|
The cached value or default. |
Source code in src/glazing/utils/cache.py
get_stats() -> dict[str, int | float]
¶
Get cache statistics.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, int | float]
|
Cache statistics including hits, misses, and hit rate. |
Source code in src/glazing/utils/cache.py
put(key: Hashable, value: T) -> None
¶
Store a value in the cache.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
value
|
The value to cache.
TYPE:
|
Source code in src/glazing/utils/cache.py
PersistentCache(cache_dir: Path | str, serializer: str = 'json')
¶
Bases: CacheBase
File-based persistent cache.
| PARAMETER | DESCRIPTION |
|---|---|
cache_dir
|
Directory to store cache files.
TYPE:
|
serializer
|
Serialization method ("json" or "pickle").
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
get |
Get a value from persistent storage. |
put |
Store a value persistently. |
Initialize the persistent cache.
Source code in src/glazing/utils/cache.py
Functions¶
clear() -> None
¶
get(key: str, default: T | None = None) -> T | None
¶
Get a value from persistent storage.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
default
|
Default value if not found.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T | None
|
The cached value or default. |
Source code in src/glazing/utils/cache.py
put(key: str, value: T) -> None
¶
Store a value persistently.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
value
|
The value to cache.
TYPE:
|
Source code in src/glazing/utils/cache.py
QueryCache(max_size: int = 256, ttl: float = 600.0)
¶
Bases: CacheBase
Specialized cache for dataset queries.
Combines LRU and TTL strategies for optimal query caching.
| PARAMETER | DESCRIPTION |
|---|---|
max_size
|
Maximum number of cached queries.
TYPE:
|
ttl
|
Time-to-live for query results.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
get_query_result |
Get cached query result. |
cache_query_result |
Cache a query result. |
invalidate_query_type |
Invalidate all queries of a specific type. |
Initialize the query cache.
Source code in src/glazing/utils/cache.py
Functions¶
cache_query_result(query_type: str, params: QueryParams, result: CacheValue, ttl: float | None = None) -> None
¶
Cache a query result.
| PARAMETER | DESCRIPTION |
|---|---|
query_type
|
Type of query.
TYPE:
|
params
|
Query parameters.
TYPE:
|
result
|
Query result to cache.
TYPE:
|
ttl
|
Custom TTL for this result.
TYPE:
|
Source code in src/glazing/utils/cache.py
clear() -> None
¶
get_query_result(query_type: str, params: QueryParams) -> CacheValue | None
¶
Get cached query result.
| PARAMETER | DESCRIPTION |
|---|---|
query_type
|
Type of query (e.g., "frame_by_lemma").
TYPE:
|
params
|
Query parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheValue | None
|
Cached result or None. |
Source code in src/glazing/utils/cache.py
get_stats() -> dict[str, int | float | dict[str, int | float]]
¶
Get combined cache statistics.
invalidate_query_type(query_type: str) -> None
¶
Invalidate all queries of a specific type.
| PARAMETER | DESCRIPTION |
|---|---|
query_type
|
Type of query to invalidate.
TYPE:
|
Source code in src/glazing/utils/cache.py
TTLCache(max_size: int = 128, ttl: float = 300.0)
¶
Bases: CacheBase
Time-To-Live cache with automatic expiration.
| PARAMETER | DESCRIPTION |
|---|---|
max_size
|
Maximum number of entries.
TYPE:
|
ttl
|
Time-to-live in seconds for each entry.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
get |
Get a value if not expired. |
put |
Store a value with optional custom TTL. |
cleanup |
Remove expired entries. |
Examples:
>>> cache = TTLCache[str](max_size=100, ttl=60.0)
>>> cache.put("key1", "value1")
>>> time.sleep(61)
>>> cache.get("key1") # Returns None (expired)
Initialize the TTL cache.
Source code in src/glazing/utils/cache.py
Functions¶
cleanup() -> int
¶
Remove expired entries.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of entries removed. |
Source code in src/glazing/utils/cache.py
clear() -> None
¶
get(key: Hashable, default: T | None = None) -> T | None
¶
Get a value if not expired.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
default
|
Default value if key not found or expired.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T | None
|
The cached value or default. |
Source code in src/glazing/utils/cache.py
put(key: Hashable, value: T, ttl: float | None = None) -> None
¶
Store a value with TTL.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The cache key.
TYPE:
|
value
|
The value to cache.
TYPE:
|
ttl
|
Custom TTL in seconds, or use default.
TYPE:
|
Source code in src/glazing/utils/cache.py
Functions¶
cached_method(cache: CacheBase | None = None, ttl: float | None = None) -> Callable[[Callable[..., T]], Callable[..., T]]
¶
Decorator for caching method results.
| PARAMETER | DESCRIPTION |
|---|---|
cache
|
Cache instance to use, or create LRU cache.
TYPE:
|
ttl
|
Time-to-live for cached results.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorator that adds caching to a function. |
Examples:
Source code in src/glazing/utils/cache.py
clear_all_caches() -> int
¶
Clear all active caches in the application.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of caches cleared. |
Source code in src/glazing/utils/cache.py
generate_cache_key(*args, **kwargs) -> str
¶
Generate a unique cache key from function arguments.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Positional arguments.
DEFAULT:
|
**kwargs
|
Keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A unique cache key. |
Source code in src/glazing/utils/cache.py
set_caching_enabled(enabled: bool) -> None
¶
Enable or disable all caching globally.
| PARAMETER | DESCRIPTION |
|---|---|
enabled
|
Whether caching should be enabled.
TYPE:
|