PHP code example of kolirt / laravel-cacheable

1. Go to this page and download the library: Download kolirt/laravel-cacheable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kolirt / laravel-cacheable example snippets


use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
}

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

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function clearExampleMethod() 
    {
        $this->clearCache('exampleMethod');
    }

    public function clearExampleMethodWithParams(int $id) 
    {
        $this->clearCache('exampleMethodWithParams', $id);
    }
}

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function updateExampleMethod() 
    {
        $this->updateCache('exampleMethod', 'new example data');
    }
}

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function exampleMethod() {
        return $this->cache(fn () => 'example data');
    }

    public function refreshExampleMethod() 
    {
        $this->refreshCache('exampleMethod');
    }
}

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function __construct()
    {
        $this->setCacheTime(now()->endOfDay());
    }
}

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
    
    protected bool $taggable = true;
}

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
    
    public function __construct() {
        $this->appendCacheTags(['tag1', 'tag2']);
    }
}

$example = new Example();
$example->flushAllCache();

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

use Illuminate\Support\Facades\Cache;

Cache::tags(['tag1'])->flush();
Cache::tags(['tag2'])->flush();
Cache::tags(['tag1', 'tag2'])->flush();
bash
php artisan cacheable:install
json
{
    "autoload": {
        "exclude-from-classmap": [
            "vendor/laravel/framework/src/Illuminate/Cache/TaggedCache.php"
        ],
        "files": [
            "vendor/kolirt/laravel-cacheable/src/Overrides/TaggedCache.php"
        ]
    }
}