| Crates.io | jdb_lru |
| lib.rs | jdb_lru |
| version | 0.1.8 |
| created_at | 2025-12-28 12:22:44.143224+00 |
| updated_at | 2026-01-09 04:26:33.4167+00 |
| description | Efficient LRU cache with zero-allocation alternatives / 高效LRU缓存与零分配替代方案 |
| homepage | https://github.com/js0-site/rust/tree/main/jdb_lru |
| repository | https://github.com/js0-site/rust.git |
| max_upload_size | |
| id | 2008683 |
| size | 67,111 |
jdb_lru is lightweight Rust library providing LRU (Least Recently Used) cache implementations with zero-allocation alternatives. The library offers unified Cache trait interface supporting both functional LRU caching and no-op cache implementations for garbage collection friendly scenarios.
hashlink crateuse jdb_lru::{Cache, Lru};
let mut cache = Lru::new(3);
cache.set("key1".to_string(), "value1");
cache.set("key2".to_string(), "value2");
assert_eq!(cache.get(&"key1".to_string()), Some(&"value1"));
let mut cache = Lru::new(2);
cache.set(1, "a");
cache.set(2, "b");
// Access key 1 to make it recently used
assert_eq!(cache.get(&1), Some(&"a"));
// Add third item, evicts least recently used (key 2)
cache.set(3, "c");
assert_eq!(cache.get(&2), None); // evicted
use jdb_lru::{Cache, NoCache};
let mut cache: NoCache = NoCache;
cache.set("key", "value");
assert_eq!(<NoCache as Cache<String, String>>::get(&mut cache, &"key".to_string()), None);
The Cache<K, V> trait defines the cache interface:
get(&mut self, key: &K) -> Option<&V>: Retrieve value by keyset(&mut self, key: K, val: V): Insert key-value pairrm(&mut self, key: &K): Remove entry by keyAll implementations guarantee O(1) complexity for these operations.
new(cap: usize) -> Self: Create LRU cache with specified capacityhashlink::LruCache for O(1) operationsK: Hash + Eq and Vget() operation always returns Nonegraph TD
A[Cache Trait] --> B[Lru Implementation]
A --> C[NoCache Implementation]
B --> D[hashlink::LruCache]
C --> E[Zero Operations]
F[User Code] --> A
G[get/set/rm] --> A
Cache<K, V> trait defines unified interfacehashlink::LruCache for O(1) operationsHash + Eq, values have no constraintsThe design prioritizes performance and flexibility, allowing runtime selection between functional caching and zero-overhead no-op implementations.
| Cache | get | set | rm | Space |
|---|---|---|---|---|
| Lru | O(1) | O(1) | O(1) | O(n) |
| NoCache | O(1) | O(1) | O(1) | O(1) |
jdb_lru/
├── src/
│ ├── lib.rs # Core library with Cache trait and implementations
│ ├── cache.rs # Cache trait definition
│ ├── lru.rs # LRU cache implementation
│ └── no.rs # NoCache implementation
├── tests/
│ └── main.rs # Comprehensive test suite
├── readme/
│ ├── en.md # English documentation
│ └── zh.md # Chinese documentation
├── Cargo.toml # Project configuration
└── README.mdt # Documentation index
The LRU (Least Recently Used) cache algorithm has its roots in operating system memory management, first described in the 1960s as part of page replacement strategies. The concept gained prominence with the development of virtual memory systems, where efficient eviction policies became crucial for performance.
In modern software systems, LRU caches are ubiquitous - from CPU caches and database buffer pools to web browser caches and content delivery networks. The hashlink crate that powers this implementation builds upon decades of research into cache-oblivious algorithms and lock-free data structures.
Rust's ownership system and zero-cost abstractions make it particularly well-suited for implementing cache systems, providing memory safety without runtime overhead. This library continues that tradition by offering both high-performance caching and zero-allocation alternatives in single, unified interface.
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:
jdb_lru是轻量级Rust库,提供LRU(最近最少使用)缓存实现和零分配替代方案。库提供统一的Cache trait接口,支持功能性LRU缓存和垃圾回收友好的无操作缓存实现。
hashlink库use jdb_lru::{Cache, Lru};
let mut cache = Lru::new(3);
cache.set("key1".to_string(), "value1");
cache.set("key2".to_string(), "value2");
assert_eq!(cache.get(&"key1".to_string()), Some(&"value1"));
let mut cache = Lru::new(2);
cache.set(1, "a");
cache.set(2, "b");
// 访问key 1使其成为最近使用
assert_eq!(cache.get(&1), Some(&"a"));
// 添加第三项,淘汰最近最少使用的(key 2)
cache.set(3, "c");
assert_eq!(cache.get(&2), None); // 已淘汰
use jdb_lru::{Cache, NoCache};
let mut cache: NoCache = NoCache;
cache.set("key", "value");
assert_eq!(<NoCache as Cache<String, String>>::get(&mut cache, &"key".to_string()), None);
Cache<K, V> trait定义缓存接口:
get(&mut self, key: &K) -> Option<&V>:按键检索值set(&mut self, key: K, val: V):插入键值对rm(&mut self, key: &K):按键删除条目所有实现都保证这些操作的O(1)复杂度。
new(cap: usize) -> Self:创建指定容量的LRU缓存hashlink::LruCache实现O(1)操作K: Hash + Eq和Vget()操作始终返回Nonegraph TD
A[Cache Trait] --> B[Lru实现]
A --> C[NoCache实现]
B --> D[hashlink::LruCache]
C --> E[零操作]
F[用户代码] --> A
G[get/set/rm] --> A
Cache<K, V> trait定义统一接口hashlink::LruCache实现O(1)操作Hash + Eq,值无约束设计优先考虑性能和灵活性,允许在功能缓存和零开销无操作实现之间进行运行时选择。
| Cache | get | set | rm | Space |
|---|---|---|---|---|
| Lru | O(1) | O(1) | O(1) | O(n) |
| NoCache | O(1) | O(1) | O(1) | O(1) |
jdb_lru/
├── src/
│ ├── lib.rs # 核心库,包含Cache trait和实现
│ ├── cache.rs # Cache trait定义
│ ├── lru.rs # LRU缓存实现
│ └── no.rs # NoCache实现
├── tests/
│ └── main.rs # 综合测试套件
├── readme/
│ ├── en.md # 英文文档
│ └── zh.md # 中文文档
├── Cargo.toml # 项目配置
└── README.mdt # 文档索引
LRU(最近最少使用)缓存算法起源于操作系统内存管理,最早在1960年代作为页面替换策略的一部分被描述。这个概念随着虚拟内存系统的发展而获得prominence,其中高效的淘汰策略对性能变得至关重要。
在现代软件系统中,LRU缓存无处不在 - 从CPU缓存和数据库缓冲池到网页浏览器缓存和内容分发网络。驱动此实现的hashlink库建立在数十年的缓存无关算法和无锁数据结构研究基础之上。
Rust的所有权系统和零成本抽象使其特别适合实现缓存系统,在不产生运行时开销的情况下提供内存安全。本库延续了这一传统,在单一统一接口中同时提供高性能缓存和零分配替代方案。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: