expire_set : High-performance concurrent expiration set
A high-performance, concurrent set with automatic item expiration, built on top of expire_cache using unsafe raw pointers for maximum performance.
Table of Contents
- Introduction
- Features
- Usage
- Design Philosophy
- API Documentation
- Technology Stack
- Directory Structure
- Historical Trivia
Introduction
expire_set is a specialized Rust library designed for high-throughput scenarios where items need to expire after a short duration, such as caching 404 request paths to prevent DoS attacks.
It is a wrapper around expire_cache, providing a simplified Set interface while leveraging the efficient double-buffering expiration strategy of the underlying library. The implementation uses unsafe raw pointers for optimal performance in concurrent environments.
Features
- Memory Efficient: Does not store expiration timestamps for individual items.
- Ideal for Short-Lived Cache: Perfect for use cases like "expire after 1 minute," such as 404 flooding protection or deduplication buffers.
- High Performance: Leverages
expire_cache's lock-free and double-buffering mechanisms with unsafe raw pointers. - Concurrency: Thread-safe concurrent access.
- Automatic Bulk Expiration: Background timer rotates buffers to expire old items in bulk.
Usage
Add this to your Cargo.toml:
[]
= "0.1.7"
= { = "1", = ["time", "rt"] }
Example usage:
use ExpireSet;
use Duration;
async
Design Philosophy
The core design relies on the Double Buffering mechanism provided by expire_cache:
- Underlying Storage: Uses
expire_cache::Expire<HashSet<K>>internally (powered by Papaya). - Simplified Interface: Exposes
insertandcontainsmethods tailored for Set operations. - Efficient Expiration: Inherits the bulk expiration strategy where items are cleared in generations rather than individually.
API Documentation
ExpireSet<K>
The main struct. K must implement Hash + Eq + Send + Sync + 'static.
fn new(expire: u64) -> Self
Creates a new ExpireSet.
expire: The duration in seconds before the buffer rotates. Items live for roughlyexpireto2 * expireseconds.
fn insert(&self, key: K)
Inserts a key into the set.
fn contains<Q>(&self, key: &Q) -> bool
Checks if the key exists in the set.
Q: The query type that can be borrowed asK- Returns:
trueif the key exists,falseotherwise
Technology Stack
- Rust: Core language.
- expire_cache: Underlying expiration logic.
- Tokio: Async runtime.
- Papaya: Fast and ergonomic concurrent hash-table (via
expire_cache).
Directory Structure
.
├── Cargo.toml # Project configuration
├── readme/ # Documentation
│ ├── en.md # English README
│ └── zh.md # Chinese README
├── src/
│ └── lib.rs # Source code (ExpireSet implementation)
└── tests/
│ └── main.rs # Integration tests
Historical Trivia
The Origin of Double Buffering
The "rotating cache" technique used in this project is analogous to Double Buffering in computer graphics.
Double buffering originated in the late 1960s and became standard in the 1980s with systems like the Amiga. In graphics, it involves drawing to a hidden "back buffer" while displaying the "front buffer," then swapping them instantly to prevent screen tearing.
Similarly, expire_set (via expire_cache) writes to a "current" buffer while keeping the "previous" buffer available for reads. When the timer fires, it "swaps" the buffers and clears the old one, ensuring a smooth transition and efficient bulk expiration.
About
This library is developed by WebC.site.
WebC.site: A new paradigm of web development for AI
expire_set : 基于非安全原始指针的高性能并发过期集合
基于 expire_cache 构建的高性能并发集合,使用非安全原始指针实现最大性能,支持自动过期。
目录
简介
expire_set 是一个专为高吞吐量场景设计的 Rust 库,适用于需要短时间过期的项目,例如缓存 404 请求路径以防止 DoS 攻击。
它是 expire_cache 的封装,提供了简化的 Set 接口,同时利用了底层库高效的双缓冲过期策略。实现使用非安全原始指针在并发环境中实现最佳性能。
特性
- 极致省内存:不为单个项目保存过期时间戳。
- 短时缓存利器:非常适合"一分钟后过期"这类场景,如 404 洪水攻击防护或去重缓冲区。
- 高性能:利用
expire_cache的无锁和双缓冲机制,配合非安全原始指针实现最优性能。 - 高并发:线程安全并发访问。
- 批量自动过期:后台定时器轮转缓冲区,批量过期旧项目。
使用演示
在 Cargo.toml 中添加:
[]
= "0.1.7"
= { = "1", = ["time", "rt"] }
代码示例:
use ExpireSet;
use Duration;
async
设计思路
核心设计依赖于 expire_cache 提供的 双缓冲 机制:
- 底层存储:内部使用
expire_cache::Expire<HashSet<K>>(基于 Papaya)。 - 简化接口:暴露专为 Set 操作定制的
insert和contains方法。 - 高效过期:继承了批量过期策略,按代清除数据而非逐个清除。
API 文档
ExpireSet<K>
主结构体。K 必须实现 Hash + Eq + Send + Sync + 'static。
fn new(expire: u64) -> Self
创建新 ExpireSet。
expire: 缓冲区轮转间隔(秒)。项目存活时间约为expire到2 * expire秒。
fn insert(&self, key: K)
将键插入集合。
fn contains<Q>(&self, key: &Q) -> bool
检查键是否存在于集合中。
Q: 可借用的查询类型- 返回值: 如果键存在返回
true,否则返回false
技术堆栈
- Rust: 核心语言。
- expire_cache: 底层过期逻辑。
- Tokio: 异步运行时。
- Papaya: 高性能并发哈希表(通过
expire_cache)。
目录结构
.
├── Cargo.toml # 项目配置
├── readme/ # 文档目录
│ ├── en.md # 英文说明
│ └── zh.md # 中文说明
├── src/
│ └── lib.rs # 源代码 (ExpireSet 实现)
└── tests/
└── main.rs # 集成测试
历史小故事
双缓冲技术的起源
本项目使用的"轮转缓存"技术类比于计算机图形学中的 双缓冲(Double Buffering)。
双缓冲起源于 20 世纪 60 年代末,并在 80 年代随着 Amiga 等系统的出现成为标准。在图形学中,它涉及在显示"前缓冲区"的同时向隐藏的"后缓冲区"绘图,然后瞬间交换两者以防止画面撕裂。
类似地,expire_set(通过 expire_cache)写入"当前"缓冲区,同时保留"上一个"缓冲区供读取。当定时器触发时,它"交换"缓冲区并清空旧缓冲区,确保平滑过渡和高效批量过期。
关于
本库由 WebC.site 开发。
WebC.site : 面向人工智能的网站开发新范式