Skip to content

Commit

Permalink
Merge pull request #1 from PsiACE/improve
Browse files Browse the repository at this point in the history
Improve code
  • Loading branch information
selmeci authored Dec 20, 2020
2 parents 60f6c05 + 4d431e1 commit d493b93
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://github.com/selmeci/cascara"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
indexmap = "1.3"
indexmap = "1.6"
log = "0.4"
rand = "0.7"
probabilistic-collections = "0.7"
11 changes: 8 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ where
/// }
/// ```
///
pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, ()> {
pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, Option<()>> {
self.insert_with_ttl(k, v, Duration::from_secs(0))
}

Expand Down Expand Up @@ -538,7 +538,12 @@ where
/// assert!(!cache.contains(&1));
/// ```
///
pub fn insert_with_ttl(&mut self, k: K, v: V, expiration: Duration) -> Result<Option<V>, ()> {
pub fn insert_with_ttl(
&mut self,
k: K,
v: V,
expiration: Duration,
) -> Result<Option<V>, Option<()>> {
self.store.cleanup(&self.on_evict);

let key_hash = self.key_hash(&k);
Expand All @@ -561,7 +566,7 @@ where
}
Err(victim) => {
self.remove_victim(victim);
Err(())
Err(Some(()))
}
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
use std::fmt::{self, Debug, Formatter};

const METRICS: usize = 6;
const METRICS: usize = 5;

///
/// Possible metric types
///
#[derive(Debug, Clone)]
pub enum MetricType {
Hit,
Hit = 0,
Miss,
KeyInsert,
KeyUpdate,
KeyEvict,
}

impl MetricType {
fn metric_idx(&self) -> usize {
match self {
MetricType::Hit => 0,
MetricType::Miss => 1,
MetricType::KeyInsert => 2,
MetricType::KeyUpdate => 3,
MetricType::KeyEvict => 4,
}
}
}

///
/// Collector of possible metrics types in cache
///
Expand All @@ -49,15 +37,15 @@ impl Metrics {
///
pub fn insert(&mut self, metric: MetricType, k: &u64, delta: usize) {
let idx = (k % 25) * 10;
let vals = &mut self.all[metric.metric_idx()];
let vals = &mut self.all[metric as usize];
vals[idx as usize] += delta;
}

///
/// Get collected data about metric type
///
pub fn get(&self, metric: MetricType) -> usize {
let vals = &self.all[metric.metric_idx()];
let vals = &self.all[metric as usize];
vals.iter().sum()
}

Expand Down Expand Up @@ -116,6 +104,12 @@ impl Metrics {
}
}

impl Default for Metrics {
fn default() -> Self {
Self::new()
}
}

impl Debug for Metrics {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Metrics")
Expand Down
2 changes: 1 addition & 1 deletion src/tiny_lfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl TinyLFU for TinyLFUCache {
} else {
self.sketcher.insert(k, 1);
self.previous_window.remove(k);
self.actual_window.insert(k.clone());
self.actual_window.insert(*k);
}
self.increments += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Expiration for ExpirationMap {
let mut result = HashSet::new();
let mut buckets = Vec::new();
for (id, _) in self.buckets.range(..now) {
buckets.push(id.clone())
buckets.push(*id)
}
for bucket in buckets {
for item in self.buckets.remove(&bucket).unwrap() {
Expand Down

0 comments on commit d493b93

Please sign in to comment.