Easily cache and control class methods without having to remember cache key names
Easily cache and control class methods without having to remember cache key names
- PHP >= 8.1
- Laravel >= 10
composer require kolirt/laravel-cacheable
Publish config file
php artisan cacheable:install
By default, Laravel has a problem with multitags, which leads to excessive duplication of data in the cache and unexpected behavior when clearing and reading the cache. You can read more about it here. To fix this issue, add the following code to the composer.json
and run composer dump-autoload
{
"autoload": {
"exclude-from-classmap": [
"vendor/laravel/framework/src/Illuminate/Cache/TaggedCache.php"
],
"files": [
"vendor/kolirt/laravel-cacheable/src/Overrides/TaggedCache.php"
]
}
}
Use the Cacheable
trait in the target class
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
}
cacheable:install
- Install cacheable packagecacheable:publish-config
- Publish the config file
Using the cache
method, cache everything you need
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function exampleMethod()
{
return $this->cache(fn () => 'example data');
}
public function exampleMethodWithParams(int $id)
{
return $this->cache(fn () => 'example data with id ' . $id);
}
}
To clear the cache, use the clearCache
method
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function clearExampleMethod()
{
$this->clearCache('exampleMethod');
}
public function clearExampleMethodWithParams(int $id)
{
$this->clearCache('exampleMethodWithParams', $id);
}
}
To update the cache, use the updateCache
method
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function updateExampleMethod()
{
$this->updateCache('exampleMethod', 'new example data');
}
}
To refresh the cache, use the refreshCache
method
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function exampleMethod() {
return $this->cache(fn () => 'example data');
}
public function refreshExampleMethod()
{
$this->refreshCache('exampleMethod');
}
}
To set the cache time, use the setCacheTime
method
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function __construct()
{
$this->setCacheTime(now()->endOfDay());
}
}
Clearing the all cache works on tags. You have to switch the class to taggable mode
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
protected bool $taggable = true;
}
Or you can add tags to the class by using the appendCacheTags
method without taggable mode
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
public function __construct() {
$this->appendCacheTags(['tag1', 'tag2']);
}
}
To flush all cache, use the flushAllCache
method
$example = new Example();
$example->flushAllCache();
In addition to the basic tag that is added automatically in taggable mode, you can add additional tags that you need using the appendCacheTags
method
use Kolirt\Cacheable\Traits\Cacheable;
class Example
{
use Cacheable;
protected bool $taggable = true;
/** add additional tags for all methods */
public function __construct()
{
$this->appendCacheTags(['tag1', 'tag2']);
}
/** or add additional tags for specific method */
public function exampleMethod()
{
$this->appendCacheTags(['tag1', 'tag2']);
return $this->cache(fn () => 'example data');
}
}
Then, through Cache facade, you can delete the cache for the tag you need
use Illuminate\Support\Facades\Cache;
Cache::tags(['tag1'])->flush();
Cache::tags(['tag2'])->flush();
Cache::tags(['tag1', 'tag2'])->flush();
Check closed issues to get answers for most asked questions
Check out my other packages on my GitHub profile