{"id":11453,"date":"2026-05-02T07:44:44","date_gmt":"2026-05-02T07:44:44","guid":{"rendered":"https:\/\/programmingfields.com\/?p=11453"},"modified":"2026-05-04T08:26:21","modified_gmt":"2026-05-04T08:26:21","slug":"laravel-13-cache-touch","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/laravel-13-cache-touch\/","title":{"rendered":"Laravel 13 Cache::touch(): Extend Cache TTL Without Re-Fetching the Value"},"content":{"rendered":"\n<p>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 <strong>extend<\/strong> a cache item&#8217;s expiry \u2014 without fetching and re-storing the entire value?<\/p>\n\n\n\n<p>Before Laravel 13, you had no clean way to do this. You had to <code>Cache::get()<\/code> the value, then <code>Cache::put()<\/code> it back. That meant two round trips to your cache driver \u2014 even when the actual data had not changed at all. Laravel 13 fixes this with <strong><code>Cache::touch()<\/code><\/strong>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#What_Is_Cache_touch_in_Laravel\" >What Is Cache::touch() in Laravel?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#Basic_Usage\" >Basic Usage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#Using_Carbon_and_DateTime\" >Using Carbon and DateTime<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#Real-World_Use_Cases\" >Real-World Use Cases<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#1_Sliding_Session_Windows\" >1. Sliding Session Windows<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#2_Keeping_Active_Dashboard_Data_Fresh\" >2. Keeping Active Dashboard Data Fresh<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#3_Rate_Limiting_with_Sliding_Windows\" >3. Rate Limiting with Sliding Windows<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#Before_vs_After_Laravel_13\" >Before vs After Laravel 13<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/laravel-13-cache-touch\/#Final_Thoughts\" >Final Thoughts<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_Cache_touch_in_Laravel\"><\/span><strong>What Is Cache::touch() in Laravel?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Laravel Cache touch<\/strong> 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.<\/p>\n\n\n\n<p>No reading. No rewriting. Just a TTL update.<\/p>\n\n\n\n<p>Under the hood, it maps to the most efficient native operation for each driver:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Redis<\/strong> \u2192 single <code>EXPIRE<\/code> command<\/li>\n\n\n\n<li><strong>Memcached<\/strong> \u2192 native <code>TOUCH<\/code> operation<\/li>\n\n\n\n<li><strong>Database<\/strong> \u2192 single <code>UPDATE<\/code> query<\/li>\n<\/ul>\n\n\n\n<p>This is cleaner, faster, and uses far less memory \u2014 especially in high-traffic applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_Usage\"><\/span><strong>Basic Usage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using <strong>Laravel Cache touch<\/strong> is simple. Just pass the cache key and the new TTL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\Cache;\n\n\/\/ Extend by seconds\nCache::touch('user_session:123', 3600);<\/code><\/pre>\n\n\n\n<p>The method returns <code>true<\/code> if the key exists and the TTL was extended successfully. It returns <code>false<\/code> if the key does not exist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$extended = Cache::touch('user_session:123', 3600);\n\nif ($extended) {\n    \/\/ TTL extended successfully\n} else {\n    \/\/ Key not found in cache\n}<\/code><\/pre>\n\n\n\n<p>That return value is very useful. Moreover, you can use it to decide whether to regenerate the cache when a key has already expired.<\/p>\n\n\n\n<p class=\"has-text-align-center\">Recommended: <a href=\"https:\/\/programmingfields.com\/laravel-13-ai-agents\/\">Laravel 13 AI Agents: Complete Guide with Real Code Examples<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_Carbon_and_DateTime\"><\/span><strong>Using Carbon and DateTime<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You are not limited to seconds. You can also pass a <code>Carbon<\/code> instance, a <code>DateTime<\/code>, or a <code>DateInterval<\/code> to specify an exact expiry time.<\/p>\n\n\n\n<p>This is especially helpful for readable, expressive code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ Extend until 6 hours from now\nCache::touch('analytics_data', now()-&gt;addHours(6));\n\n\/\/ Extend until end of the day\nCache::touch('daily_report', now()-&gt;endOfDay());\n\n\/\/ Extend by a DateInterval\nCache::touch('rate_limit:user:42', new DateInterval('PT30M'));<\/code><\/pre>\n\n\n\n<p>Clean, readable, and very Laravel. Furthermore, this makes it easy to align cache expiry with business logic \u2014 like expiring a report cache at midnight or extending a session until the end of a working day.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Real-World_Use_Cases\"><\/span><strong>Real-World Use Cases<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, where would you actually use <strong>Laravel Cache touch<\/strong> in a real project? Here are a few practical examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Sliding_Session_Windows\"><\/span><strong>1. Sliding Session Windows<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ In a middleware, after every authenticated request\nCache::touch('user_session:' . $user-&gt;id, 1800); \/\/ 30 minutes<\/code><\/pre>\n\n\n\n<p>This is a single cache operation per request. Previously, you would need two operations. That adds up significantly at scale.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Keeping_Active_Dashboard_Data_Fresh\"><\/span><strong>2. Keeping Active Dashboard Data Fresh<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public function ping(Request $request)\n{\n    \/\/ User is active \u2014 extend the dashboard cache\n    Cache::touch('dashboard:' . $request-&gt;user()-&gt;id, 300);\n\n    return response()-&gt;json(['status' =&gt; 'active']);\n}<\/code><\/pre>\n\n\n\n<p>Therefore, the cache only expires when the user genuinely stops interacting \u2014 not on a fixed timer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Rate_Limiting_with_Sliding_Windows\"><\/span><strong>3. Rate Limiting with Sliding Windows<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Rate limiters often need a sliding TTL. Instead of rebuilding the counter from scratch, just extend it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$key = 'api_rate:' . $request-&gt;ip();\n\nif (Cache::has($key)) {\n    Cache::touch($key, 60); \/\/ Slide the window forward\n} else {\n    Cache::put($key, 1, 60);\n}<\/code><\/pre>\n\n\n\n<p>As a result, you get a true sliding rate limit \u2014 not a fixed-window one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Before_vs_After_Laravel_13\"><\/span><strong>Before vs After Laravel 13<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is a direct comparison of the old approach versus the new <strong>Laravel Cache touch<\/strong> approach:<\/p>\n\n\n\n<p><strong>Before Laravel 13:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ Two operations \u2014 get the value, then put it back with new TTL\n$value = Cache::get('report_cache');\n\nif ($value !== null) {\n    Cache::put('report_cache', $value, now()-&gt;addHours(6));\n}<\/code><\/pre>\n\n\n\n<p>This fetches the full cached value over the network. For large objects, this is expensive.<\/p>\n\n\n\n<p><strong>After Laravel 13:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ One operation \u2014 just update the TTL\nCache::touch('report_cache', now()-&gt;addHours(6));<\/code><\/pre>\n\n\n\n<p>Simpler. Faster. No unnecessary data transfer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Thoughts\"><\/span><strong>Final Thoughts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><code>Cache::touch()<\/code> 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 <strong>Laravel Cache touch<\/strong> is an easy win. Less network traffic, fewer cache operations, and cleaner code \u2014 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s expiry \u2014 without fetching and re-storing the entire value? Before Laravel 13, you had no clean way to do this. You had to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":11466,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2564],"tags":[],"yst_prominent_words":[373,100,1132,456,364,191],"class_list":{"0":"post-11453","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-laravel","8":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2026\/05\/Laravel-Cache-Touch.png?fit=1672%2C941&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=11453"}],"version-history":[{"count":2,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11453\/revisions"}],"predecessor-version":[{"id":11474,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11453\/revisions\/11474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/11466"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=11453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=11453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=11453"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=11453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}