forked from fuzzland/cached-eth-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lru cache backend; update arg parsing; add version to Cargo.lock;…
… add caching for eth_maxPriorityFeePerGas
- Loading branch information
Showing
9 changed files
with
236 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use lru::LruCache; | ||
use std::num::NonZeroUsize; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use anyhow::Context; | ||
use serde_json::{from_str, Value}; | ||
|
||
use super::{CacheBackend, CacheBackendFactory, CacheStatus}; | ||
|
||
pub struct LruBackendFactory { | ||
data: Arc<Mutex<LruCache<String, String>>>, | ||
} | ||
|
||
impl LruBackendFactory { | ||
pub fn new(cap: usize) -> Self { | ||
Self { | ||
data: Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(cap).unwrap()))), | ||
} | ||
} | ||
} | ||
|
||
impl CacheBackendFactory for LruBackendFactory { | ||
fn get_instance(&self) -> anyhow::Result<Box<dyn CacheBackend>> { | ||
Ok(Box::new(LruBackend { | ||
data: self.data.clone(), | ||
})) | ||
} | ||
} | ||
|
||
pub struct LruBackend { | ||
data: Arc<Mutex<LruCache<String, String>>>, | ||
} | ||
|
||
impl CacheBackend for LruBackend { | ||
fn read(&mut self, method: &str, params_key: &str) -> anyhow::Result<CacheStatus> { | ||
let key = format!("{method}:{params_key}"); | ||
|
||
let mut lru_cache = self.data.lock().unwrap(); | ||
let v = match lru_cache.get(&key) { | ||
Some(value) => { | ||
let value = from_str::<Value>(&value).context("fail to deserialize cache value")?; | ||
|
||
CacheStatus::Cached { key, value } | ||
} | ||
|
||
None => CacheStatus::Missed { key }, | ||
}; | ||
|
||
Ok(v) | ||
} | ||
|
||
fn write(&mut self, key: &str, value: &str) -> anyhow::Result<()> { | ||
let mut lru_cache = self.data.lock().unwrap(); | ||
let _ = lru_cache.put(key.to_string(), value.to_string()); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod lru_backend; | ||
pub mod memory_backend; | ||
pub mod redis_backend; | ||
|
||
|
Oops, something went wrong.