expire_cache: High-performance generational cache
expire_cache implements an efficient expiration cache using generational collection strategy. Instead of tracking individual item expiration times, it maintains two data buckets (generations), significantly reducing memory overhead and CPU usage for expiration checks.
Features
- High Performance: O(1) amortized expiration overhead per item
- Concurrent Access: Built on papaya for lock-free thread-safe operations
- Async Support: Native async initialization with
get_or_init_async! - Flexible Storage: Support for both key-value maps and sets
- Simple API: Clean interface with
get,insert, and initialization methods
Installation
[]
= { = "0.1.22", = ["hashmap", "get_or_init_async"] }
Available features:
hashmap: Enable HashMap support (papaya::HashMap)hashset: Enable HashSet support (papaya::HashSet)get_or_init: Enable synchronous initializationget_or_init_async: Enable asynchronous initialization macro
Quick Start
Basic Usage
use Expire;
use HashMap;
use Duration;
async
Async Initialization
use ;
use HashMap;
async
Sync Initialization
use ;
use HashMap;
Set Usage
use Expire;
use HashSet;
async
API Reference
Expire<T: Map>
new(expire: u64) -> Self: Create cache with expiration period in secondsget(&self, key) -> Option<RefVal>: Retrieve value from cacheinsert(&self, key, val): Insert value into cacheget_or_init(&self, key, func) -> Result<RefVal, E>: Sync initialization (requiresget_or_initfeature)
get_or_init_async! macro
get_or_init_async!(cache, key, init_fn) -> Result<Val, E>: Async initialization macro, callsinit_fn()only on cache miss
Why a macro instead of a callback function?
Rust's impl Trait return types create distinct opaque types for each call site. When a closure captures references and returns an impl Future, the compiler cannot unify the Future's lifetime with the closure parameter's lifetime in generic contexts. This causes "lifetime mismatch" errors.
The macro approach bypasses this by inlining the code at the call site, so the async expression is awaited directly without going through a generic callback, naturally avoiding the lifetime inference issues.
This is a known Rust limitation: rust-lang/rust#100013
Design
Generational Collection
The cache uses a double-buffer approach with two generations:
- Insertion: New entries always go to the active generation
- Lookup: Check active generation first, then passive generation
- Expiration: Background task periodically clears passive generation and swaps roles
- Lifecycle: Items live between
expireand2 * expireseconds
This approach trades absolute precision for significant throughput improvements and reduced memory fragmentation.
License
MulanPSL-2.0
About
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
expire_cache: 高性能分代缓存
expire_cache 实现了基于分代收集策略的高效过期缓存。它不追踪单个条目的过期时间,而是维护两个数据桶(代),显著降低过期检查的内存开销和 CPU 使用率。
特性
- 高性能:每条目摊销 O(1) 过期开销
- 并发访问:基于 papaya 的无锁线程安全操作
- 异步支持:原生异步初始化宏
get_or_init_async! - 灵活存储:支持键值映射和集合
- 简洁 API:清晰的
get、insert和初始化接口
安装
[]
= { = "0.1.22", = ["hashmap", "get_or_init_async"] }
可用特性:
hashmap:启用 HashMap 支持 (papaya::HashMap)hashset:启用 HashSet 支持 (papaya::HashSet)get_or_init:启用同步初始化get_or_init_async:启用异步初始化宏
快速开始
基本用法
use Expire;
use HashMap;
use Duration;
async
异步初始化
use ;
use HashMap;
async
同步初始化
use ;
use HashMap;
集合用法
use Expire;
use HashSet;
async
API 参考
Expire<T: Map>
new(expire: u64) -> Self:创建带过期周期的缓存(秒)get(&self, key) -> Option<RefVal>:从缓存检索值insert(&self, key, val):向缓存插入值get_or_init(&self, key, func) -> Result<RefVal, E>:同步初始化(需要get_or_initfeature)
get_or_init_async! 宏
get_or_init_async!(cache, key, init_fn) -> Result<Val, E>:异步初始化宏,仅在缓存未命中时调用init_fn()
为什么用宏而不是回调函数?
Rust 的 impl Trait 返回类型在每个调用点会生成不同的 opaque type。当闭包捕获引用并返回 impl Future 时,编译器无法在泛型上下文中统一 Future 的生命周期与闭包参数的生命周期,导致"生命周期不匹配"错误。
宏通过在调用点内联代码来绕过这个问题,异步表达式直接被 await,无需经过泛型回调,自然避免了生命周期推断问题。
这是 Rust 的已知限制:rust-lang/rust#100013
设计
分代收集策略
缓存使用双缓冲方法,包含两个代:
- 插入:新条目总是进入活跃代
- 查找:先检查活跃代,再检查被动代
- 过期:后台任务定期清空被动代并交换角色
- 生命周期:条目存活时间在
expire到2 * expire秒之间
这种方法牺牲了绝对精度,换取了显著的吞吐量提升和内存碎片减少。
许可证
MulanPSL-2.0
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: