Skip to content

It it possible to serialize cache data? #235

Answered by tatsuya6502
sondrelg asked this question in Q&A
Discussion options

You must be logged in to vote

Hi. moka cache currently do not provide a convenient way to serialize the cache data. So you need to do it by yourself:

  1. Iterate over the cache and collect the entries into a std::collections::HashMap.
  2. Serialize the HashMap into binary using serde and bincode.

There is a limitation though. I will explain it later.

Example

use std::collections::HashMap;

use moka::sync::Cache;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
struct ResultItem {
    value: String,
}

fn main() {
    // Create a cache.
    let cache = Cache::builder().max_capacity(1024).build();

    // Insert a value for "key1".
    cache.insert(
        "key1".to_string(),
        Resu…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@sondrelg
Comment options

@tatsuya6502
Comment options

@sondrelg
Comment options

Answer selected by sondrelg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants