statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

FR: Optional context for cache tags #2415

Open rrelmy opened 5 years ago

rrelmy commented 5 years ago

Describe the solution you'd like I would like to add a custom context/scope that is added to the cache key to a cache tag. So I can use a cache tag inside a loop and cache individual items instead of the complete loop.

Additional context

{{ collection, or some other loop }}
{{ cache :context="id" }}
  <h1>{{ some expensive stuff}}</h1>
{{ /cache }}
…

Would you be willing to sponsor this feature? Nah, but I can send a patch :-)

rrelmy commented 5 years ago

Context might be not the best name but it works.

Patch based on statamic 2.11.13

Index: statamic/bundles/Cache/CacheTags.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- statamic/bundles/Cache/CacheTags.php    (revision 29eb66ad5bf91f7385b85ea2d5665571fb6748d1)
+++ statamic/bundles/Cache/CacheTags.php    (date 1562597019000)
@@ -21,9 +21,10 @@
         }

         // Create a hash so we can identify it. Include the URL in the hash if this is scoped to the page.
+        $context = $this->get('context');
         $hash = ($this->get('scope', 'site') === 'page')
-            ? md5(URL::makeAbsolute(URL::getCurrent()) . $this->content)
-            : md5($this->content);
+            ? md5(URL::makeAbsolute(URL::getCurrent()) . $this->content . $context)
+            : md5($this->content . $context);

         $path = 'troves:' . $hash;