Struct cached::stores::DiskCacheBuilder
source · pub struct DiskCacheBuilder<K, V> { /* private fields */ }
Implementations§
source§impl<K, V> DiskCacheBuilder<K, V>
impl<K, V> DiskCacheBuilder<K, V>
sourcepub fn new<S: AsRef<str>>(cache_name: S) -> DiskCacheBuilder<K, V>
pub fn new<S: AsRef<str>>(cache_name: S) -> DiskCacheBuilder<K, V>
Initialize a DiskCacheBuilder
sourcepub fn set_lifespan(self, seconds: u64) -> Self
pub fn set_lifespan(self, seconds: u64) -> Self
Specify the cache TTL/lifespan in seconds
sourcepub fn set_refresh(self, refresh: bool) -> Self
pub fn set_refresh(self, refresh: bool) -> Self
Specify whether cache hits refresh the TTL
sourcepub fn set_disk_directory<P: AsRef<Path>>(self, dir: P) -> Self
pub fn set_disk_directory<P: AsRef<Path>>(self, dir: P) -> Self
Set the disk path for where the data will be stored
sourcepub fn set_sync_to_disk_on_cache_change(
self,
sync_to_disk_on_cache_change: bool
) -> Self
pub fn set_sync_to_disk_on_cache_change( self, sync_to_disk_on_cache_change: bool ) -> Self
Specify whether the cache should sync to disk on each cache change. sled flushes every sled::Config::flush_every_ms which has a default value. In some use cases, the default value may not be quick enough, or a user may want to reduce the flush rate / turn off auto-flushing to reduce IO (and only flush on cache changes). (see DiskCacheBuilder::set_connection_config for more control over the sled connection)
sourcepub fn set_connection_config(self, config: Config) -> Self
pub fn set_connection_config(self, config: Config) -> Self
Specify the sled::Config to use for the connection to the disk cache.
§Note
Don’t use sled::Config::path as any value set here will be overwritten by either the path specified in DiskCacheBuilder::set_disk_directory, or the default value calculated by DiskCacheBuilder.
§Example Use Case
By default sled automatically syncs to disk at a frequency specified in sled::Config::flush_every_ms. A user may want to reduce IO by setting a lower flush frequency, or by setting sled::Config::flush_every_ms to None. Also see DiskCacheBuilder::set_sync_to_disk_on_cache_change which allows for syncing to disk on each cache change.
use cached::stores::{DiskCacheBuilder, DiskCache};
let config = sled::Config::new().flush_every_ms(None);
let cache: DiskCache<String, String> = DiskCacheBuilder::new("my-cache")
.set_connection_config(config)
.set_sync_to_disk_on_cache_change(true)
.build()
.unwrap();