示例工程 12-decorators-functools

12-decorators-functools/03-lru-cache-stats/README.md

markdown · 0.7 KB

# 03 · lru-cache-stats
 
**演示**:`lru_cache(maxsize=2)` 的命中/未命中统计、LRU 淘汰与 `cache_clear`;参数必须可哈希,列表参数在进入函数前就抛 `TypeError`。
**运行**:
 
```sh
uv run python examples/12-decorators-functools/03-lru-cache-stats/lru_cache_stats.py
```
 
**预期输出**(3.12.9):
 
```text
hits: 1, misses: 2, currsize: 2
computed: ['a', 'b']
after evict: hits: 1, misses: 4, currsize: 2
cleared: hits: 0, misses: 0
unhashable: unhashable type: 'list'
```
 
**依赖**:仅标准库。
**注意**:`cache_info()` 的 `hits`/`misses` 是累计值;`cache_clear()` 会同时清零统计。缓存键由位置参数和关键字参数共同组成,`f(1)` 与 `f(x=1)` 不是同一个键。