Skip to content

Commit

Permalink
fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
w273732573 committed Jun 23, 2024
1 parent 8a9f4d6 commit 4cd9314
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/lru.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

use algorithm::LruCache;

#[cfg(feature="ttl")]
fn run_ttl() {
let mut lru = LruCache::new(3);
lru.insert_with_ttl("help", "ok", 1);
Expand All @@ -20,5 +21,6 @@ fn main() {
assert_eq!(lru.get("this"), Some(&"lru"));
assert_eq!(lru.get("now"), None);

#[cfg(feature="ttl")]
run_ttl();
}
3 changes: 3 additions & 0 deletions src/cache/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl<K, V, S> LruCache<K, V, S> {
}

/// 获取当前检查lru的间隔
#[cfg(feature="ttl")]
pub fn get_check_step(&self) -> u64 {
self.check_step
}
Expand All @@ -174,6 +175,7 @@ impl<K, V, S> LruCache<K, V, S> {
/// 如果数据太大的话遍历一次可能会比较久的时长
/// 一次清理时间复杂度O(n)
/// 仅仅在插入时触发检查,获取时仅检查当前元素
#[cfg(feature="ttl")]
pub fn set_check_step(&mut self, check_step: u64) {
self.check_step = check_step;
self.check_next = get_timestamp() + self.check_step;
Expand Down Expand Up @@ -642,6 +644,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
/// 插入带有生存时间的元素
/// 每次获取像redis一样,并不会更新生存时间
/// 如果需要更新则需要手动的进行重新设置
#[cfg(feature="ttl")]
#[inline(always)]
pub fn insert_with_ttl(&mut self, k: K, v: V, ttl: u64) -> Option<V> {
self.has_ttl = true;
Expand Down

0 comments on commit 4cd9314

Please sign in to comment.