Skip to content

πŸ“¦ Easily cache and control class methods without having to remember cache key names

License

Notifications You must be signed in to change notification settings

kolirt/laravel-cacheable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel Cacheable

Easily cache and control class methods without having to remember cache key names

Structure

Buy Me A Coffee

Getting started

Easily cache and control class methods without having to remember cache key names

Requirements

  • PHP >= 8.1
  • Laravel >= 10

Installation

composer require kolirt/laravel-cacheable

Setup

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;
}

Console commands

  • cacheable:install - Install cacheable package
  • cacheable:publish-config - Publish the config file

Methods

cache

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);
    }
}

clearCache

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);
    }
}

updateCache

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');
    }
}

refreshCache

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');
    }
}

setCacheTime

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());
    }
}

flushAllCache

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();

appendCacheTags

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();

FAQ

Check closed issues to get answers for most asked questions

License

MIT

Other packages

Check out my other packages on my GitHub profile

About

πŸ“¦ Easily cache and control class methods without having to remember cache key names

Topics

Resources

License

Stars

Watchers

Forks

Languages