Skip to content

Commit

Permalink
enhanced dynamic content cache handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirk Fletcher committed May 12, 2019
1 parent f910083 commit a53e5da
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/Pagespeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\View;
use kirksfletcher\pagespeed\filters\RemoveComments;
use kirksfletcher\pagespeed\filters\RemoveWhiteSpace;
use Mockery\Exception;

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

if($this->allowDynamic) {
$cacheRef = md5(strtolower($view.$slug) . serialize($data));
$tag = ($slug == '') ? md5(strtolower($view)) : md5(strtolower($slug));

$view = Cache::tags([$tag])->rememberForever($cacheRef, function () use ($view, $data) {
return $this->renderView($view, $data);
});
}else{
$cacheRef = ($slug == '') ? md5(strtolower($view)) : md5(strtolower($slug));

$view = Cache::rememberForever($cacheRef, function () use ($view, $data) {
return $this->renderView($view, $data);
});
}

$view = Cache::rememberForever($cacheRef, function () use ($view, $data) {
return $this->renderView($view, $data);
});


} else {
$view = $this->renderView($view, $data);
}
Expand All @@ -54,7 +63,13 @@ public function view($view, $data = [], $slug = '')
public function killCacheView($slug)
{
$slug = md5(strtolower($slug));
Cache::forget($slug);

if($this->allowDynamic){
Cache::tags([$slug])->flush();
}else{
Cache::forget($slug);
}

}

/**
Expand Down Expand Up @@ -84,7 +99,11 @@ public function plugin($plugin, $enable = true)
*/
public function allowDynamicContent($state = true)
{
$this->allowDynamic = $state;
if(Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
$this->allowDynamic = $state;
}else{
throw new Exception('Dynamic content caching requires memcached or redis enabled as the cache driver.');
}
}


Expand Down

0 comments on commit a53e5da

Please sign in to comment.