Skip to main content

CacheStore

Struct CacheStore 

Source
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

Source

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.

Source

pub fn with_path(path: PathBuf) -> Self

Creates a new CacheStore with a custom path.

This is primarily useful for testing.

Source

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.

Source

pub fn path(&self) -> &PathBuf

Returns the path to the cache file.

Source

pub fn load(&self) -> Result<Cache>

Loads the cache from disk.

§Errors
  • Returns CacheStoreError::ReadError if the file cannot be read.
  • Returns CacheStoreError::Json if 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.

Source

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::ReadError for I/O errors other than “file not found”.
  • Returns CacheStoreError::Json if the file contains invalid JSON.
Source

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::CreateDirError if the directory cannot be created.
  • Returns CacheStoreError::WriteError if the file cannot be written.
  • Returns CacheStoreError::Json if serialization fails.
Source

pub fn exists(&self) -> bool

Returns true if the cache file exists on disk.

Source

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.

Source

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::ReadError if the file cannot be read.
  • Returns CacheStoreError::Json if 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.

Source

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::ReadError for I/O errors other than “file not found”.
  • Returns CacheStoreError::Json if the file contains invalid JSON.
Source

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::CreateDirError if the directory cannot be created.
  • Returns CacheStoreError::WriteError if the file cannot be written.
  • Returns CacheStoreError::Json if serialization fails.
Source

pub async fn delete_async(&self) -> Result<()>

Deletes the cache file from disk asynchronously.

This is the async equivalent of delete().

§Errors

Returns CacheStoreError::DeleteError if the file cannot be deleted. Does not return an error if the file doesn’t exist.

Trait Implementations§

Source§

impl Clone for CacheStore

Source§

fn clone(&self) -> CacheStore

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CacheStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more