pub struct Cache {Show 14 fields
pub sync_token: String,
pub full_sync_date_utc: Option<DateTime<Utc>>,
pub last_sync: Option<DateTime<Utc>>,
pub items: Vec<Item>,
pub projects: Vec<Project>,
pub labels: Vec<Label>,
pub sections: Vec<Section>,
pub notes: Vec<Note>,
pub project_notes: Vec<ProjectNote>,
pub reminders: Vec<Reminder>,
pub filters: Vec<Filter>,
pub collaborators: Vec<Collaborator>,
pub collaborator_states: Vec<CollaboratorState>,
pub user: Option<User>,
/* private fields */
}Expand description
Local cache for Todoist data.
The cache structure mirrors the Sync API response for easy updates from sync operations. It stores all relevant resources and metadata about the last sync.
§Thread Safety
Cache is Send and Sync, but it has no internal synchronization.
Concurrent reads are safe, but concurrent writes or read-modify-write
patterns require external synchronization.
For multi-threaded access, wrap in Arc<RwLock<Cache>>:
use std::sync::{Arc, RwLock};
use todoist_cache_rs::Cache;
let cache = Arc::new(RwLock::new(Cache::new()));
// Read access
let items_count = cache.read().unwrap().items.len();
// Write access
cache.write().unwrap().sync_token = "new_token".to_string();In typical CLI usage, the cache is owned by a single-threaded runtime and external synchronization is not needed.
Fields§
§sync_token: StringThe sync token for incremental syncs. Use “*” for a full sync or the stored token for incremental updates.
full_sync_date_utc: Option<DateTime<Utc>>UTC timestamp when the last full sync was performed. This is set when a full sync completes successfully.
last_sync: Option<DateTime<Utc>>UTC timestamp of the last successful sync (full or incremental).
items: Vec<Item>Cached tasks (called “items” in the Sync API).
projects: Vec<Project>Cached projects.
labels: Vec<Label>Cached personal labels.
sections: Vec<Section>Cached sections.
notes: Vec<Note>Cached task comments (called “notes” in the Sync API).
project_notes: Vec<ProjectNote>Cached project comments.
reminders: Vec<Reminder>Cached reminders.
filters: Vec<Filter>Cached saved filters.
collaborators: Vec<Collaborator>Cached collaborators for shared projects.
collaborator_states: Vec<CollaboratorState>Cached collaborator membership states by project.
user: Option<User>Cached user information.
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty cache with sync_token set to “*” for initial full sync.
Sourcepub fn with_data(
sync_token: String,
full_sync_date_utc: Option<DateTime<Utc>>,
last_sync: Option<DateTime<Utc>>,
items: Vec<Item>,
projects: Vec<Project>,
labels: Vec<Label>,
sections: Vec<Section>,
notes: Vec<Note>,
project_notes: Vec<ProjectNote>,
reminders: Vec<Reminder>,
filters: Vec<Filter>,
user: Option<User>,
) -> Self
pub fn with_data( sync_token: String, full_sync_date_utc: Option<DateTime<Utc>>, last_sync: Option<DateTime<Utc>>, items: Vec<Item>, projects: Vec<Project>, labels: Vec<Label>, sections: Vec<Section>, notes: Vec<Note>, project_notes: Vec<ProjectNote>, reminders: Vec<Reminder>, filters: Vec<Filter>, user: Option<User>, ) -> Self
Creates a new cache with provided data and rebuilds indexes.
This is primarily useful for testing. The indexes are automatically rebuilt after construction.
Sourcepub fn rebuild_indexes(&mut self)
pub fn rebuild_indexes(&mut self)
Rebuilds all lookup indexes from current cache data.
This is called automatically after applying sync responses and should be called after loading the cache from disk.
Sourcepub fn find_project(&self, name_or_id: &str) -> Option<&Project>
pub fn find_project(&self, name_or_id: &str) -> Option<&Project>
Find a project by ID or name (case-insensitive). O(1) lookup.
Sourcepub fn find_section(
&self,
name_or_id: &str,
project_id: Option<&str>,
) -> Option<&Section>
pub fn find_section( &self, name_or_id: &str, project_id: Option<&str>, ) -> Option<&Section>
Find a section by ID or name (case-insensitive) within a project. O(1) lookup.
If project_id is provided, returns the section only if it belongs to that project.
If project_id is None and there’s exactly one match, returns it.
Sourcepub fn find_label(&self, name_or_id: &str) -> Option<&Label>
pub fn find_label(&self, name_or_id: &str) -> Option<&Label>
Find a label by ID or name (case-insensitive). O(1) lookup.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the cache has never been synced (sync_token is “*”).
Sourcepub fn needs_full_sync(&self) -> bool
pub fn needs_full_sync(&self) -> bool
Returns true if the cache requires a full sync. This is true when the sync_token is “*”.
Sourcepub fn apply_sync_response(&mut self, response: &SyncResponse)
pub fn apply_sync_response(&mut self, response: &SyncResponse)
Applies a sync response to the cache, merging in changes.
This method handles both full and incremental sync responses:
- Updates the sync token and timestamps
- For full sync: replaces all resources with the response data
- For incremental sync: merges changes (add/update/delete by ID)
Resources with is_deleted: true are removed from the cache.
§Arguments
response- The sync response from the Todoist API
Sourcepub fn apply_mutation_response(&mut self, response: &SyncResponse)
pub fn apply_mutation_response(&mut self, response: &SyncResponse)
Applies a mutation response to the cache.
This method is similar to apply_sync_response() but is specifically
designed for write operation (mutation) responses. It:
- Updates the sync_token from the response
- Updates the last_sync timestamp
- Merges any resources returned in the response (add/update/delete by ID)
Unlike full sync responses, mutation responses always use incremental merge logic since they only contain affected resources.
Note: The temp_id_mapping from the response should be used by the caller
to resolve temporary IDs before calling this method, or the caller can
use the returned response’s temp_id_mapping to look up real IDs.
§Arguments
response- The sync response from a mutation (write) operation