Git Product home page Git Product logo

dashmap-cache's Introduction

Simple example how to use the lib.

Dashmap cache is thread safe and can be shared among threads through Arc in a similar way than Dashmap can.

use dashmap_cache::DashmapCache;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug)]
struct Repository {}

#[derive(Clone, Serialize, Deserialize, Hash, PartialEq, Eq, Debug)]
pub struct SomeStruct {
    pub some_field: String,
    pub some_id: u64,
}

#[derive(Clone, Serialize, Deserialize, Hash, PartialEq, Eq, Debug)]
pub struct SomeResult {
    pub some_result_field: String,
    pub some_result_int: u64,
}
impl Repository {
    pub fn c(&self, arg: &SomeStruct) -> SomeResult {
        // Emulates a database call
        println!("call C with {:?}", arg);
        SomeResult {
            some_result_field: arg.some_field.clone(),
            some_result_int: arg.some_id,
        }
    }
}

fn main() {
    let repo = Repository {};
    let dmc: DashmapCache = DashmapCache::new();
    let invalidation_keys = vec!["some-key".to_string(), "some-other".to_string()];
    let const_arg = SomeStruct {
        some_field: "test".to_owned(),
        some_id: 1,
    };
    // Fills cache with a value
    println!(
        "{}",
        dmc.cached(
            &invalidation_keys,
            |some_struct| repo.c(some_struct),
            &const_arg
        )
        .unwrap()
        .some_result_field
    );
    // Identical call gets memoized;
    println!(
        "{}",
        dmc.cached(
            &invalidation_keys,
            |some_struct| repo.c(some_struct),
            &const_arg
        )
        .unwrap()
        .some_result_field
    );
    // Until you invalidate one of the associated key
    dmc.invalidate("some-key");
    println!(
        "{}",
        dmc.cached(
            &invalidation_keys,
            |some_struct| repo.c(some_struct),
            &const_arg
        )
        .unwrap()
        .some_result_field
    );

    /*
    > cargo run

        call C with SomeStruct { some_field: "test", some_id: 1 }
        test
        test
        call C with SomeStruct { some_field: "test", some_id: 1 }
        test
    */
}

dashmap-cache's People

Contributors

princess-entrapta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.