Repository navigation
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.
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=filefor a simple persistent store orCACHE_STORE=databasewhen you need cache entries visible across application restarts.
The Laravex.Cache.Manager module is the cache facade. All key arguments are automatically coerced to strings.
# Store forever
Laravex.Cache.Manager.put("key", "value")
# Store for N seconds
Laravex.Cache.Manager.put("key", "value", 3600)# 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")# Delete a single key
Laravex.Cache.Manager.forget("key")
# Clear the entire cache
Laravex.Cache.Manager.flush()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.
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 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.
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 migrateThe 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.
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}.
-
Task-Scheduling —
withoutOverlappinguses the cache for overlap prevention -
Artisan-Console —
config:cache,optimize, andoptimize:clear
Laravex — Laravel-faithful application skeleton & runtime for Elixir.
GitHub
·
Docs
·
Limitations
·
Laravel Docs
Not affiliated with Laravel LLC or the Elixir core team. Built to feel like Laravel on the BEAM.
- Home
- Installation
- Configuration
- Directory-Structure
- Request-Lifecycle
- Service-Container
- Service-Providers
- Deployment
- Routing
- Middleware
- CSRF-Protection
- Controllers
- Requests
- Responses
- Views
- Blade-Templates
- Asset-Bundling
- Session
- Validation
- Error-Handling
- Logging
- Artisan-Console
- Broadcasting
- Cache
- Collections
- Events
- File-Storage
- Helpers
- HTTP-Client
- Localization
- Notifications
- Pipeline
- Queues
- Rate-Limiting
- Strings
- Task-Scheduling