Move crate root docs into readme

This commit is contained in:
Felix Bargfeldt 2023-05-22 16:22:13 +02:00
parent 420b8b0be8
commit eebb02c45f
Signed by: Defelo
GPG key ID: 2A05272471204DD3
2 changed files with 44 additions and 46 deletions

View file

@ -6,3 +6,46 @@
# fnct
Simple caching library for Rust that supports cache invalidation via tags
## Example
```rust
use std::time::Duration;
use fnct::{backend::AsyncRedisBackend, format::PostcardFormatter, keyfn, AsyncCache};
use redis::{aio::MultiplexedConnection, Client};
struct Application {
cache: AsyncCache<AsyncRedisBackend<MultiplexedConnection>, PostcardFormatter>,
}
keyfn!(my_cache_key(a: i32, b: i32));
impl Application {
async fn test(&self, a: i32, b: i32) -> i32 {
self.cache
.cached(my_cache_key(a, b), &["sum"], None, || async {
// expensive computation
a + b
})
.await
.unwrap()
}
}
#[tokio::main]
async fn main() {
let Ok(redis_server) = std::env::var("REDIS_SERVER") else { return; };
let client = Client::open(redis_server).unwrap();
let conn = client.get_multiplexed_async_connection().await.unwrap();
let app = Application {
cache: AsyncCache::new(
AsyncRedisBackend::new(conn, "my_application".to_owned()),
PostcardFormatter,
Duration::from_secs(600),
),
};
assert_eq!(app.test(1, 2).await, 3); // run expensive computation and fill cache
assert_eq!(app.test(1, 2).await, 3); // load result from cache
app.cache.pop_key(my_cache_key(1, 2)).await.unwrap(); // invalidate cache by key
app.cache.pop_tag("sum").await.unwrap(); // invalidate cache by tag
}
```

View file

@ -1,49 +1,4 @@
//! Simple caching library that supports cache invalidation via tags.
//!
//! #### Example
//! ```
//! use std::time::Duration;
//!
//! use fnct::{backend::AsyncRedisBackend, format::PostcardFormatter, keyfn, AsyncCache};
//! use redis::{aio::MultiplexedConnection, Client};
//!
//! struct Application {
//! cache: AsyncCache<AsyncRedisBackend<MultiplexedConnection>, PostcardFormatter>,
//! }
//!
//! keyfn!(my_cache_key(a: i32, b: i32));
//! impl Application {
//! async fn test(&self, a: i32, b: i32) -> i32 {
//! self.cache
//! .cached(my_cache_key(a, b), &["sum"], None, || async {
//! // expensive computation
//! a + b
//! })
//! .await
//! .unwrap()
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! # if let Ok(REDIS_SERVER) = std::env::var("REDIS_SERVER") {
//! let client = Client::open(REDIS_SERVER).unwrap();
//! let conn = client.get_multiplexed_async_connection().await.unwrap();
//! let app = Application {
//! cache: AsyncCache::new(
//! AsyncRedisBackend::new(conn, "my_application".to_owned()),
//! PostcardFormatter,
//! Duration::from_secs(600),
//! ),
//! };
//! assert_eq!(app.test(1, 2).await, 3); // run expensive computation and fill cache
//! assert_eq!(app.test(1, 2).await, 3); // load result from cache
//! app.cache.pop_key(my_cache_key(1, 2)).await.unwrap(); // invalidate cache by key
//! app.cache.pop_tag("sum").await.unwrap(); // invalidate cache by tag
//! # }
//! }
//! ```
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(clippy::dbg_macro, clippy::use_debug)]
#![warn(missing_docs, missing_debug_implementations)]