Skip to content
David Ciulla edited this page Nov 7, 2026 · 2 revisions

Cache

Laravex provides a unified cache API backed by a GenServer in Laravex.Cache.Manager. The manager supports three store drivers: array, file, and database.


Configuration

Cache settings live in config/cache.exs. The active store is selected by the cache.default key (overridable by the CACHE_STORE environment variable):

%{
  "default" => Laravex.Env.get("CACHE_STORE", "array"),
  "stores" => %{
    "array"    => %{"driver" => "array"},
    "file"     => %{"driver" => "file"},
    "database" => %{"driver" => "database"}
  }
}
Driver Description
array In-process ETS-backed memory store. Data is lost when the application restarts.
file Writes Erlang term binaries under storage/framework/cache/data/. Key names are hex-encoded.
database Reads and writes to the cache table (created by the included migration). Falls back to array if the table does not exist.

Note: Redis and Memcached drivers are not available in Laravex. Use CACHE_STORE=file for a simple persistent store or CACHE_STORE=database when you need cache entries visible across application restarts.


Basic Usage

The Laravex.Cache.Manager module is the cache facade. All key arguments are automatically coerced to strings.

Storing Values

# Store forever
Laravex.Cache.Manager.put("key", "value")

# Store for N seconds
Laravex.Cache.Manager.put("key", "value", 3600)

Retrieving Values

# Returns nil when the key is missing
Laravex.Cache.Manager.get("key")

# Returns the default when the key is missing or expired
Laravex.Cache.Manager.get("key", "default")

Removing Values

# Delete a single key
Laravex.Cache.Manager.forget("key")

# Clear the entire cache
Laravex.Cache.Manager.flush()

Remember

remember/3 retrieves an item from the cache; if the item does not exist, it executes the given closure, stores the result, and returns it.

users = Laravex.Cache.Manager.remember("users", 3600, fn ->
  App.Models.User.all()
end)

If the value is already cached and has not expired, the closure is not called.


Helper Access

The cache/0 helper in Helpers returns the Laravex.Cache.Manager module so you can call it without the full module name in controllers or service modules:

import Laravex.Support.Helpers

cache().put("featured_posts", posts, 600)
cache().get("featured_posts")

TTL and Expiry

TTL is specified in seconds.

# 10 minutes
Laravex.Cache.Manager.put("token", token, 600)

# 1 hour
Laravex.Cache.Manager.put("config_snapshot", config, 3600)

# No expiry (nil TTL = persisted until flush or forget)
Laravex.Cache.Manager.put("app_key", key)

Expired entries are evaluated lazily — they are removed when the key is next read.


Database Store

When using the database driver, the cache table must exist. The migration is included at database/migrations/0001_01_01_000001_create_cache_table.ex. Run it with:

./artisan migrate

The cache table has three columns: key, value (Erlang term encoded as Base64), and expiration (Unix timestamp, 0 for no expiry).

If the cache table is missing at runtime, the manager silently degrades to the array driver rather than raising an error.


Cache in the Task Scheduler

The Task-Scheduling without_overlapping guard uses Laravex.Cache.Manager internally to implement a distributed mutex. The cache key follows the pattern schedule:overlap:{name}.


See Also

Clone this wiki locally