Skip to content

Commit a53e5da

Browse files
author
Kirk Fletcher
committed
enhanced dynamic content cache handling
1 parent f910083 commit a53e5da

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/Pagespeed.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\View;
88
use kirksfletcher\pagespeed\filters\RemoveComments;
99
use kirksfletcher\pagespeed\filters\RemoveWhiteSpace;
10+
use Mockery\Exception;
1011

1112
class Pagespeed
1213
{
@@ -29,13 +30,21 @@ public function view($view, $data = [], $slug = '')
2930

3031
if($this->allowDynamic) {
3132
$cacheRef = md5(strtolower($view.$slug) . serialize($data));
33+
$tag = ($slug == '') ? md5(strtolower($view)) : md5(strtolower($slug));
34+
35+
$view = Cache::tags([$tag])->rememberForever($cacheRef, function () use ($view, $data) {
36+
return $this->renderView($view, $data);
37+
});
3238
}else{
3339
$cacheRef = ($slug == '') ? md5(strtolower($view)) : md5(strtolower($slug));
40+
41+
$view = Cache::rememberForever($cacheRef, function () use ($view, $data) {
42+
return $this->renderView($view, $data);
43+
});
3444
}
3545

36-
$view = Cache::rememberForever($cacheRef, function () use ($view, $data) {
37-
return $this->renderView($view, $data);
38-
});
46+
47+
3948
} else {
4049
$view = $this->renderView($view, $data);
4150
}
@@ -54,7 +63,13 @@ public function view($view, $data = [], $slug = '')
5463
public function killCacheView($slug)
5564
{
5665
$slug = md5(strtolower($slug));
57-
Cache::forget($slug);
66+
67+
if($this->allowDynamic){
68+
Cache::tags([$slug])->flush();
69+
}else{
70+
Cache::forget($slug);
71+
}
72+
5873
}
5974

6075
/**
@@ -84,7 +99,11 @@ public function plugin($plugin, $enable = true)
8499
*/
85100
public function allowDynamicContent($state = true)
86101
{
87-
$this->allowDynamic = $state;
102+
if(Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
103+
$this->allowDynamic = $state;
104+
}else{
105+
throw new Exception('Dynamic content caching requires memcached or redis enabled as the cache driver.');
106+
}
88107
}
89108

90109

0 commit comments

Comments
 (0)