Skip to content

Commit 1c4b9f1

Browse files
authored
Merge pull request #101 from sebastienheyd/master
Cache tagging support
2 parents 83f7a95 + d90bf8a commit 1c4b9f1

10 files changed

+69
-8
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,39 @@ No caching is turned on by default.
302302
A cache key depends on a widget name and each widget parameter.
303303
Override ```cacheKey``` method if you need to adjust it.
304304

305+
### Cache tagging
306+
307+
When tagging is supported ([see the Laravel cache documentation](https://laravel.com/docs/cache#cache-tags)) and to
308+
simplify cache flushing, a tag `widgets` is assigned by default to all widgets.
309+
You can define one or more additional tags to your widgets by setting the values
310+
in the `$cacheTags` property in your widget class. Example :
311+
312+
```php
313+
class RecentNews extends AbstractWidget
314+
{
315+
/**
316+
* Cache tags allow you to tag related items in the cache
317+
* and then flush all cached values that assigned a given tag.
318+
*
319+
* @var array
320+
*/
321+
public $cacheTags = ['news', 'frontend'];
322+
}
323+
```
324+
325+
For this example, if you need to flush :
326+
327+
```php
328+
// Clear widgets with the tag news
329+
Cache::tags('news')->flush();
330+
331+
// Clear widgets with the tag news OR backend
332+
Cache::tags(['news', 'frontend'])->flush();
333+
334+
// Flush all widgets cache
335+
Cache::tags('widgets')->flush();
336+
```
337+
305338
## Widget groups (extra)
306339

307340
In most cases Blade is a perfect tool for setting the position and order of widgets.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"illuminate/contracts": ">=5.1",
1717
"illuminate/view": ">=5.1",
1818
"illuminate/container": ">=5.1",
19-
"illuminate/console": ">=5.1"
19+
"illuminate/console": ">=5.1",
20+
"illuminate/cache": ">=5.1"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "~4.0"

src/AbstractWidget.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ abstract class AbstractWidget
2020
*/
2121
public $cacheTime = false;
2222

23+
/**
24+
* Cache tags allow you to tag related items in the cache and then flush all cached values that assigned a given tag.
25+
*
26+
* @var array
27+
*/
28+
public $cacheTags = [];
29+
2330
/**
2431
* Should widget params be encrypted before sending them to /arrilot/load-widget?
2532
* Turning encryption off can help with making custom reloads from javascript, but makes widget params publicly accessible.
@@ -84,6 +91,16 @@ public function cacheKey(array $params = [])
8491
return 'arrilot.widgets.'.serialize($params);
8592
}
8693

94+
/**
95+
* Cache tags to help flush all cache with the same tag(s).
96+
*
97+
* @return array
98+
*/
99+
public function cacheTags()
100+
{
101+
return array_unique(array_merge(['widgets'], $this->cacheTags));
102+
}
103+
87104
/**
88105
* Add defaults to configuration array.
89106
*

src/Contracts/ApplicationWrapperContract.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ interface ApplicationWrapperContract
1111
*
1212
* @param $key
1313
* @param $minutes
14+
* @param $tags
1415
* @param callable $callback
1516
*
1617
* @return mixed
1718
*/
18-
public function cache($key, $minutes, Closure $callback);
19+
public function cache($key, $minutes, $tags, Closure $callback);
1920

2021
/**
2122
* Wrapper around app()->call().

src/Factories/JavascriptFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getLoader($encryptParams = true)
4747
* Construct javascript code to reload the widget.
4848
*
4949
* @param float|int $timeout
50-
* @param bool $encryptParams
50+
* @param bool $encryptParams
5151
*
5252
* @return string
5353
*/

src/Factories/WidgetFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function getContent()
6767
protected function getContentFromCache($args)
6868
{
6969
if ($cacheTime = (float) $this->getCacheTime()) {
70-
return $this->app->cache($this->widget->cacheKey($args), $cacheTime, function () {
70+
return $this->app->cache($this->widget->cacheKey($args), $cacheTime, $this->widget->cacheTags(), function () {
7171
return $this->getContent();
7272
});
7373
}

src/Misc/EncryptException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class EncryptException extends Exception
88
{
9-
109
}

src/Misc/LaravelApplicationWrapper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ public function __construct()
2626
*
2727
* @param $key
2828
* @param $minutes
29+
* @param $tags
2930
* @param callable $callback
3031
*
3132
* @return mixed
3233
*/
33-
public function cache($key, $minutes, Closure $callback)
34+
public function cache($key, $minutes, $tags, Closure $callback)
3435
{
35-
return $this->app->make('cache')->remember($key, $minutes, $callback);
36+
$cache = $this->app->make('cache');
37+
38+
if (method_exists($cache->getStore(), 'tags')) {
39+
$cache = $cache->tags($tags);
40+
}
41+
42+
return $cache->remember($key, $minutes, $callback);
3643
}
3744

3845
/**

tests/Dummies/TestCachedWidget.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class TestCachedWidget extends AbstractWidget
88
{
99
public $cacheTime = 60;
1010

11+
public $cacheTags = ['test'];
12+
1113
protected $slides = 6;
1214

1315
public function run()

tests/Support/TestApplicationWrapper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ class TestApplicationWrapper implements ApplicationWrapperContract
2525
*
2626
* @param $key
2727
* @param $minutes
28+
* @param $tags
2829
* @param Closure $callback
2930
*
3031
* @return mixed
3132
*/
32-
public function cache($key, $minutes, Closure $callback)
33+
public function cache($key, $minutes, $tags, Closure $callback)
3334
{
3435
return 'Cached output. Key: '.$key.', minutes: '.$minutes;
3536
}

0 commit comments

Comments
 (0)