Struct algorithm::LfuCache

source ·
pub struct LfuCache<K, V, S> { /* private fields */ }
Expand description

一个 lfu(least frequently used/最不经常使用页置换算法 ) 缓存的实现, 接口参照Hashmap保持一致 根据元素的访问次数进行按分组进行淘汰测试 在访问次数达到设定值时将全体所有的访问次数下降1处理 以使高频数据在一定时间后将过期处理

§Examples

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    let _ = lru.get("hello");
    let _ = lru.get("this");
    lru.insert("now", "ok");
    lru.insert("auth", "tickbh");
    assert!(lru.len() == 3);
    assert_eq!(lru.get("hello"), Some(&"algorithm"));
    assert_eq!(lru.get("this"), Some(&"lru"));
    assert_eq!(lru.get("now"), None);
}

Implementations§

source§

impl<K: Hash + Eq, V> LfuCache<K, V, RandomState>

source

pub fn new(cap: usize) -> Self

source§

impl<K, V, S> LfuCache<K, V, S>

source

pub fn with_hasher(cap: usize, hash_builder: S) -> LfuCache<K, V, S>

提供hash函数

source

pub fn set_default_count(&mut self, default_count: usize)

设定初始进入列表中默认的访问次数,防止出现一进入就权重过低的情况

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.get_visit(&"this") == Some(5));
    assert!(lru.get_visit(&"hello") == Some(5));
}
source

pub fn get_default_count(&self) -> usize

source

pub fn set_reduce_count(&mut self, reduce_count: usize)

每多少访问存储中触发值, 如设置100次,那么将100次发生get或者put时将触发一次调整 每次调整时间复杂度为O(n)

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    lru.set_reduce_count(100);
    lru.set_reduce_step(100);
    assert!(lru.get_visit(&"hello") == Some(5));
    assert!(lru.get_visit(&"this") == Some(5));
    for _ in 0..100 {
        let _ = lru.get("this");
    }
    assert!(lru.get_visit(&"this") == Some(5));
    assert!(lru.get_visit(&"hello") == Some(0));
    let mut keys = lru.keys();
    assert!(keys.next()==Some(&"this"));
    assert!(keys.next()==Some(&"hello"));
    assert!(keys.next() == None);
}
source

pub fn get_reduce_count(&self) -> usize

source

pub fn set_reduce_step(&mut self, reduce_step: usize)

每次触发访问减少的值,如设置100,那所有元素中的访问次数将减少100 用于长时间不访问的数据能在一定时间后过期

source

pub fn get_reduce_step(&self) -> usize

source

pub fn capacity(&self) -> usize

获取当前容量

source

pub fn clear(&mut self)

清理当前数据

§Examples
use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("now", "ok");
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.len() == 3);
    lru.clear();
    assert!(lru.len() == 0);
}
source

pub fn len(&self) -> usize

获取当前长度

source

pub fn reserve(&mut self, additional: usize)

扩展当前容量

source

pub fn iter(&self) -> Iter<'_, K, V, S>

遍历当前的所有值

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    for (k, v) in lru.iter() {
        assert!(k == &"hello" || k == &"this");
        assert!(v == &"algorithm" || v == &"lru");
    }
    assert!(lru.len() == 2);
}
source

pub fn keys(&self) -> Keys<'_, K, V, S>

遍历当前的key值

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    let _ = lru.get("this");
    let mut keys = lru.keys();
    assert!(keys.next()==Some(&"this"));
    assert!(keys.next()==Some(&"hello"));
    assert!(keys.next() == None);
}
source

pub fn values(&self) -> Values<'_, K, V, S>

遍历当前的valus值

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    let _ = lru.get("this");
    let mut values = lru.values();
    assert!(values.next()==Some(&"lru"));
    assert!(values.next()==Some(&"algorithm"));
    assert!(values.next() == None);
}
source

pub fn hasher(&self) -> &S

source§

impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S>

source

pub fn drain(&mut self) -> Drain<'_, K, V, S>

排出当前数据

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    {
        let mut drain = lru.drain();
        assert!(drain.next()==Some(("hello", "algorithm")));
    }
    assert!(lru.len() == 1);
}
source

pub fn pop(&mut self) -> Option<(K, V)>

弹出栈顶上的数据, 最近使用的数据

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    let _ = lru.get("this");
    assert!(lru.pop()==Some(("this", "lru")));
    assert!(lru.len() == 1);
}
source

pub fn pop_last(&mut self) -> Option<(K, V)>

弹出栈尾上的数据, 最久未使用的数据

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    let _ = lru.get("this");
    assert!(lru.pop_last()==Some(("hello", "algorithm")));
    assert!(lru.len() == 1);
}
source

pub fn get_visit<Q>(&mut self, k: &Q) -> Option<usize>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取key值相对应的value值, 根本hash判定

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.get_visit(&"this") == Some(5));
}
source

pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取key值相对应的value值, 根本hash判定

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.get(&"this") == Some(&"lru"));
}
source

pub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取key值相对应的key和value值, 根本hash判定

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.get_key_value(&"this") == Some((&"this", &"lru")));
}
source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取key值相对应的value值, 根本hash判定, 可编辑被改变

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm".to_string());
    lru.insert("this", "lru".to_string());
    lru.get_mut(&"this").unwrap().insert_str(3, " good");
    assert!(lru.get_key_value(&"this") == Some((&"this", &"lru good".to_string())));
}
source

pub fn insert(&mut self, k: K, v: V) -> Option<V>

插入值, 如果值重复将返回原来的数据

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.insert("this", "lru good") == Some(&"lru"));
}
source

pub fn capture_insert(&mut self, k: K, v: V) -> Option<(K, V)>

source

pub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

移除元素

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    assert!(lru.remove("this") == Some(("this", "lru")));
    assert!(lru.len() == 1);
}
source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &mut V) -> bool,

根据保留当前的元素, 返回false则表示抛弃元素

use algorithm::LfuCache;
fn main() {
    let mut lru = LfuCache::new(3);
    lru.insert("hello", "algorithm");
    lru.insert("this", "lru");
    lru.insert("year", "2024");
    lru.retain(|_, v| *v == "2024" || *v == "lru");
    assert!(lru.len() == 2);
    assert!(lru.get("this") == Some(&"lru"));
}

Trait Implementations§

source§

impl<K: Clone + Hash + Eq, V: Clone, S: Clone + BuildHasher> Clone for LfuCache<K, V, S>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K, V, S> Drop for LfuCache<K, V, S>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for LfuCache<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for LfuCache<K, V, S>

§

impl<K, V, S> !Send for LfuCache<K, V, S>

§

impl<K, V, S> !Sync for LfuCache<K, V, S>

§

impl<K, V, S> Unpin for LfuCache<K, V, S>
where S: Unpin,

§

impl<K, V, S> UnwindSafe for LfuCache<K, V, S>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.