Expand description
Simple caching library that supports cache invalidation via tags.
Example
use std::time::Duration;
use fnct::async_redis::AsyncRedisCache;
use redis::{aio::MultiplexedConnection, Client};
struct Application {
cache: AsyncRedisCache<MultiplexedConnection>,
}
impl Application {
async fn cached_function(&self, a: i32, b: i32) -> i32 {
self.cache.cached((a, b), &["sum"], None, async move {
// expensive computation
a + b
})
.await
.unwrap()
}
}
let client = Client::open("redis://localhost:6379/0").unwrap();
let conn = client.get_multiplexed_async_connection().await.unwrap();
let app = Application {
cache: AsyncRedisCache::new(conn, "my_application".to_owned(), Duration::from_secs(600)),
};
assert_eq!(app.cached_function(1, 2).await, 3); // run expensive computation and fill cache
assert_eq!(app.cached_function(1, 2).await, 3); // load result from cache
app.cache.pop_key((1, 2)).await.unwrap(); // invalidate cache by key
app.cache.pop_tag("sum").await.unwrap(); // invalidate cache by tagModules
- Async cache backed by redis.