GeezerVison is showing docs.rs — ArcCache in algorithm - Rust pub struct ArcCache<K, V, S> { /* private fields */ }
Expand descriptionARC(Adaptive Replacement Cache): 自适应缓存替换算法,它结合了LRU与LFU,来获得可用缓存的最佳使用。
设置容量之后将最大保持该容量大小的数据
后进的数据将会淘汰最久没有被访问的数据
§Examples
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("now", "ok");
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
arc.insert("auth", "tickbh");
assert!(arc.len() == 4);
assert_eq!(arc.get("hello"), Some(&"algorithm"));
assert_eq!(arc.get("this"), Some(&"arc"));
assert_eq!(arc.get("now"), Some(&"ok"));
}
Implementations§
设置当前检查lru的间隔
单位为秒,意思就是每隔多少秒会清理一次数据
如果数据太大的话遍历一次可能会比较久的时长
一次清理时间复杂度O(n)
仅仅在插入时触发检查,获取时仅检查当前元素
清理当前数据
§Examples
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("now", "ok");
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.len() == 3);
arc.clear();
assert!(arc.len() == 0);
}
遍历当前的所有值
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
for (k, v) in arc.iter() {
assert!(k == &"hello" || k == &"this");
assert!(v == &"algorithm" || v == &"arc");
}
assert!(arc.len() == 2);
}
遍历当前的所有值, 可变
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm".to_string());
arc.insert("this", "arc".to_string());
for (k, v) in arc.iter_mut() {
v.push_str(" ok");
}
assert!(arc.len() == 2);
assert!(arc.get(&"this") == Some(&"arc ok".to_string()));
assert!(arc.get(&"hello") == Some(&"algorithm ok".to_string()));
}
遍历当前的key值
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
let mut keys = arc.keys();
assert!(keys.next()==Some(&"this"));
assert!(keys.next()==Some(&"hello"));
assert!(keys.next() == None);
}
遍历当前的valus值
use algorithm::ArcCache;
fn main() {
let vec = vec![(1, 1), (2, 2), (3, 3)];
let mut map: ArcCache<_, _, _> = vec.into_iter().collect();
for value in map.values_mut() {
*value = (*value) * 2
}
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values.len(), 3);
assert!(values.contains(&2));
assert!(values.contains(&4));
assert!(values.contains(&6));
}
遍历当前的valus值
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm".to_string());
arc.insert("this", "arc".to_string());
{
let mut values = arc.values_mut();
values.next().unwrap().push_str(" ok");
values.next().unwrap().push_str(" ok");
assert!(values.next() == None);
}
assert_eq!(arc.get(&"this"), Some(&"arc ok".to_string()))
}
弹出栈顶上的数据, 最常使用的数据
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.pop_usual()==Some(("this", "arc")));
assert!(arc.len() == 1);
}
弹出栈尾上的数据, 最久未使用的数据
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.pop_unusual()==Some(("hello", "algorithm")));
assert!(arc.len() == 1);
}
取出栈顶上的数据, 最近使用的数据
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.peek_usual()==Some((&"this", &"arc")));
assert!(arc.len() == 2);
}
取出栈尾上的数据, 最久未使用的数据
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.peek_last()==Some((&"hello", &"algorithm")));
assert!(arc.len() == 2);
}
获取key值相对应的value值, 根据hash判定
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.raw_get(&"this") == Some(&"arc"));
}
获取key值相对应的value值, 根据hash判定
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.get(&"this") == Some(&"arc"));
}
获取key值相对应的key和value值, 根据hash判定
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.get_key_value(&"this") == Some((&"this", &"arc")));
}
获取key值相对应的value值, 根据hash判定, 可编辑被改变
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm".to_string());
arc.insert("this", "arc".to_string());
arc.get_mut(&"this").unwrap().insert_str(3, " good");
assert!(arc.get_key_value(&"this") == Some((&"this", &"arc good".to_string())));
}
插入值, 如果值重复将返回原来的数据
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.insert("this", "arc good") == Some(&"arc"));
}
插入带有生存时间的元素
每次获取像redis一样,并不会更新生存时间
如果需要更新则需要手动的进行重新设置
移除元素
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
assert!(arc.remove("this") == Some(("this", "arc")));
assert!(arc.len() == 1);
}
根据保留当前的元素, 返回false则表示抛弃元素
use algorithm::ArcCache;
fn main() {
let mut arc = ArcCache::new(3);
arc.insert("hello", "algorithm");
arc.insert("this", "arc");
arc.insert("year", "2024");
arc.retain(|_, v| *v == "2024" || *v == "arc");
assert!(arc.len() == 2);
assert!(arc.get("this") == Some(&"arc"));
}
Trait Implementations§
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more
🔬This is a nightly-only experimental API. (pin_ergonomics) Execute the destructor for this type, but different to Drop::drop, it requires self
to be pinned. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one) Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one) Reserves capacity in a collection for the given number of additional elements. Read more
The returned type after indexing.
Performs the indexing ( container[index]) operation. Read more
Performs the mutable indexing ( container[index]) operation. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Auto Trait Implementations§Blanket Implementations§
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬This is a nightly-only experimental API. (clone_to_uninit) Performs copy-assignment from self to dest. Read more
Compare self to key and return true if they are equal.
Returns the argument unchanged.
Calls U::from(self).
That is, this conversion is whatever the implementation of
From<T> for U chooses to do.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion. |