What is the difference between Session and Cache in Laravel?

What is the difference between Session and Cache in Laravel?

What is the difference between Session and Cache in Laravel?

Question

What is the difference between Session and Cache in Laravel?

Answer

Session

Session data is per-user and persists across multiple HTTP requests for the duration of a user's visit. It is identified by a session ID stored in a cookie.

// Store
session(['cart' => $items]);
// or
$request->session()->put('cart', $items);

// Retrieve
$cart = session('cart');

// Flash (available only for next request)
session()->flash('success', 'Order placed!');

// Remove
session()->forget('cart');

Cache

Cache data is application-wide and shared across all users. It is used to store expensive computation results or query results that rarely change.

// Store for 1 hour
Cache::put('settings', $settings, now()->addHour());

// Retrieve or compute
$settings = Cache::remember('settings', 3600, fn () => Setting::all());

// Remove
Cache::forget('settings');

Quick Comparison

SessionCache
ScopePer userApplication-wide
Use caseAuth, cart, flash messagesQuery results, config
Driversfile, cookie, database, redisfile, redis, memcached, array
All Comments