-
Notifications
You must be signed in to change notification settings - Fork 6
fs-cache: Add Cache Struct
#95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pushkarm029
wants to merge
13
commits into
main
Choose a base branch
from
cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b056880
init
pushkarm029 8797e8f
fix
pushkarm029 56597ac
few changes
pushkarm029 e5b41bc
simplified cache & removed `remove`
pushkarm029 33091bf
fix
pushkarm029 60225f4
major changes
pushkarm029 39d9bb0
fix
pushkarm029 a4b201a
fix
pushkarm029 de954cd
fix
pushkarm029 9136354
improve comments
pushkarm029 b469c6b
few changes
pushkarm029 4741ab6
clippy
pushkarm029 d6a002b
few more tweaks
pushkarm029 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| [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"] } | ||
| data-error = { path = "../data-error" } | ||
| data-resource = { path = "../data-resource" } | ||
pushkarm029 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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" | ||
pushkarm029 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tareknaser marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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>, | ||
| } | ||
pushkarm029 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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() | ||
| // } | ||
pushkarm029 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// 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() | ||
| } | ||
pushkarm029 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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; | ||
pushkarm029 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub mod memory_limited_storage; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.