kapot_cache/backend/policy/lru/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
pub mod lru_cache;
use crate::backend::policy::CachePolicyPutResult;
use crate::backend::CachePolicy;
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
pub trait LruCachePolicy: CachePolicy {
/// Retrieve the value for the given key,
/// marking it as recently used and moving it to the back of the LRU list.
fn get_lru(&mut self, k: &Self::K) -> Option<Self::V>;
/// Put value for given key.
///
/// If a key already exists, its old value will be returned.
///
/// If necessary, will remove the value at the front of the LRU list to make room.
fn put_lru(
&mut self,
k: Self::K,
v: Self::V,
) -> CachePolicyPutResult<Self::K, Self::V>;
/// Remove the least recently used entry and return it.
///
/// If the `LruCache` is empty this will return None.
fn pop_lru(&mut self) -> Option<(Self::K, Self::V)>;
}
pub trait ResourceCounter: Debug + Send + 'static {
/// Resource key.
type K: Clone + Eq + Hash + Ord + Debug + Send + 'static;
/// Resource value.
type V: Clone + Debug + Send + 'static;
/// Consume resource for a given key-value pair.
fn consume(&mut self, k: &Self::K, v: &Self::V);
/// Return resource for a given key-value pair.
fn restore(&mut self, k: &Self::K, v: &Self::V);
/// Check whether the current used resource exceeds the capacity
fn exceed_capacity(&self) -> bool;
}
#[derive(Debug, Clone, Copy)]
pub struct DefaultResourceCounter<K, V>
where
K: Clone + Eq + Hash + Ord + Debug + Send + 'static,
V: Clone + Debug + Send + 'static,
{
max_num: usize,
current_num: usize,
_key_marker: PhantomData<K>,
_value_marker: PhantomData<V>,
}
impl<K, V> DefaultResourceCounter<K, V>
where
K: Clone + Eq + Hash + Ord + Debug + Send + 'static,
V: Clone + Debug + Send + 'static,
{
pub fn new(capacity: usize) -> Self {
Self {
max_num: capacity,
current_num: 0,
_key_marker: PhantomData,
_value_marker: PhantomData,
}
}
}
impl<K, V> ResourceCounter for DefaultResourceCounter<K, V>
where
K: Clone + Eq + Hash + Ord + Debug + Send + 'static,
V: Clone + Debug + Send + 'static,
{
type K = K;
type V = V;
fn consume(&mut self, _k: &Self::K, _v: &Self::V) {
self.current_num += 1;
}
fn restore(&mut self, _k: &Self::K, _v: &Self::V) {
self.current_num -= 1;
}
fn exceed_capacity(&self) -> bool {
self.current_num > self.max_num
}
}