kapot_cache/backend/policy/lru/
mod.rs

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