Skip to content

Commit

Permalink
lfu 设定
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Jun 26, 2024
1 parent 18cc5d3 commit a97c3db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
## algorithm
## algorithm/ 算法结构相关
[![crates.io](https://img.shields.io/crates/v/algorithm.svg)](https://crates.io/crates/algorithm)
[![rustc 1.70.0](https://img.shields.io/badge/rust-1.70%2B-orange.svg)](https://img.shields.io/badge/rust-1.70%2B-orange.svg)
[![Released API docs](https://docs.rs/algorithm/badge.svg)](https://docs.rs/algorithm)

将提供一些常用的数据结构以供使用。目前提供的数据结构
* **LruCache** 最近未使用缓存
* **LruKCache** 最近未使用缓存, K次分类列表
* **LfuCache** 按缓存访问次数做排序,优先淘汰访问最少次数的
* **LruCache** 最近未使用缓存,可用feature启用ttl
* **LruKCache** 最近未使用缓存, K次分类列表,可用feature启用ttl
* **LfuCache** 按缓存访问次数做排序,优先淘汰访问最少次数的,可用feature启用ttl
* **ArcCache** Adaptive Replacement Cache,自适应缓存替换算法,可用feature启用ttl
* **Slab** 仿linux中的Slab结构,对大对象做到初始化缓存使用
* **BitMap** 位图, 按位做标记的图
* **RoaringBitMap** 位图, 因为位图占用的内存太大, 对于稀疏位图会更小内存
* **TimerWheel** 计时器轮, 模仿时钟的高效定时器组件
* **CircularBuffer** 环形Buffer组件, 适用于内存限定较严格的, 设置不超过缓存值的环形结构
* **RBTree** 红黑村, 高效的排序树, 可用于做定时器组件
* **FixedVec** 模拟指针的可变长数组

# lru 全称是Least Recently Used,即最近最久未使用的意思。
每次元素访问将其更新到列表的最前,时间复杂度为O(1)。当达到容量限制时将淘汰双向列表中的链尾数据
Expand Down Expand Up @@ -201,4 +207,9 @@ fn main() {

assert!(timer.is_empty());
}
```
```


## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=tickbh/algorithm-rs&type=Date)](https://star-history.com/#tickbh/algorithm-rs&Date)
34 changes: 20 additions & 14 deletions src/cache/lfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ use crate::get_milltimestamp;
#[cfg(feature = "ttl")]
const DEFAULT_CHECK_STEP: u64 = 120;

/// Lfu节点数据
pub(crate) struct LfuEntry<K, V> {
pub key: mem::MaybeUninit<K>,
pub val: mem::MaybeUninit<V>,
/// 访问总频次
pub counter: usize,
/// 带ttl的过期时间,单位秒
/// 如果为u64::MAX,则表示不过期
Expand Down Expand Up @@ -156,11 +158,15 @@ pub struct LfuCache<K, V, S> {
/// 因为HashSet的pop耗时太长, 所以取LfuCache暂时做为平替
times_map: HashMap<u8, LruCache<KeyRef<K>, (), DefaultHasher>>,
cap: usize,
/// 最大的访问频次
max_freq: u8,
/// 最小的访问频次
min_freq: u8,
/// 总的访问次数
visit_count: usize,

/// 初始的访问次数
default_count: usize,
/// 每多少次访问进行一次衰减
reduce_count: usize,

/// 下一次检查的时间点,如果大于该时间点则全部检查是否过期
Expand Down Expand Up @@ -552,7 +558,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
if val.is_empty() {
continue;
}
let key = Self::_pop_one(val).unwrap();
let key = val.pop_unusual().unwrap().0;
let value = self.map.remove(&key).expect("must ok");
let node = *Box::from_raw(value.as_ptr());
let LfuEntry { key, val, .. } = node;
Expand Down Expand Up @@ -587,7 +593,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
if val.is_empty() {
continue;
}
let key = Self::_pop_one(val).unwrap();
let key = val.pop_unusual().unwrap().0;
let value = self.map.remove(&key).expect("must ok");
let node = *Box::from_raw(value.as_ptr());
let LfuEntry { key, val, .. } = node;
Expand Down Expand Up @@ -1061,7 +1067,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
if val.is_empty() {
continue;
}
let key = Self::_pop_one(val).unwrap();
let key = val.pop_unusual().unwrap().0;
let old_node = self.map.remove(&key).unwrap();
let node_ptr: *mut LfuEntry<K, V> = old_node.as_ptr();

Expand Down Expand Up @@ -1123,16 +1129,16 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
}
}

fn _pop_one(keys: &mut LruCache<KeyRef<K>, (), DefaultHasher>) -> Option<KeyRef<K>> {
keys.pop_usual().map(|(k, _)| k)
// let k = if let Some(k) = keys.iter().next() {
// KeyRef { k: k.k }
// } else {
// return None;
// };
// keys.remove(&k);
// Some(k)
}
// fn _pop_one(keys: &mut LruCache<KeyRef<K>, (), DefaultHasher>) -> Option<KeyRef<K>> {
// keys.pop_usual().map(|(k, _)| k)
// // let k = if let Some(k) = keys.iter().next() {
// // KeyRef { k: k.k }
// // } else {
// // return None;
// // };
// // keys.remove(&k);
// // Some(k)
// }
}


Expand Down

0 comments on commit a97c3db

Please sign in to comment.