Caching is one of the best ways to speed up a Laravel application. But there has always been a small, annoying problem. What if you just want to extend a cache item’s expiry — without fetching and re-storing the entire value?
Before Laravel 13, you had no clean way to do this. You had to Cache::get() the value, then Cache::put() it back. That meant two round trips to your cache driver — even when the actual data had not changed at all. Laravel 13 fixes this with Cache::touch().
What Is Cache::touch() in Laravel?
Laravel Cache touch is a new method added in Laravel 13. It lets you extend the TTL (Time To Live) of an existing cache item with a single operation.
No reading. No rewriting. Just a TTL update.
Under the hood, it maps to the most efficient native operation for each driver:
- Redis → single
EXPIREcommand - Memcached → native
TOUCHoperation - Database → single
UPDATEquery
This is cleaner, faster, and uses far less memory — especially in high-traffic applications.
Basic Usage
Using Laravel Cache touch is simple. Just pass the cache key and the new TTL:
use Illuminate\Support\Facades\Cache;
// Extend by seconds
Cache::touch('user_session:123', 3600);
The method returns true if the key exists and the TTL was extended successfully. It returns false if the key does not exist:
$extended = Cache::touch('user_session:123', 3600);
if ($extended) {
// TTL extended successfully
} else {
// Key not found in cache
}
That return value is very useful. Moreover, you can use it to decide whether to regenerate the cache when a key has already expired.
Using Carbon and DateTime
You are not limited to seconds. You can also pass a Carbon instance, a DateTime, or a DateInterval to specify an exact expiry time.
This is especially helpful for readable, expressive code:
// Extend until 6 hours from now
Cache::touch('analytics_data', now()->addHours(6));
// Extend until end of the day
Cache::touch('daily_report', now()->endOfDay());
// Extend by a DateInterval
Cache::touch('rate_limit:user:42', new DateInterval('PT30M'));
Clean, readable, and very Laravel. Furthermore, this makes it easy to align cache expiry with business logic — like expiring a report cache at midnight or extending a session until the end of a working day.
Real-World Use Cases
So, where would you actually use Laravel Cache touch in a real project? Here are a few practical examples.
1. Sliding Session Windows
Some applications need sessions that extend on every user action. Instead of regenerating and re-storing the session data on each request, simply touch the key:
// In a middleware, after every authenticated request
Cache::touch('user_session:' . $user->id, 1800); // 30 minutes
This is a single cache operation per request. Previously, you would need two operations. That adds up significantly at scale.
2. Keeping Active Dashboard Data Fresh
Imagine a real-time dashboard that refreshes every 5 minutes. But if a user is actively viewing it, you want to keep the cached data alive:
public function ping(Request $request)
{
// User is active — extend the dashboard cache
Cache::touch('dashboard:' . $request->user()->id, 300);
return response()->json(['status' => 'active']);
}
Therefore, the cache only expires when the user genuinely stops interacting — not on a fixed timer.
3. Rate Limiting with Sliding Windows
Rate limiters often need a sliding TTL. Instead of rebuilding the counter from scratch, just extend it:
$key = 'api_rate:' . $request->ip();
if (Cache::has($key)) {
Cache::touch($key, 60); // Slide the window forward
} else {
Cache::put($key, 1, 60);
}
As a result, you get a true sliding rate limit — not a fixed-window one.
Before vs After Laravel 13
Here is a direct comparison of the old approach versus the new Laravel Cache touch approach:
Before Laravel 13:
// Two operations — get the value, then put it back with new TTL
$value = Cache::get('report_cache');
if ($value !== null) {
Cache::put('report_cache', $value, now()->addHours(6));
}
This fetches the full cached value over the network. For large objects, this is expensive.
After Laravel 13:
// One operation — just update the TTL
Cache::touch('report_cache', now()->addHours(6));
Simpler. Faster. No unnecessary data transfer.
Final Thoughts
Cache::touch() is a small feature. But small improvements like this add up quickly in production applications. If you have any sliding sessions, active dashboards, or rate limiters in your Laravel app, switching to Laravel Cache touch is an easy win. Less network traffic, fewer cache operations, and cleaner code — all at once. Furthermore, this is a great example of what makes each Laravel release worth upgrading to. Not every feature needs to be a headline. Sometimes, the best improvements are the ones that quietly make your code better.

Leave a Reply