The cache system lets you store the result of expensive database queries, computed data, or external API responses so they don't need to be recalculated on every request. Veldora supports two drivers: file (default, persists to disk) and array (in-memory, resets per request — useful for testing).
Configuration
Set the driver in your .env file:
.env
INI
CACHE_DRIVER=file # Options: file, arrayOr change it per-environment in config/cache.php.
Cache Drivers
| Driver | Description | When to use |
|---|---|---|
file | Stores cached items in storage/framework/cache/ | Default for most apps; persistent across requests |
array | Stores in PHP memory only; lost after each request | Automated tests, local debugging |
Storing & Reading Values
example.php
PHP
// Store a value for 10 minutes (600 seconds)
cache(['top_posts' => $posts], 600);
// Retrieve a cached value (returns null if missing or expired)
$posts = cache('top_posts');The remember() Pattern (Recommended)
The remember() helper is the cleanest way to use the cache. It checks if a value is cached — if yes, it returns it; if not, it runs your closure, stores the result, and returns it:
example.php
PHP
// Fetch from cache, or run the closure and cache for 1 hour
$stats = cache()->remember('dashboard_stats', 3600, function () {
return [
'total_users' => User::count(),
'total_posts' => Post::count(),
'new_today' => User::where('created_at', '>=', date('Y-m-d'))->count(),
];
});This is the preferred pattern for controller actions that serve expensive aggregated data.
All Cache Methods
example.php
PHP
// Store (TTL in seconds)
cache(['key' => $value], 3600);
cache()->put('key', $value, 600);
// Retrieve
$value = cache('key'); // Returns null if missing
$value = cache('key', 'default'); // Returns default if missing
// Remember (fetch or compute)
$value = cache()->remember('key', 3600, fn() => expensiveQuery());
// Check existence
if (cache()->has('key')) { ... }
// Remove single item
cache()->forget('key');
// Clear entire cache store
cache()->flush();
// Atomic counter increment / decrement (great for rate limiting, view counts)
cache()->increment('page_views:post-42');
cache()->increment('page_views:post-42', 5); // Increment by 5
cache()->decrement('credits_remaining');
// Store forever (no expiry)
cache()->forever('site_settings', $settings);Practical Example — Caching Posts in a Controller
example.php
PHP
public function index(Request $request): Response
{
$page = (int) $request->query('page', 1);
$posts = cache()->remember("posts:page:{$page}", 300, function () use ($page) {
return Post::where('is_published', '=', 1)
->orderBy('created_at', 'DESC')
->paginate(15, $page);
});
return view('posts.index', ['posts' => $posts]);
}