Skip to content

Commit

Permalink
Merge 8797e8f into 56343b4
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkarm029 authored Nov 17, 2024
2 parents 56343b4 + 8797e8f commit 603c1db
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"data-pdf",
"data-resource",
"fs-atomic-versions",
"fs-cache",
"fs-atomic-light",
"fs-metadata",
"fs-properties",
Expand All @@ -23,6 +24,7 @@ default-members = [
"data-pdf",
"data-resource",
"fs-atomic-versions",
"fs-cache",
"fs-atomic-light",
"fs-metadata",
"fs-properties",
Expand Down
30 changes: 30 additions & 0 deletions fs-cache/Cargo.toml
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"]
69 changes: 69 additions & 0 deletions fs-cache/src/cache.rs
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()
}
}
2 changes: 2 additions & 0 deletions fs-cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cache;
pub mod memory_limited_storage;
Loading

0 comments on commit 603c1db

Please sign in to comment.