diff --git a/src/FragmentCaching.php b/src/FragmentCaching.php new file mode 100644 index 0000000..1739cf7 --- /dev/null +++ b/src/FragmentCaching.php @@ -0,0 +1,70 @@ +cache = $cache; + } + + /** + * Put to the cache. + * + * @param mixed $key + * @param string $fragment + */ + public function put($key, $fragment) + { + $key = $this->normalizeCacheKey($key); + + return $this->cache + ->tags('views') + ->rememberForever($key, function () use ($fragment) { + return $fragment; + }); + } + + /** + * Check if the given key exists in the cache. + * + * @param mixed $key + */ + public function has($key) + { + $key = $this->normalizeCacheKey($key); + + return $this->cache + ->tags('views') + ->has($key); + } + + /** + * Normalize the cache key. + * + * @param mixed $key + */ + protected function normalizeCacheKey($key) + { + if (is_object($key) && method_exists($key, 'getCacheKey')) { + return $key->getCacheKey(); + } + + return $key; + } +} diff --git a/tests/FragmentCachingTest.php b/tests/FragmentCachingTest.php new file mode 100644 index 0000000..e52df23 --- /dev/null +++ b/tests/FragmentCachingTest.php @@ -0,0 +1,25 @@ +makePost(); + + $cache = new \Illuminate\Cache\Repository( + new \Illuminate\Cache\ArrayStore + ); + $cache = new FragmentCaching($cache); + + $cache->put($post->getCacheKey(), '