Crate powerpack_cache

Source
Expand description

⚡ Cache management for your Alfred workflow

This crate provides a simple cache management system for your Alfred workflow. Data is cached in the workflow’s cache directory and is updated asynchronously.

The cache supports arbitrary data types for each key as long as they can be serialized and deserialized from JSON.

§Concepts

  • key: a unique identifier for a piece of data stored in the cache.

  • ttl: the Time To Live (TTL) for the data in the cache. If the data in the cache is older than this then it is considered “expired”.

  • checksum: an optional checksum for a particular cache key. You can use this to bust the cache for some other reason than the data being expired.

  • update_fn: a function that is called to update the cache for a key. This is typically some operation that is expensive and/or slow and you do not want to block the Alfred workflow. This function is called asynchronously to update the cache. If the cache is already being updated by another process, then the function is not called.

The following behaviour is determined by the policy of the query:

  • When to call a provided update_fn.
  • When to return bad, expired, or checksum mismatched data.

§Usage

Use a Builder to construct a new Cache`.

use std::time::Duration;
use powerpack::cache;

let cache = cache::Builder::new().ttl(Duration::from_secs(60 * 60)).build();

Then the only function to call is .query(..) which will fetch the cached value and/or detach a process to update it.

let expensive_fn = || {
    // perform some expensive operation, like fetching
    // something over the internet
};

let q = cache::Query::new("unique_key").update_fn(expensive_fn);
let data = cache.query(q)?;

Structs§

Builder
A builder for a cache.
Cache
Manage a cache of data on disk.
Query
Query the cache for data.

Enums§

BuildError
Raised when constructing a new cache.
QueryError
Raised when accessing data in the cache.
QueryPolicy
The policy for querying the cache.