glimr/cache/file/cache

File Cache Operations

Provides cache operations for file-based caching. Files are stored in a 2-level directory structure using SHA256 hashes of keys for efficient filesystem access.

Values

pub fn decrement(
  pool: pool.Pool,
  key: String,
  by: Int,
) -> Result(Int, cache.CacheError)

Decrements a numeric value in the cache. If the key doesn’t exist, starts from 0. Delegates to increment with a negated value.

pub fn flush(pool: pool.Pool) -> Result(Nil, cache.CacheError)

Removes all cached values from the cache directory. Deletes the entire cache directory tree and all its contents.

pub fn forget(
  pool: pool.Pool,
  key: String,
) -> Result(Nil, cache.CacheError)

Removes a value from the cache by key. Returns Ok(Nil) even if the key didn’t exist, making it safe to call without checking existence first.

pub fn get(
  pool: pool.Pool,
  key: String,
) -> Result(String, cache.CacheError)

Retrieves a value from the cache by key. Returns NotFound if the key doesn’t exist or has expired. Expired entries are lazily deleted on access.

pub fn get_json(
  pool: pool.Pool,
  key: String,
  decoder: decode.Decoder(a),
) -> Result(a, cache.CacheError)

Retrieves a JSON value from the cache and decodes it. Returns SerializationError if the cached value cannot be parsed as valid JSON matching the decoder.

pub fn has(pool: pool.Pool, key: String) -> Bool

Checks if a key exists in the cache and hasn’t expired. Uses get internally so expired entries are lazily deleted during this check.

pub fn increment(
  pool: pool.Pool,
  key: String,
  by: Int,
) -> Result(Int, cache.CacheError)

Increments a numeric value in the cache. If the key doesn’t exist, starts from 0. Stores the result with no expiration.

pub fn pull(
  pool: pool.Pool,
  key: String,
) -> Result(String, cache.CacheError)

Retrieves a value and removes it from the cache in one operation. Useful for one-time tokens or consuming queued values.

pub fn put(
  pool: pool.Pool,
  key: String,
  value: String,
  ttl_seconds: Int,
) -> Result(Nil, cache.CacheError)

Stores a value in the cache with a TTL (time-to-live) in seconds. Returns Ok(Nil) on success, or a CacheError on failure. Creates directories as needed.

pub fn put_forever(
  pool: pool.Pool,
  key: String,
  value: String,
) -> Result(Nil, cache.CacheError)

Stores a value in the cache permanently (no expiration). Uses a special timestamp value of 0 to indicate no TTL. Creates directories as needed.

pub fn put_json(
  pool: pool.Pool,
  key: String,
  value: a,
  encoder: fn(a) -> json.Json,
  ttl_seconds: Int,
) -> Result(Nil, cache.CacheError)

Stores a value as JSON in the cache with a TTL. Encodes the value using the provided encoder function before storing.

pub fn put_json_forever(
  pool: pool.Pool,
  key: String,
  value: a,
  encoder: fn(a) -> json.Json,
) -> Result(Nil, cache.CacheError)

Stores a value as JSON in the cache permanently. Encodes the value using the provided encoder function before storing with no expiration.

pub fn remember(
  pool: pool.Pool,
  key: String,
  ttl_seconds: Int,
  compute: fn() -> Result(String, e),
) -> Result(String, cache.CacheError)

Gets a value from cache, or computes and stores it if not found. The compute function returns a Result to handle computation errors gracefully.

pub fn remember_forever(
  pool: pool.Pool,
  key: String,
  compute: fn() -> Result(String, e),
) -> Result(String, cache.CacheError)

Gets a value from cache, or computes and stores it permanently. Like remember but with no TTL for values that should never expire.

pub fn remember_json(
  pool: pool.Pool,
  key: String,
  ttl_seconds: Int,
  decoder: decode.Decoder(a),
  compute: fn() -> Result(a, e),
  encoder: fn(a) -> json.Json,
) -> Result(a, cache.CacheError)

Gets a JSON value from cache, or computes, encodes, and stores it. Combines remember semantics with JSON encoding and decoding.

Search Document