Skip to content

Commit

Permalink
feat: limit cache size by configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrix126 committed Jun 14, 2024
1 parent 33343e0 commit c305037
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ uuid = {version="1.8", features=["v4", "fast-rng"]}
nohash = "0.2"
derive_more = {version="0.99", default-features=false, features=["deref", "deref_mut"]}
enclose = "1.2"
typesize = "0.1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Mnemosyne is a http request caching in memory proxy API.
## Status of development
Work in progress, not functional.
### TODO
- [ ] allows to limit cache by size
- [x] allows to limit cache by size
- [ ] remove allocation when possible
- [ ] organize code in modules
- [ ] tracing
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ impl Default for Config {
pub struct CacheConfig {
/// cache expiration after last request
pub expiration: Duration,
/// in megabytes, the maximum size of memory the cache can take.
pub size_limit: u64,
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use reqwest::{
};
use std::{str::FromStr, sync::Arc};
use tokio::{spawn, sync::Mutex};
use typesize::TypeSize;
use url::Url;
use uuid::Uuid;

Expand Down Expand Up @@ -45,6 +46,22 @@ impl Cache {
MokaCache::builder()
.name("mnemosyne")
.time_to_idle(config.cache.expiration)
.weigher(
|_key: &Uuid, (s, h, b): &(StatusCode, HeaderMap, Bytes)| -> u32 {
let s = s.to_string().get_size() as u32;
let h = h.iter().fold(0, |acc, x| {
acc + (x.0.to_string().get_size()
+ x.1.to_str().unwrap().to_string().get_size())
as u32
});
let b = b.len() as u32;
// note that the size overhead of the index cache is not taken into account.
// could take about 100B per entry.
s + h + b
},
)
// This cache will hold up to 32MiB of values.
.max_capacity(config.cache.size_limit * 1024 * 1024)
.build_with_hasher(ahash::RandomState::new()),
)
}
Expand Down

0 comments on commit c305037

Please sign in to comment.