Cache Management¶
Utilities for controlling the symbol cache path, inspecting statistics, and managing cache state.
Overview¶
Yuzuha maintains three persistent SQLite databases:
| Database | File | Content |
|---|---|---|
| CG basis cache | .yuzuha/cgbasis.db |
(edges, alphas) → canonical basis array |
| X-symbol cache | .yuzuha/xsymbol.db |
(spec_a, spec_b, contraction) → (x_array, spec_c) |
| R-symbol cache | .yuzuha/rsymbol.db |
(spec, permutation) → (r_array, spec_permuted) |
All three databases are created automatically on first use and are thread-safe.
Cache Path¶
The cache directory is resolved in the following priority order:
- Thread-local override (set by
TestCacheContext) — highest priority - Environment variable:
YUZUHA_CACHE_PATH - Default:
.yuzuha/in the current working directory
Setting the Cache Path¶
import yuzuha
# Set via Python
yuzuha.set_cache_path('/path/to/cache')
# Or via environment variable (before importing yuzuha)
# export YUZUHA_CACHE_PATH=/path/to/cache
API Reference¶
set_cache_path¶
set_cache_path
¶
Set the cache directory path programmatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or None
|
Cache directory path. If None, uses default. |
required |
reset_caches¶
reset_caches
¶
Reset global cache instances.
This is useful for testing to ensure clean state between tests.
clear_all_caches¶
clear_all_caches
¶
get_cache_stats¶
get_cache_stats
¶
Get statistics for all symbol caches.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with keys 'xsymbol' and 'rsymbol', each containing cache statistics (size, db_path, db_size_bytes). |
Examples:
print_cache_stats¶
print_cache_stats
¶
Print a formatted summary of cache statistics.
This is a convenience function for quickly inspecting cache status.
Examples:
TestCacheContext¶
TestCacheContext
¶
Usage Examples¶
Inspecting Cache State¶
import yuzuha
# Print a human-readable summary
yuzuha.print_cache_stats()
# X-symbol cache:
# Entries: 42
# Database: .yuzuha/xsymbol.db
# Size: 1.2 MB
# R-symbol cache:
# Entries: 18
# Database: .yuzuha/rsymbol.db
# Size: 0.5 MB
# Or get the raw dict
stats = yuzuha.get_cache_stats()
print(stats['xsymbol']['size']) # number of cached X-symbols
print(stats['rsymbol']['db_path']) # path to R-symbol database
Clearing Caches¶
Isolated Cache for Testing¶
import yuzuha
from yuzuha import TestCacheContext
with TestCacheContext():
# All cache operations use an isolated temporary directory
x, spec_c = yuzuha.compute_xsymbol(spec_a, spec_b, contraction)
# Temporary cache is cleaned up automatically on exit
See Also¶
- Database: Pre-populate the canonical basis cache
- X-Symbol: Uses the X-symbol cache transparently
- R-Symbol: Uses the R-symbol cache transparently
- Yuzuha Protocol — Data Caching: Formal specification of the caching interface
Notes¶
reset_caches() closes and destroys the in-memory cache objects so that the next
call to compute_xsymbol or compute_rsymbol opens a fresh connection. It does
not delete the database files. Use clear_all_caches() to remove cached entries
while preserving the file and schema.