Redis.Cache.Store (Redis v0.7.1)

Copy Markdown View Source

ETS-backed cache store for client-side caching.

This is the default implementation of Redis.Cache.Backend. Stores key -> value mappings with optional TTL, bounded size, and configurable eviction policy. Tracks hits, misses, and evictions for observability.

Options

  • :max_entries - maximum number of entries (default: 10_000, 0 for unlimited)
  • :eviction_policy - :lru or :fifo (default: :lru)

Summary

Functions

Destroys the cache store (deletes the ETS tables).

Clears the entire cache.

Gets a value from the cache. Returns {:hit, value, store} or {:miss, store}.

Initializes the store from options. Implements Redis.Cache.Backend.init/1.

Invalidates one or more keys. Called when Redis pushes invalidation.

Invalidates all cache entries that reference the given Redis key(s).

Creates a new cache store backed by ETS tables.

Puts a value in the cache with optional TTL in milliseconds.

Puts a value in the cache and records a ref from redis_key to cache_key.

Returns cache statistics.

Removes expired entries from the cache.

Types

eviction_policy()

@type eviction_policy() :: :lru | :fifo

t()

@type t() :: %Redis.Cache.Store{
  eviction_policy: eviction_policy(),
  evictions: non_neg_integer(),
  hits: non_neg_integer(),
  index_table: :ets.tid(),
  max_entries: non_neg_integer(),
  misses: non_neg_integer(),
  refs_table: :ets.tid(),
  stores: non_neg_integer(),
  table: :ets.tid()
}

Functions

destroy(store)

@spec destroy(t()) :: :ok

Destroys the cache store (deletes the ETS tables).

flush(store)

@spec flush(t()) :: t()

Clears the entire cache.

get(store, key)

@spec get(t(), term()) :: {:hit, term(), t()} | {:miss, t()}

Gets a value from the cache. Returns {:hit, value, store} or {:miss, store}.

init(opts)

@spec init(keyword()) :: {:ok, t()}

Initializes the store from options. Implements Redis.Cache.Backend.init/1.

invalidate(store, keys)

@spec invalidate(t(), [String.t()] | nil) :: t()

Invalidates one or more keys. Called when Redis pushes invalidation.

invalidate_refs(store, keys)

@spec invalidate_refs(t(), [String.t()] | nil) :: t()

Invalidates all cache entries that reference the given Redis key(s).

Looks up the refs table to find cache keys that depend on each Redis key, invalidates them, and cleans up the ref entries.

new(name \\ :redis_ex_cache, opts \\ [])

@spec new(
  atom(),
  keyword()
) :: t()

Creates a new cache store backed by ETS tables.

put(store, key, value, ttl_ms \\ nil)

@spec put(t(), term(), term(), non_neg_integer() | nil) :: t()

Puts a value in the cache with optional TTL in milliseconds.

put_with_ref(store, cache_key, redis_key, value, ttl_ms \\ nil)

@spec put_with_ref(t(), term(), String.t(), term(), non_neg_integer() | nil) :: t()

Puts a value in the cache and records a ref from redis_key to cache_key.

When redis_key is later invalidated via invalidate_refs/2, all cache entries that reference it are removed.

stats(store)

@spec stats(t()) :: map()

Returns cache statistics.

sweep_expired(store)

@spec sweep_expired(t()) :: t()

Removes expired entries from the cache.