pub struct Cache { /* private fields */ }Implementations§
Source§impl Cache
impl Cache
Sourcepub fn drop()
pub fn drop()
Drops the global cache instance and clears all in-memory entries, locks, and file locks.
After calling this, Cache::instance will return an error until Cache::new is called again.
Sourcepub fn instance() -> Result<Self, Box<dyn Error>>
pub fn instance() -> Result<Self, Box<dyn Error>>
Returns the globally initialized Cache instance if it exists.
§Errors
Returns an error if Cache::new has not been called yet.
§Example
let cache = cache_ro::Cache::instance().unwrap();Sourcepub fn new(config: CacheConfig) -> Result<Self, Box<dyn Error>>
pub fn new(config: CacheConfig) -> Result<Self, Box<dyn Error>>
Creates and initializes a new global Cache instance.
If persistence is enabled in the provided CacheConfig, the directory will be created
and any persisted entries will be loaded into memory.
This will also start a background thread to periodically clean up expired entries.
§Errors
Returns an error if:
- A cache instance already exists.
- The persistence directory cannot be created.
§Example
let cache = cache_ro::Cache::new(Default::default()).unwrap();Sourcepub fn set<V: Serialize>(
&self,
key: &str,
value: V,
ttl: Duration,
) -> Result<(), Box<dyn Error>>
pub fn set<V: Serialize>( &self, key: &str, value: V, ttl: Duration, ) -> Result<(), Box<dyn Error>>
Sourcepub fn get<V: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<V>
pub fn get<V: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<V>
Sourcepub fn expire(&self, key: &str) -> Option<Duration>
pub fn expire(&self, key: &str) -> Option<Duration>
Returns the remaining TTL for a given cache key.
§Returns
Some(duration) if the entry exists and is not expired, otherwise None.
Sourcepub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>
pub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>
Removes an entry from the cache.
If persistence is enabled, the removal is also reflected on disk.
§Errors
Returns an error if persistence fails.
Sourcepub fn clear(&self) -> Result<(), Box<dyn Error>>
pub fn clear(&self) -> Result<(), Box<dyn Error>>
Clears all entries from the cache (in memory and on disk if persistent).
§Errors
Returns an error if persistent files cannot be deleted.