al8n / stretto

Stretto is a Rust implementation for Dgraph's ristretto (https://github.com/dgraph-io/ristretto). A high performance memory-bound Rust cache.
Apache License 2.0
413 stars 28 forks source link

How to use CacheCallback #28

Closed 0xtyls closed 2 years ago

0xtyls commented 2 years ago

How can I use CacheCallback? I need to delete the related file from the file system when the entry is evicted

al8n commented 2 years ago

Hi, you just need to implement CacheCallback for your own struct, and set it by AsyncCacheBuilder or CacheBuilder depends on the cache type you want to use.

e.g.

struct FooCacheCallback {
 //  ... you code here
}

impl CacheCallback for FooCacheCallback {
  // ... you code here
}

fn main() {
  let cache = CacheBuilder::new(cap, max_cost)
        .set_callback(FooCacheCallback::new())
        .finalize()
        .expect("failed to create cache");
}
0xtyls commented 2 years ago

I'm using AsyncCache and get this compiling error:

expected struct DefaultCacheCallback, found struct FooCacheCallback

0xtyls commented 2 years ago

I figured it out. Instead of using type AsyncCache<String, PathBuf>, I need to use AsyncCache<String, PathBuf, DefaultKeyBuilder, DefaultCoster, DefaultUpdateValidator, FooCacheCallback>