Skip to content

Commit 30ce858

Browse files
committed
Bump version + Changelog + Readme
1 parent 4fdf7ea commit 30ce858

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# v0.2.0
2+
New/Changed:
3+
* TTL Backing now uses fewer iterations through the entire list
4+
* Backings can define a `Meta` type which can be used for improved handling with
5+
the loader
6+
* TTL Backing now supports a `TtlMeta` -> Custom duration per entry
7+
* TTL Backing now supports other nested `CacheBacking`s (i.e. TTL+LRU)
8+
* New methods: `LoadingCache::with_meta_loader()`, `LoadingCache#set_with_meta`
9+
10+
Breaking:
11+
* Backings trait changed:
12+
1. All methods now return a result
13+
2. contains_key takes a mutable self reference
14+
3. remove_if returns the removed k/v pairs
15+
4. Meta Type added / set signature now has a meta field
16+
117
# v0.1.2
218
New/Changed:
319
* Replace SystemTime calls with Instant

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "cache_loader_async"
33
description = "A thread safe loading cache with async loader functions based on tokio"
44
repository = "https://github.com/ZeroTwo-Bot/cache-loader-async-rs"
5-
version = "0.1.2"
5+
version = "0.2.0"
66
authors = ["Alexander Becker <bytealex@zerotwo.bot>", "Spencer Sharkey <spencer@sf-n.com>"]
77
edition = "2018"
88
keywords = ["cache", "async"]

README.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ async fn main() {
7474
}
7575
```
7676

77+
You can also provide a custom TTL per key, if you use the `with_meta_loader` method.
78+
Below example will override the global 30s ttl with a 10s ttl.
79+
Yes, it doesn't make sense to override every key, so you should be having conditions there.
80+
```rust
81+
async fn main() {
82+
let duration: Duration = Duration::from_secs(30);
83+
let cache = LoadingCache::with_meta_loader(TtlCacheBacking::new(duration), move |key: String| {
84+
async move {
85+
Ok(key.to_lowercase())
86+
.with_meta(Some(TtlMeta::from(Duration::from_secs(10))))
87+
}
88+
});
89+
}
90+
```
91+
92+
Additionally, the TTL backing allows you to customize the underlying backing. By default, it's using the
93+
`HashMapBacking`.
94+
95+
```rust
96+
async fn main() {
97+
let duration: Duration = Duration::from_secs(30);
98+
let cache = LoadingCache::with_meta_loader(TtlCacheBacking::with_backing(LruCacheBacking::new(10), duration), move |key: String| {
99+
async move {
100+
Ok(key.to_lowercase())
101+
.with_meta(Some(TtlMeta::from(Duration::from_secs(10))))
102+
}
103+
});
104+
}
105+
```
106+
77107
## Own Backing
78108

79109
To implement an own cache backing, simply implement the public `CacheBacking` trait from the `backing` mod.
@@ -82,11 +112,14 @@ To implement an own cache backing, simply implement the public `CacheBacking` tr
82112
pub trait CacheBacking<K, V>
83113
where K: Eq + Hash + Sized + Clone + Send,
84114
V: Sized + Clone + Send {
85-
fn get(&mut self, key: &K) -> Option<&V>;
86-
fn set(&mut self, key: K, value: V) -> Option<V>;
87-
fn remove(&mut self, key: &K) -> Option<V>;
88-
fn contains_key(&self, key: &K) -> bool;
89-
fn remove_if(&mut self, predicate: Box<dyn Fn((&K, &V)) -> bool + Send + 'static>);
90-
fn clear(&mut self);
115+
type Meta: Clone + Send;
116+
117+
fn get_mut(&mut self, key: &K) -> Result<Option<&mut V>, BackingError>;
118+
fn get(&mut self, key: &K) -> Result<Option<&V>, BackingError>;
119+
fn set(&mut self, key: K, value: V, meta: Option<Self::Meta>) -> Result<Option<V>, BackingError>;
120+
fn remove(&mut self, key: &K) -> Result<Option<V>, BackingError>;
121+
fn contains_key(&mut self, key: &K) -> Result<bool, BackingError>;
122+
fn remove_if(&mut self, predicate: Box<dyn Fn((&K, &V)) -> bool + Send + Sync + 'static>) -> Result<Vec<(K, V)>, BackingError>;
123+
fn clear(&mut self) -> Result<(), BackingError>;
91124
}
92125
```

src/cache_api.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,40 @@ impl<
376376
///
377377
/// # Arguments
378378
///
379-
/// * `key` - The key which should be loaded
379+
/// * `key` - The key which should be set
380+
/// * `value` - The value which should be set
381+
/// * `meta` - The meta which should be attached to the key
380382
///
381383
/// # Return Value
382384
///
383385
/// Returns a Result with:
384386
/// Ok - Previous value of type V wrapped in an Option depending whether there was a previous
385387
/// value
386388
/// Err - Error of type CacheLoadingError
387-
pub async fn set(&self, key: K, value: V) -> Result<Option<V>, CacheLoadingError<E>> {
388-
self.send_cache_action(CacheAction::Set(key, value, None)).await
389+
pub async fn set_with_meta(&self, key: K, value: V, meta: Option<B::Meta>) -> Result<Option<V>, CacheLoadingError<E>> {
390+
self.send_cache_action(CacheAction::Set(key, value, meta)).await
389391
.map(|opt_meta| opt_meta.map(|meta| meta.result))
390392
}
391393

394+
/// Sets the value for specified key and bypasses eventual currently ongoing loads
395+
/// If a key has been set programmatically, eventual concurrent loads will not change
396+
/// the value of the key.
397+
///
398+
/// # Arguments
399+
///
400+
/// * `key` - The key which should be set
401+
/// * `value` - The value which should be set
402+
///
403+
/// # Return Value
404+
///
405+
/// Returns a Result with:
406+
/// Ok - Previous value of type V wrapped in an Option depending whether there was a previous
407+
/// value
408+
/// Err - Error of type CacheLoadingError
409+
pub async fn set(&self, key: K, value: V) -> Result<Option<V>, CacheLoadingError<E>> {
410+
self.set_with_meta(key, value, None).await
411+
}
412+
392413
/// Loads the value for the specified key from the cache and returns None if not present
393414
///
394415
/// # Arguments

0 commit comments

Comments
 (0)