Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Apr 9, 2018
2 parents 5a5ea64 + 1c4b9f1 commit 839f5c0
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 8 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,39 @@ No caching is turned on by default.
A cache key depends on a widget name and each widget parameter.
Override ```cacheKey``` method if you need to adjust it.

### Cache tagging

When tagging is supported ([see the Laravel cache documentation](https://laravel.com/docs/cache#cache-tags)) and to
simplify cache flushing, a tag `widgets` is assigned by default to all widgets.
You can define one or more additional tags to your widgets by setting the values
in the `$cacheTags` property in your widget class. Example :

```php
class RecentNews extends AbstractWidget
{
/**
* Cache tags allow you to tag related items in the cache
* and then flush all cached values that assigned a given tag.
*
* @var array
*/
public $cacheTags = ['news', 'frontend'];
}
```

For this example, if you need to flush :

```php
// Clear widgets with the tag news
Cache::tags('news')->flush();

// Clear widgets with the tag news OR backend
Cache::tags(['news', 'frontend'])->flush();

// Flush all widgets cache
Cache::tags('widgets')->flush();
```

## Widget groups (extra)

In most cases Blade is a perfect tool for setting the position and order of widgets.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"illuminate/contracts": ">=5.1",
"illuminate/view": ">=5.1",
"illuminate/container": ">=5.1",
"illuminate/console": ">=5.1"
"illuminate/console": ">=5.1",
"illuminate/cache": ">=5.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand Down
17 changes: 17 additions & 0 deletions src/AbstractWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ abstract class AbstractWidget
*/
public $cacheTime = false;

/**
* Cache tags allow you to tag related items in the cache and then flush all cached values that assigned a given tag.
*
* @var array
*/
public $cacheTags = [];

/**
* Should widget params be encrypted before sending them to /arrilot/load-widget?
* Turning encryption off can help with making custom reloads from javascript, but makes widget params publicly accessible.
Expand Down Expand Up @@ -84,6 +91,16 @@ public function cacheKey(array $params = [])
return 'arrilot.widgets.'.serialize($params);
}

/**
* Cache tags to help flush all cache with the same tag(s).
*
* @return array
*/
public function cacheTags()
{
return array_unique(array_merge(['widgets'], $this->cacheTags));
}

/**
* Add defaults to configuration array.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/ApplicationWrapperContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ interface ApplicationWrapperContract
*
* @param $key
* @param $minutes
* @param $tags
* @param callable $callback
*
* @return mixed
*/
public function cache($key, $minutes, Closure $callback);
public function cache($key, $minutes, $tags, Closure $callback);

/**
* Wrapper around app()->call().
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/JavascriptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getLoader($encryptParams = true)
* Construct javascript code to reload the widget.
*
* @param float|int $timeout
* @param bool $encryptParams
* @param bool $encryptParams
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/WidgetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function getContent()
protected function getContentFromCache($args)
{
if ($cacheTime = (float) $this->getCacheTime()) {
return $this->app->cache($this->widget->cacheKey($args), $cacheTime, function () {
return $this->app->cache($this->widget->cacheKey($args), $cacheTime, $this->widget->cacheTags(), function () {
return $this->getContent();
});
}
Expand Down
1 change: 0 additions & 1 deletion src/Misc/EncryptException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class EncryptException extends Exception
{

}
11 changes: 9 additions & 2 deletions src/Misc/LaravelApplicationWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ public function __construct()
*
* @param $key
* @param $minutes
* @param $tags
* @param callable $callback
*
* @return mixed
*/
public function cache($key, $minutes, Closure $callback)
public function cache($key, $minutes, $tags, Closure $callback)
{
return $this->app->make('cache')->remember($key, $minutes, $callback);
$cache = $this->app->make('cache');

if (method_exists($cache->getStore(), 'tags')) {
$cache = $cache->tags($tags);
}

return $cache->remember($key, $minutes, $callback);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Dummies/TestCachedWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TestCachedWidget extends AbstractWidget
{
public $cacheTime = 60;

public $cacheTags = ['test'];

protected $slides = 6;

public function run()
Expand Down
3 changes: 2 additions & 1 deletion tests/Support/TestApplicationWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class TestApplicationWrapper implements ApplicationWrapperContract
*
* @param $key
* @param $minutes
* @param $tags
* @param Closure $callback
*
* @return mixed
*/
public function cache($key, $minutes, Closure $callback)
public function cache($key, $minutes, $tags, Closure $callback)
{
return 'Cached output. Key: '.$key.', minutes: '.$minutes;
}
Expand Down

0 comments on commit 839f5c0

Please sign in to comment.