glimr_postgres/cache/cache
PostgreSQL Cache Operations
Provides cache operations using PostgreSQL as the storage backend. Cache entries are stored in a database table with key, value, and expiration columns. Expired entries are cleaned up lazily.
Values
pub fn cleanup_expired(
pool: pool.Pool,
) -> Result(Nil, cache.CacheError)
Removes expired entries from the cache table. Can be called periodically to clean up stale data and reclaim storage space.
pub fn create_table(
pool: pool.Pool,
) -> Result(Nil, cache.CacheError)
Creates the cache table if it doesn’t exist. Should be called during application startup or migration to ensure the cache storage is ready.
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 table. Deletes every row regardless of expiration status, effectively resetting the cache.
pub fn forget(
pool: pool.Pool,
key: String,
) -> Result(Nil, cache.CacheError)
Removes a value from the cache by key. Returns Ok 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 remain in the table until cleanup_expired is called.
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 to check both existence and expiration status.
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. Preserves the original expiration timestamp.
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. Overwrites any existing value for the same key with the new value and expiration.
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 expiration value of 0 to indicate the entry never expires.
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.