Spaces:
No application file
No application file
File size: 11,392 Bytes
669d6a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | # Cache System Migration Guide
## 🎯 TL;DR - What Changed
**Auto-versioning is now ENABLED BY DEFAULT.**
Your cache will automatically invalidate when function code changes. This prevents stale cache bugs.
**Most users need to do nothing** - just update and enjoy automatic cache invalidation.
**Only opt-out if:**
- Function takes hours/days to compute AND
- Function is stable/won't change AND
- You understand the risk of stale results
---
## Overview of Changes
1. **`auto_versioning=True` by default**: Cache keys include function source hash
2. **One decorator to rule them all**: `@cacheable()` replaces multiple decorators
3. **Removed `smart_cacheable`**: Now redundant (built into default behavior)
4. **Selective cleaner refocused**: Maintenance tool for orphaned caches
---
## Quick Migration Table
| Old Code | New Code | Notes |
|----------|----------|-------|
| `@robust_cacheable` | `@cacheable()` | Now has auto-versioning by default |
| `@time_aware_cacheable` | `@cacheable(time_aware=True)` | Now has auto-versioning by default |
| `@cv_cacheable` | `@cacheable()` | Now has auto-versioning by default |
| `@smart_cacheable` | `@cacheable()` | **REMOVED - now default behavior** |
| `@cacheable()` (old) | `@cacheable(auto_versioning=False)` | **Only if you need old behavior** |
---
## What is Auto-Versioning?
### The Problem It Solves
```python
# Without auto-versioning
@cacheable(auto_versioning=False)
def calculate_returns(prices):
return prices.pct_change()
calculate_returns(df) # Cache miss, stores result
calculate_returns(df) # Cache hit ✓
# Developer fixes a bug
def calculate_returns(prices):
return prices.pct_change().fillna(0) # Bug fix!
calculate_returns(df) # Cache HIT - WRONG RESULT! ❌
# Returns OLD buggy result from cache
```
### With Auto-Versioning (Now Default)
```python
# With auto-versioning (NEW DEFAULT)
@cacheable() # auto_versioning=True by default
def calculate_returns(prices):
return prices.pct_change()
calculate_returns(df) # Cache miss, stores at key "v_abc123..."
calculate_returns(df) # Cache hit ✓
# Developer fixes bug
def calculate_returns(prices):
return prices.pct_change().fillna(0) # Bug fix!
calculate_returns(df) # Cache MISS - new key "v_def456..." ✓
# Computes with NEW correct code
```
---
## Migration Steps
### Step 1: Update `smart_cacheable` (REQUIRED)
**Old code:**
```python
from afml.cache import smart_cacheable
@smart_cacheable
def my_function(data):
return data.mean()
```
**New code:**
```python
from afml.cache import cacheable
@cacheable() # That's it! auto_versioning is now default
def my_function(data):
return data.mean()
```
### Step 2: Review Expensive Functions (OPTIONAL)
If you have functions that take **hours to compute** and **rarely change**:
```python
@cacheable(auto_versioning=False) # Explicit opt-out
def train_huge_model(data):
"""Takes 48 hours, changes once per year"""
return expensive_training(data)
```
⚠️ **Warning**: With `auto_versioning=False`, adding a comment invalidates cache:
```python
@cacheable(auto_versioning=False)
def train_huge_model(data):
"""Added this docstring""" # THIS CHANGE WON'T INVALIDATE CACHE
return expensive_training(data) # May return stale result!
```
### Step 3: Clean Up Old Caches (RECOMMENDED)
After migration, clean up orphaned caches:
```python
from afml.cache import cache_maintenance
# One-time cleanup after migration
cache_maintenance(
clean_orphaned=True,
max_cache_size_mb=1000,
max_age_days=30
)
```
---
## Understanding Auto-Versioning Behavior
### How Cache Keys Work
**Without auto-versioning:**
```
cache_key = md5("module.function_name" + "arg_hashes")
= "a1b2c3d4..."
```
**With auto-versioning (default):**
```
cache_key = md5("module.function_name" + "v_abc123" + "arg_hashes")
^^^^^^^^^^
function source hash
= "e5f6g7h8..." # Different key!
```
### When Cache Invalidates
Cache invalidates when:
- ✅ Function body changes
- ✅ Function name changes
- ✅ Default parameters change
- ✅ Decorators change
- ❌ Comments change (graceful: uses file mtime as fallback)
- ❌ Docstrings change (graceful: uses file mtime as fallback)
### Graceful Fallback
For built-in/dynamic functions where source is unavailable:
```python
# Can't get source for built-ins
import numpy as np
@cacheable() # Gracefully falls back to file mtime
def use_builtin(data):
return np.mean(data) # np.mean has no source
# Warning logged, but doesn't crash
```
---
## Common Scenarios
### Scenario 1: Development (Default - No Changes Needed)
```python
from afml.cache import cacheable
@cacheable() # Just use defaults!
def my_feature(data, window):
"""Feature under active development"""
return data.rolling(window).mean()
# Work normally - cache auto-invalidates on changes
result1 = my_feature(df, 20)
result2 = my_feature(df, 20) # Cache hit
# ... modify my_feature ...
result3 = my_feature(df, 20) # Cache miss (automatic!)
```
### Scenario 2: Expensive Computation (Explicit Opt-Out)
```python
from afml.cache import cacheable
@cacheable(auto_versioning=False) # Explicit opt-out
def train_production_model(data):
"""Takes 24 hours, changes rarely, want to preserve cache"""
return expensive_training(data)
```
### Scenario 3: Bulk Opt-Out for Stable Functions
```python
from afml.cache import disable_auto_versioning
# Create custom decorator without versioning
cacheable_stable = disable_auto_versioning()
@cacheable_stable()
def stable_func_1(data): ...
@cacheable_stable()
def stable_func_2(data): ...
@cacheable_stable(time_aware=True) # Can combine with other options
def stable_func_3(data): ...
```
### Scenario 4: Mixed Strategy
```python
from afml.cache import cacheable
# Under development - auto-versioning
@cacheable()
def experimental_feature(data):
return data.ewm(span=20).mean()
# Production stable - opt-out
@cacheable(auto_versioning=False)
def load_data(symbol, start, end):
return expensive_data_load(symbol, start, end)
```
---
## Maintenance & Cleanup
### Periodic Cleanup (Recommended)
Set up weekly/monthly cleanup:
```python
from afml.cache import cache_maintenance
# Run weekly via cron/scheduler
cache_maintenance(
clean_orphaned=True, # Remove old function versions
max_cache_size_mb=2000, # Enforce size limit
max_age_days=90, # Remove very old caches
min_orphan_age_hours=48 # Keep recent orphans (grace period)
)
```
### Analyze Cache Fragmentation
Check if auto-versioning is creating too many versions:
```python
from afml.cache import print_version_analysis
print_version_analysis()
# Output:
# ========================================
# CACHE VERSION ANALYSIS
# ========================================
# Functions with versions: 12
# Total versions: 34
# Total size: 1.2 GB
#
# Top fragmented functions:
# 1. calculate_feature
# Versions: 8
# Size: 450 MB
```
If fragmentation is high, consider opting out for those functions.
---
## Performance Implications
### Overhead of Auto-Versioning
**Minimal overhead** - hash computed once at decorator application:
```python
# Old smart_cacheable: 0.5ms PER CALL
@smart_cacheable # Read source + hash on EVERY call
def fast_func(x):
return x + 1
# New auto_versioning: 0ms per call
@cacheable() # Hash computed ONCE at import time
def fast_func(x):
return x + 1
```
### Storage Implications
With auto-versioning, multiple versions can coexist temporarily:
```bash
cache/
my_module/
my_function/
v_abc123_args_xyz/ # Version 1 (orphaned)
v_def456_args_xyz/ # Version 2 (current)
v_ghi789_args_xyz/ # Version 3 (current)
```
**Mitigation**: Run `cache_maintenance()` periodically to clean orphans.
---
## Testing Your Migration
### 1. Check for `smart_cacheable` usage
```bash
# This should find zero results after migration
grep -r "smart_cacheable" your_project/
```
### 2. Test auto-versioning behavior
```python
from afml.cache import cacheable
@cacheable()
def test_func(x):
return x * 2
# First call
result1 = test_func(5) # Cache miss
# Second call (should hit)
result2 = test_func(5) # Cache hit
# Change function
def test_func(x):
return x * 3 # Changed!
# Third call (should miss due to version change)
result3 = test_func(5) # Cache miss (automatic!)
assert result3 == 15 # New result
```
### 3. Verify cleanup works
```python
from afml.cache import find_orphaned_caches
orphans = find_orphaned_caches()
print(f"Found {orphans['orphaned_count']} orphaned caches")
print(f"Total size: {orphans['total_size_mb']} MB")
```
---
## Troubleshooting
### Issue: Cache not invalidating on changes
**Cause**: Function source unavailable (built-in/dynamic)
**Solution**: Check logs for warnings:
```python
# Look for:
# "Cannot hash source for my_func, using file mtime for versioning"
```
If file mtime also fails, explicitly use `auto_versioning=False` and manage manually.
### Issue: Too many cache versions
**Cause**: Rapid development with many changes
**Solution**: Run cleanup more frequently:
```python
from afml.cache import cache_maintenance
cache_maintenance(
clean_orphaned=True,
min_orphan_age_hours=12 # More aggressive
)
```
### Issue: Expensive function cache lost
**Cause**: Auto-versioning invalidated cache on minor change
**Solution**: Opt-out for that specific function:
```python
@cacheable(auto_versioning=False)
def expensive_stable_function(data):
return days_of_computation(data)
```
---
## Backward Compatibility
### Old Decorator Aliases
These still work (no changes needed):
```python
from afml.cache import (
robust_cacheable, # = cacheable()
time_aware_cacheable, # = cacheable(time_aware=True)
cv_cacheable, # = cacheable()
)
# All now have auto_versioning=True by default
```
### Disabling Auto-Versioning Globally
If you want old behavior everywhere (not recommended):
```python
# In your __init__.py or main module
from afml.cache import disable_auto_versioning
# Use this instead of cacheable
cacheable = disable_auto_versioning()
# Now all @cacheable() calls have auto_versioning=False
```
---
## Getting Help
### Check Cache Health
```python
from afml.cache import print_cache_report
print_cache_report()
```
### Debug Specific Function
```python
from afml.cache import debug_function_cache
debug_function_cache("afml.features.my_func")
```
### Analyze Version Fragmentation
```python
from afml.cache import analyze_cache_versions, print_version_analysis
analysis = analyze_cache_versions()
print_version_analysis()
```
---
## Summary
✅ **What You Need to Do:**
1. Replace `@smart_cacheable` with `@cacheable()` (required)
2. Review expensive functions and opt-out if needed (optional)
3. Set up periodic cache maintenance (recommended)
✅ **What's Better Now:**
- Automatic cache invalidation on code changes (correctness)
- No per-call overhead (performance)
- Complete invalidation for all args (reliability)
- Simpler mental model (clarity)
✅ **Default is Correct:**
- `auto_versioning=True` prevents stale cache bugs
- Only opt-out for specific expensive stable functions
- When in doubt, use the default
|