Skip to main content

Module l1_cache

Module l1_cache 

Source
Expand description

L1 一级缓存(Level-1 Cache)— Session 级别 Identity Map

对应 tasks.md M2-T6~T8,design.md §5.1.2 M2-T11~T17。

§核心概念

  • L1Cache:Session 级别一级缓存(Identity Map),同主键查询返回同引用
  • Identity Map:相同主键的多次查询返回相同 Arc<T> 引用(Arc::ptr_eq 为 true)
  • LRU 淘汰:容量上限 + 最久未使用条目淘汰
  • AtomicU64 统计:无锁命中/未命中/淘汰计数
  • Session 绑定:生命周期与 Session 绑定,Drop 时自动清空,不跨 Session 共享

与 L2 缓存(crate::l2_cache::L2Cache)的区别:

  • L1:单次 Session 内有效,Identity Map 语义,Drop 自动清空
  • L2:跨 Session 共享,进程级缓存,需显式失效

§L1→L2→DB 查询协作

  1. L1 命中 → 直接返回
  2. L1 未命中 → 查 L2 → L2 命中 → 回填 L1 → 返回
  3. L2 未命中 → 查 DB → 回填 L1 + L2 → 返回

§使用示例

use sz_orm_core::l1_cache::L1Cache;
use std::sync::Arc;

let cache: L1Cache<String> = L1Cache::new(100);
cache.put(1, Arc::new("Alice".to_string()));
let a = cache.get(&1).unwrap();
let b = cache.get(&1).unwrap();
assert!(Arc::ptr_eq(&a, &b)); // Identity Map 语义

Structs§

L1Cache
L1 一级缓存(Session 级别 Identity Map)
L1CacheStats
L1 缓存统计快照
L1L2Coordinator
L1→L2→DB 三级查询协作器