Paulund
Subscribe

Caching in Laravel

2 March 2026, 3 min read

From the archive. Versions and APIs may have moved on since I wrote this.

On this page
  1. Cache Drivers
  2. Basic Operations
  3. remember() — The Most Useful Method
  4. Cache Tags
  5. Lock-Based Caching
  6. When to Cache
  7. Tips
  8. Related

Caching sits between your application and its slowest operations — database queries, external API calls, expensive computations. Laravel's cache system is driver-agnostic: the same code works whether you are using files, Redis, Memcached, or a database-backed store.

Cache Drivers

Configure your driver in .env:

.env
CACHE_DRIVER=redis

Common drivers and when to use them:

DriverBest for
fileLocal development, single-server deployments
redisProduction, shared across multiple servers
memcachedHigh-throughput scenarios where you do not need persistence
databaseSimple deployments where Redis is not available
arrayTests — fast, in-memory, cleared between requests
nullDisabling the cache entirely (useful in CI)

Basic Operations

PHP
use Illuminate\Support\Facades\Cache;

// Store a value for 60 seconds
Cache::put('user:1', $user, 60);

// Retrieve it (returns null if missing)
$user = Cache::get('user:1');

// Retrieve with a default fallback
$user = Cache::get('user:1', 'not found');

// Check if a key exists
if (Cache::has('user:1')) { /* ... */ }

// Remove a key
Cache::forget('user:1');

// Clear the entire cache
Cache::flush();

remember() — The Most Useful Method

remember() combines a get and a put into one call. It fetches the value from cache if it exists; otherwise it runs the closure, stores the result, and returns it:

PHP
$categories = Cache::remember('categories', 3600, function () {
    return Category::with('children')->get();
});

Use rememberForever() when the data rarely changes and you plan to invalidate manually:

PHP
$settings = Cache::rememberForever('app_settings', function () {
    return Setting::all()->keyBy('key');
});

Cache Tags

Tags let you group related cache entries and flush them together. This is especially useful when you want to invalidate all cached data related to a specific entity:

PHP
// Store with tags
Cache::tags(['articles', 'homepage'])->put('featured_articles', $articles, 3600);

// Flush everything tagged 'articles'
Cache::tags(['articles'])->flush();

Note: the file and database drivers do not support tags. Use Redis or Memcached if you need them.

Lock-Based Caching

If multiple processes might try to populate the same cache key simultaneously, use a lock to prevent a thundering herd:

PHP
$result = Cache::lock('generate-report', 10)->get(function () {
    // Only one process runs this at a time
    return generateExpensiveReport();
});

When to Cache

Cache is most valuable for:

  • Database queries that return the same data across many requests (navigation menus, config values, lookup tables).
  • External API responses that do not change frequently.
  • Rendered HTML fragments that are expensive to build.

Avoid caching data that is user-specific or changes on every request — it adds complexity without a performance gain.

Tips

  • Set cache TTLs (time-to-live) conservatively at first. It is easier to shorten a TTL than to track down stale data.
  • Use a consistent key-naming convention (e.g., {entity}:{id}:{data}) to make debugging and manual cache inspection straightforward.
  • In tests, set CACHE_DRIVER=array so each test starts with a clean cache and you do not accidentally depend on cached state from a previous test.
  • Combine caching with Laravel's config:cache and route:cache in production for maximum bootstrap performance.

Next in Laravel

2 March 2026CarbonImmutable in Laravel: prevent date mutation bugs

Tags

  • Laravel
  • Caching
  • Performance