pub struct CacheStore { /* private fields */ }Expand description
Persistent storage for the Todoist cache.
CacheStore handles reading and writing the cache to disk using XDG-compliant
paths. On Unix systems, the cache is stored at ~/.cache/td/cache.json.
§Thread Safety
CacheStore is Send and Sync, but file operations are not atomic.
Concurrent calls to save() from multiple threads could result in corrupted
data on disk. For concurrent access, use external synchronization:
use std::sync::{Arc, Mutex};
use todoist_cache_rs::CacheStore;
let store = Arc::new(Mutex::new(CacheStore::new()?));In typical CLI usage, the store is owned by a single-threaded runtime and external synchronization is not needed.
§Example
use todoist_cache_rs::{Cache, CacheStore};
let store = CacheStore::new()?;
// Load existing cache or create new one
let cache = store.load().unwrap_or_default();
// Save cache to disk
store.save(&cache)?;Implementations§
Source§impl CacheStore
impl CacheStore
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new CacheStore with the default XDG cache path.
The cache file will be located at ~/.cache/td/cache.json on Unix systems.
§Errors
Returns CacheStoreError::NoCacheDir if the home directory cannot be determined.
Sourcepub fn with_path(path: PathBuf) -> Self
pub fn with_path(path: PathBuf) -> Self
Creates a new CacheStore with a custom path.
This is primarily useful for testing.
Sourcepub fn default_path() -> Result<PathBuf>
pub fn default_path() -> Result<PathBuf>
Returns the default XDG cache path for the cache file.
On Unix: ~/.cache/td/cache.json
On macOS: ~/Library/Caches/td/cache.json
On Windows: C:\Users\<User>\AppData\Local\td\cache\cache.json
§Errors
Returns CacheStoreError::NoCacheDir if the home directory cannot be determined.
Sourcepub fn load(&self) -> Result<Cache>
pub fn load(&self) -> Result<Cache>
Loads the cache from disk.
§Errors
- Returns
CacheStoreError::ReadErrorif the file cannot be read. - Returns
CacheStoreError::Jsonif the file contains invalid JSON.
§Note
If the cache file does not exist, this returns an I/O error with
ErrorKind::NotFound. Use load_or_default() to get a default cache
when the file is missing.
Sourcepub fn load_or_default(&self) -> Result<Cache>
pub fn load_or_default(&self) -> Result<Cache>
Loads the cache from disk, returning a default cache if the file doesn’t exist.
§Errors
- Returns
CacheStoreError::ReadErrorfor I/O errors other than “file not found”. - Returns
CacheStoreError::Jsonif the file contains invalid JSON.
Sourcepub fn save(&self, cache: &Cache) -> Result<()>
pub fn save(&self, cache: &Cache) -> Result<()>
Saves the cache to disk atomically.
Creates the parent directory if it doesn’t exist. The cache is written as pretty-printed JSON for easier debugging.
Uses atomic write (tempfile + rename) to prevent corruption if the process crashes mid-write.
§Errors
- Returns
CacheStoreError::CreateDirErrorif the directory cannot be created. - Returns
CacheStoreError::WriteErrorif the file cannot be written. - Returns
CacheStoreError::Jsonif serialization fails.
Sourcepub fn delete(&self) -> Result<()>
pub fn delete(&self) -> Result<()>
Deletes the cache file from disk.
§Errors
Returns CacheStoreError::DeleteError if the file cannot be deleted.
Does not return an error if the file doesn’t exist.
Sourcepub async fn load_async(&self) -> Result<Cache>
pub async fn load_async(&self) -> Result<Cache>
Loads the cache from disk asynchronously.
This is the async equivalent of load(). Use this method
in async contexts to avoid blocking the tokio runtime.
§Errors
- Returns
CacheStoreError::ReadErrorif the file cannot be read. - Returns
CacheStoreError::Jsonif the file contains invalid JSON.
§Note
If the cache file does not exist, this returns an I/O error with
ErrorKind::NotFound. Use load_or_default_async()
to get a default cache when the file is missing.
Sourcepub async fn load_or_default_async(&self) -> Result<Cache>
pub async fn load_or_default_async(&self) -> Result<Cache>
Loads the cache from disk asynchronously, returning a default cache if the file doesn’t exist.
This is the async equivalent of load_or_default().
§Errors
- Returns
CacheStoreError::ReadErrorfor I/O errors other than “file not found”. - Returns
CacheStoreError::Jsonif the file contains invalid JSON.
Sourcepub async fn save_async(&self, cache: &Cache) -> Result<()>
pub async fn save_async(&self, cache: &Cache) -> Result<()>
Saves the cache to disk asynchronously using atomic write.
This is the async equivalent of save(). Use this method
in async contexts to avoid blocking the tokio runtime.
Creates the parent directory if it doesn’t exist. The cache is written as pretty-printed JSON for easier debugging.
Uses atomic write (tempfile + rename) to prevent corruption if the process crashes mid-write.
§Errors
- Returns
CacheStoreError::CreateDirErrorif the directory cannot be created. - Returns
CacheStoreError::WriteErrorif the file cannot be written. - Returns
CacheStoreError::Jsonif serialization fails.
Trait Implementations§
Source§impl Clone for CacheStore
impl Clone for CacheStore
Source§fn clone(&self) -> CacheStore
fn clone(&self) -> CacheStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more