pub struct ArcCache<K, V, S> { /* private fields */ }
Expand description
ARC(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§
Source§impl<K, V, S: Clone> ArcCache<K, V, S>
impl<K, V, S: Clone> ArcCache<K, V, S>
Sourcepub fn with_hasher(cap: usize, hash_builder: S) -> ArcCache<K, V, S>
pub fn with_hasher(cap: usize, hash_builder: S) -> ArcCache<K, V, S>
提供hash函数
Source§impl<K, V, S> ArcCache<K, V, S>
impl<K, V, S> ArcCache<K, V, S>
Sourcepub fn get_check_step(&self) -> u64
pub fn get_check_step(&self) -> u64
获取当前检查lru的间隔
Sourcepub fn set_check_step(&mut self, check_step: u64)
pub fn set_check_step(&mut self, check_step: u64)
设置当前检查lru的间隔 单位为秒,意思就是每隔多少秒会清理一次数据 如果数据太大的话遍历一次可能会比较久的时长 一次清理时间复杂度O(n) 仅仅在插入时触发检查,获取时仅检查当前元素
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
清理当前数据
§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);
}
pub fn is_empty(&self) -> bool
Sourcepub fn iter(&self) -> Iter<'_, K, V, S>
pub fn iter(&self) -> Iter<'_, K, V, S>
遍历当前的所有值
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);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, S>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, S>
遍历当前的所有值, 可变
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()));
}
Sourcepub fn keys(&self) -> Keys<'_, K, V, S>
pub fn keys(&self) -> Keys<'_, K, V, S>
遍历当前的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);
}
Sourcepub fn values(&self) -> Values<'_, K, V, S>
pub fn values(&self) -> Values<'_, K, V, S>
遍历当前的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));
}
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, S>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, S>
遍历当前的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()))
}
pub fn hasher(&self) -> &S
Source§impl<K: Hash + Eq, V, S: BuildHasher> ArcCache<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> ArcCache<K, V, S>
Sourcepub fn pop_usual(&mut self) -> Option<(K, V)>
pub fn pop_usual(&mut self) -> Option<(K, V)>
弹出栈顶上的数据, 最常使用的数据
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);
}
Sourcepub fn pop_unusual(&mut self) -> Option<(K, V)>
pub fn pop_unusual(&mut self) -> Option<(K, V)>
弹出栈尾上的数据, 最久未使用的数据
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);
}
Sourcepub fn peek_usual(&mut self) -> Option<(&K, &V)>
pub fn peek_usual(&mut self) -> Option<(&K, &V)>
取出栈顶上的数据, 最近使用的数据
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);
}
Sourcepub fn peek_last(&mut self) -> Option<(&K, &V)>
pub fn peek_last(&mut self) -> Option<(&K, &V)>
取出栈尾上的数据, 最久未使用的数据
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);
}
pub fn contains_key<Q>(&mut self, k: &Q) -> bool
Sourcepub fn raw_get<Q>(&self, k: &Q) -> Option<&V>
pub fn raw_get<Q>(&self, k: &Q) -> Option<&V>
获取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"));
}
Sourcepub fn get<Q>(&mut self, k: &Q) -> Option<&V>
pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
获取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"));
}
Sourcepub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
获取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")));
}
Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
获取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())));
}
pub fn get_mut_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &mut V)>
Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
插入值, 如果值重复将返回原来的数据
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"));
}
Sourcepub fn insert_with_ttl(&mut self, k: K, v: V, ttl: u64) -> Option<V>
pub fn insert_with_ttl(&mut self, k: K, v: V, ttl: u64) -> Option<V>
插入带有生存时间的元素 每次获取像redis一样,并不会更新生存时间 如果需要更新则需要手动的进行重新设置
pub fn capture_insert(&mut self, k: K, v: V) -> Option<(K, V, bool)>
pub fn capture_insert_with_ttl( &mut self, k: K, v: V, ttl: u64, ) -> Option<(K, V, bool)>
pub fn get_or_insert<F>(&mut self, k: K, f: F) -> &Vwhere
F: FnOnce() -> V,
pub fn get_or_insert_mut<F>(&mut self, k: K, f: F) -> &mut Vwhere
F: FnOnce() -> V,
pub fn clear_expire(&mut self)
pub fn del_ttl<Q>(&mut self, k: &Q)
pub fn set_ttl<Q>(&mut self, k: &Q, expire: u64) -> bool
pub fn get_ttl<Q>(&mut self, k: &Q) -> Option<u64>
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
移除元素
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);
}
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
根据保留当前的元素, 返回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"));
}
Source§impl<K: Hash + Eq, V: Default, S: BuildHasher> ArcCache<K, V, S>
impl<K: Hash + Eq, V: Default, S: BuildHasher> ArcCache<K, V, S>
pub fn get_or_insert_default(&mut self, k: K) -> &V
pub fn get_or_insert_default_mut(&mut self, k: K) -> &mut V
Trait Implementations§
Source§impl<K: Hash + Eq, V> Extend<(K, V)> for ArcCache<K, V, DefaultHasher>
impl<K: Hash + Eq, V> Extend<(K, V)> for ArcCache<K, V, DefaultHasher>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<K: Hash + Eq, V> FromIterator<(K, V)> for ArcCache<K, V, DefaultHasher>
impl<K: Hash + Eq, V> FromIterator<(K, V)> for ArcCache<K, V, DefaultHasher>
Source§fn from_iter<T: IntoIterator<Item = (K, V)>>(
iter: T,
) -> ArcCache<K, V, DefaultHasher>
fn from_iter<T: IntoIterator<Item = (K, V)>>( iter: T, ) -> ArcCache<K, V, DefaultHasher>
Creates a value from an iterator. Read more
Source§impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for ArcCache<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for ArcCache<K, V, S>
impl<K, V, S> Eq for ArcCache<K, V, S>
impl<K: Send, V: Send, S: Send> Send for ArcCache<K, V, S>
impl<K: Sync, V: Sync, S: Sync> Sync for ArcCache<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for ArcCache<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for ArcCache<K, V, S>
impl<K, V, S> Unpin for ArcCache<K, V, S>where
S: Unpin,
impl<K, V, S> UnwindSafe for ArcCache<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.