Skip to content

Instantly share code, notes, and snippets.

@sdiama
Last active October 31, 2022 06:00
Show Gist options
  • Save sdiama/4d0e5c1fcd1faad9dad216c2314d49bd to your computer and use it in GitHub Desktop.
Save sdiama/4d0e5c1fcd1faad9dad216c2314d49bd to your computer and use it in GitHub Desktop.
Laravel macro to add 'tagsIgnored' method at Cache, to add tags only when tags are supported by the driver, without raising error
/*
* Ads method 'tagsIgnored' at Cache
* If tags are not supported by the driver, they will be ignored, instead of raising error
* Use as tags()
* ex. Instead of: Cache::tags(['tag1', 'tag2'])->put('key', 'value', 60);
* use: Cache::tagsIgnored(['tag1', 'tag2'])->put('key', 'value', 60);
*
* Warning: Basic knowledge of Mixins required (https://tutspack.com/the-magic-of-laravel-macro-collection-and-mixins-with-examples/)
*/
use Cache;
class CacheExtensions
{
public function tagsIgnored()
{
return function($inTags) {
if ($this->supportsTags()) {
return $this->tags($inTags);
} else {
return $this;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment