Skip to content

Commit

Permalink
refactor: use mutable ref to value item
Browse files Browse the repository at this point in the history
  • Loading branch information
he-ym committed Jan 12, 2024
1 parent b0028ca commit 8913da5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/data_store/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::value_entry::{CacheValueError, TypeCastErrorDetails, ValueEntry};
use super::value_entry::{CacheValue, CacheValueError, TypeCastErrorDetails, ValueEntry};
use std::collections::HashMap;
use std::num::TryFromIntError;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -104,14 +104,12 @@ impl KeyValueStore {
}

fn _add(&mut self, key: String, value: i64) -> Option<Result<i64, CacheValueError>> {
if let Some(value_entry) = self._remove_and_none_if_expired(&key) {
if let Some(value_entry) = self._data.get_mut(&key) {
match value_entry.get_value_as_i64() {
Ok(integer) => {
self._insert(
&key,
&ValueEntry::from_i64(integer + value, value_entry.expiration),
);
self.get_i64(key)
let updated_integer_value = integer + value;
value_entry.value = CacheValue::Integer64(updated_integer_value);
Some(Ok(updated_integer_value))
}
Err(e) => Some(Err(e)),
}
Expand Down
2 changes: 1 addition & 1 deletion src/data_store/value_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum CacheValueError {
}

#[derive(Clone)]
enum CacheValue {
pub enum CacheValue {
Integer64(i64),
Bytes(Vec<u8>),
String(String),
Expand Down

0 comments on commit 8913da5

Please sign in to comment.