From 8913da5830ece85fe8ae89a67f055953544bf988 Mon Sep 17 00:00:00 2001 From: Yatharth Mathur Date: Fri, 12 Jan 2024 15:25:41 +0530 Subject: [PATCH] refactor: use mutable ref to value item --- src/data_store/store.rs | 12 +++++------- src/data_store/value_entry.rs | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/data_store/store.rs b/src/data_store/store.rs index ca61b3a..c6370b7 100644 --- a/src/data_store/store.rs +++ b/src/data_store/store.rs @@ -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}; @@ -104,14 +104,12 @@ impl KeyValueStore { } fn _add(&mut self, key: String, value: i64) -> Option> { - 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)), } diff --git a/src/data_store/value_entry.rs b/src/data_store/value_entry.rs index 9bf5750..00d136c 100644 --- a/src/data_store/value_entry.rs +++ b/src/data_store/value_entry.rs @@ -20,7 +20,7 @@ pub enum CacheValueError { } #[derive(Clone)] -enum CacheValue { +pub enum CacheValue { Integer64(i64), Bytes(Vec), String(String),