Simple caching library for Rust that supports cache invalidation via tags
This repository has been archived on 2025-05-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Find a file
dependabot[bot] 150e3c85fd
Bump codecov/codecov-action from 4 to 5 in the actions group (#21)
Bumps the actions group with 1 update: [codecov/codecov-action](https://github.com/codecov/codecov-action).


Updates `codecov/codecov-action` from 4 to 5
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codecov/codecov-action/compare/v4...v5)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-11-15 07:57:54 +00:00
.github Bump codecov/codecov-action from 4 to 5 in the actions group (#21) 2024-11-15 07:57:54 +00:00
src Update redis requirement from 0.23.0 to 0.24.0 (#13) 2023-12-06 17:19:12 +00:00
tests Update redis requirement from 0.25.0 to 0.26.0 (#17) 2024-07-29 14:29:57 +00:00
.envrc Add integration tests 2023-03-22 11:42:06 +01:00
.gitignore Add integration tests 2023-03-22 11:42:06 +01:00
Cargo.toml Update thiserror requirement from 1.0.40 to 2.0.0 (#20) 2024-11-06 07:59:52 +00:00
LICENSE Add license 2023-03-22 11:43:11 +01:00
README.md Move crate root docs into readme 2023-05-22 16:22:13 +02:00
release.toml Merge remote-tracking branch 'ci/main' into develop 2023-05-02 14:34:06 +02:00

check test codecov Version dependency status

fnct

Simple caching library for Rust 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() {
    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
}