PHP code example of suitmedia / laravel-cacheable

1. Go to this page and download the library: Download suitmedia/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/ */

    

suitmedia / laravel-cacheable example snippets


'providers' => [
    // ...
    \Suitmedia\Cacheable\ServiceProvider::class,
];

'aliases' => [
    // ...
    'Cacheable' => \Suitmedia\Cacheable\Facade::class,
];

return [

    /*
    |--------------------------------------------------------------------------
    | Custom Tags
    |--------------------------------------------------------------------------
    |
    | Define all of Eloquent Models which should add custom cache tags
    | automatically to the cached objects.
    |
    */

    'customTags' => \App\User::class,

    /*
    |--------------------------------------------------------------------------
    | Default Cache Duration
    |--------------------------------------------------------------------------
    |
    | Define the default cache duration here.
    | Setting the cache duration to '0' will make the cache lasts forever.
    |
    */

    'duration' => 0,

    /*
    |--------------------------------------------------------------------------
    | Methods which shouldn't be cached
    |--------------------------------------------------------------------------
    |
    | Define a collection of method names which you don't wish
    | to be cached.
    |
    */

    'except' => [
        'cacheDuration',
        'cacheExcept',
        'cacheKey',
        'cacheTags',
        'create',
        'delete',
        'restore',
        'update',
        'updateOrCreate',
    ],
];

namespace App;

use Illuminate\Database\Eloquent\Model;
use Suitmedia\Cacheable\Contracts\CacheableModel;
use Suitmedia\Cacheable\Traits\Model\CacheableTrait;

class Article extends Model implements CacheableModel
{
    use CacheableTrait;
}

namespace App\Repositories;

use App\Article;
use Suitmedia\Cacheable\Traits\Repository\CacheableTrait;
use Suitmedia\Cacheable\Contracts\CacheableRepository;

class ArticleRepository implements CacheableRepository
{
    use CacheableTrait;

    private $article;

    public function __construct(Article $article)
    {
        $this->article = $article;
    }

    public function model()
    {
        return $this->article;
    }

    /*
    |--------------------------------------------------------------------------
    | Repository's method definition starts from here
    |--------------------------------------------------------------------------
    */

    public function all()
    {
        return $this->model->all();
    }

    public function find($id)
    {
        return $this->model->find($videoId);
    }
}

// Lets decorate the repository first
$repo = Cacheable::wrap(ArticleRepository::class);

// The result of these codes will be cached automatically
$repo->all();
$repo->find(1);

// Flush everything
Cacheable::flush();

// Flush the cache using a specific tag
Cacheable::flush('Article');

// Flush the cache using a specific tag,
// and only for the cache which belongs to a specific user
Cacheable::flush('LikedArticle:User:7');