-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
[package] | ||
name = "fs-cache" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "fs_cache" | ||
crate-type = ["rlib", "cdylib"] | ||
bench = false | ||
|
||
[dependencies] | ||
log = { version = "0.4.17", features = ["release_max_level_off"] } | ||
serde_json = "1.0.82" | ||
serde = { version = "1.0.138", features = ["derive"] } | ||
jni = { version = "0.21.1", optional = true } | ||
jnix = { version = "0.5.1", features = ["derive"], optional = true } | ||
data-error = { path = "../data-error" } | ||
data-resource = { path = "../data-resource" } | ||
fs-storage = { path = "../fs-storage"} | ||
linked-hash-map = "0.5.6" | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0.81" | ||
quickcheck = { version = "1.0.3", features = ["use_logging"] } | ||
quickcheck_macros = "1.0.0" | ||
tempdir = "0.3.7" | ||
|
||
[features] | ||
default = ["jni-bindings"] | ||
jni-bindings = ["jni", "jnix"] |
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,69 @@ | ||
use data_error::Result; | ||
use fs_storage::{base_storage::SyncStatus, monoid::Monoid}; | ||
use std::path::Path; | ||
|
||
use crate::memory_limited_storage::MemoryLimitedStorage; | ||
|
||
/// A generic cache implementation that stores values with LRU eviction in memory | ||
/// and persistence to disk. | ||
pub struct Cache<K, V> { | ||
storage: MemoryLimitedStorage<K, V>, | ||
} | ||
|
||
impl<K, V> Cache<K, V> | ||
where | ||
K: Ord | ||
+ Clone | ||
+ serde::Serialize | ||
+ serde::de::DeserializeOwned | ||
+ std::fmt::Display | ||
+ std::hash::Hash | ||
+ std::str::FromStr, | ||
V: Clone + serde::Serialize + serde::de::DeserializeOwned + Monoid<V>, | ||
{ | ||
/// Create a new cache with given capacity | ||
/// - `label`: Used for logging and error messages | ||
/// - `path`: Directory where cache files will be stored | ||
/// - `max_memory_items`: Maximum number of items to keep in memory | ||
pub fn new( | ||
label: String, | ||
path: &Path, | ||
max_memory_items: usize, | ||
) -> Result<Self> { | ||
let storage = MemoryLimitedStorage::new(label, path, max_memory_items)?; | ||
|
||
Ok(Self { storage }) | ||
} | ||
|
||
/// Get a value from the cache if it exists | ||
/// Returns None if not found | ||
pub fn get(&mut self, key: &K) -> Result<Option<V>> { | ||
self.storage.get(key) | ||
} | ||
|
||
/// Store a value in the cache | ||
/// Will persist to disk and maybe keep in memory based on LRU policy | ||
pub fn set(&mut self, key: K, value: V) -> Result<()> { | ||
self.storage.set(key, value) | ||
} | ||
|
||
/// Load most recent cached items into memory based on timestamps | ||
pub fn load_recent(&mut self) -> Result<()> { | ||
self.storage.load_fs() | ||
} | ||
|
||
/// Get number of items currently in memory | ||
// pub fn memory_items(&self) -> usize { | ||
// self.storage.memory_items() | ||
// } | ||
|
||
/// Get sync status between memory and disk | ||
pub fn sync_status(&self) -> Result<SyncStatus> { | ||
self.storage.sync_status() | ||
} | ||
|
||
/// Sync changes to disk | ||
pub fn sync(&mut self) -> Result<()> { | ||
self.storage.sync() | ||
} | ||
} |
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,2 @@ | ||
pub mod cache; | ||
pub mod memory_limited_storage; |
Oops, something went wrong.