glimr_redis/cache/cache
Redis Cache Operations
Provides cache operations for Redis-based caching. Uses Valkyrie for Redis communication with connection pooling. All keys are automatically prefixed for safe flush operations.
Values
pub fn decrement(
pool: pool.Pool,
key: String,
by: Int,
) -> Result(Int, cache.CacheError)
Decrements a numeric value in the cache by the specified amount. If the key does not exist, it is initialized to 0 before decrementing. Returns the new value.
pub fn flush(pool: pool.Pool) -> Result(Nil, cache.CacheError)
Removes all cached values for this pool using SCAN + DEL. Only deletes keys with this pool’s prefix, leaving other data in Redis intact. Safe for shared Redis instances.
pub fn forget(
pool: pool.Pool,
key: String,
) -> Result(Nil, cache.CacheError)
Removes a value from the cache by key. This operation is idempotent and returns Ok(Nil) even if the key did not exist in the cache.
pub fn get(
pool: pool.Pool,
key: String,
) -> Result(String, cache.CacheError)
Retrieves a value from the cache by key. Returns the cached string value on success, or NotFound if the key does not exist in the cache.
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 using the provided decoder. Returns SerializationError if the cached value cannot be parsed as valid JSON.
pub fn has(pool: pool.Pool, key: String) -> Bool
Checks if a key exists in the cache. Returns True if the key is present, False otherwise. Does not return the value, only checks for existence.
pub fn increment(
pool: pool.Pool,
key: String,
by: Int,
) -> Result(Int, cache.CacheError)
Increments a numeric value in the cache by the specified amount. If the key does not exist, it is initialized to 0 before incrementing. Returns the new value.
pub fn pull(
pool: pool.Pool,
key: String,
) -> Result(String, cache.CacheError)
Retrieves a value and removes it from the cache atomically. Useful for one-time tokens or values that should only be read once. Returns NotFound if key does not exist.
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. The value will automatically expire after the specified duration. Returns Ok(Nil) on success.
pub fn put_forever(
pool: pool.Pool,
key: String,
value: String,
) -> Result(Nil, cache.CacheError)
Stores a value in the cache permanently without any expiration time. The value will remain until explicitly deleted or the cache is flushed.
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. The value is encoded using the provided encoder function before being stored as a JSON string.
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 without expiration. The value is encoded using the provided encoder function before being stored.
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 is only called on cache miss. Returns ComputeError if the compute function fails.
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 if not found. The compute function is only called on cache miss. Value never expires.
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 if not found. Handles both cache miss and deserialization errors by recomputing the value.