PHP code example of mostafaznv / laracache

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

    

mostafaznv / laracache example snippets


    
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Mostafaznv\LaraCache\Traits\LaraCache;
    
    class Article extends Model
    {
        use LaraCache;
        
        /**
         * Define Cache Entities Entities
         *
         * @return CacheEntity[]
         */
        public static function cacheEntities(): array
        {
            return [
                CacheEntity::make('list.forever')
                    ->cache(function() {
                        return Article::query()->latest()->get();
                    }),
   
                CacheEntity::make('latest')
                    ->validForRestOfDay()
                    ->cache(function() {
                        return Article::query()->latest()->first();
                    })
            ];
        }
    }
    

    use App\Models\Article;
    use Mostafaznv\LaraCache\Facades\LaraCache;
   
   
    $cache = Article::cache()->get('latest');
    // or
    $cache = LaraCache::retrieve(Article::class, 'latest');
    

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->disable();
// or 
LaraCache::disable(Article::class);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->enable();
// or 
LaraCache::enable(Article::class);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->update('latest');
// or 
LaraCache::update(Article::class, 'latest');

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->updateAll();
// or 
LaraCache::updateAll(Article::class);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;

 
LaraCache::updateAll();

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->delete('latest');
// or 
LaraCache::delete(Article::class, 'latest');

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->delete('latest', true);
// or 
LaraCache::delete(Article::class, 'latest', true);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->deleteAll();
// or 
LaraCache::deleteAll(Article::class);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->deleteAll(true);
// or 
LaraCache::deleteAll(Article::class, true);

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;

LaraCache::deleteAll();
// forever
LaraCache::deleteAll(forever: true);

# config/laracache.php
return [
    // ...
    'groups' => [
        'group-1' => [
            [
                'model' => \App\Models\User::class,
                'entities' => [
                    'users.latest', 'users.featured'
                ],
            ],
            [
                'model' => \App\Models\Article::class,
                'entities' => [],
            ]
        ],

        'group-2' => [
            [
                'model' => \App\Models\Article::class,
                'entities' => [
                    'featured-list', 'latest'
                ],
            ],
            [
                'model' => \App\Models\User::class,
                'entities' => ['users.latest'],
            ]
        ],
    ]
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mostafaznv\LaraCache\Traits\LaraCache;

class Article extends Model
{
    use LaraCache;
    
    /**
     * Define Cache Entities Entities
     *
     * @return CacheEntity[]
     */
    public static function cacheEntities(): array
    {
        return [
            CacheEntity::make('list.forever')
                ->forever()
                ->setDriver('redis')
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),

            CacheEntity::make('list.day')
                ->isQueueable()
                ->validForRestOfDay()
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),

            CacheEntity::make('list.week')
                ->isQueueable(true, 'redis', 'low')
                ->validForRestOfWeek()
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),

            CacheEntity::make('list.ttl')
                ->ttl(120)
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),

            CacheEntity::make('latest')
                ->forever()
                ->cache(function() {
                    return Article::query()->latest()->first();
                }),

            CacheEntity::make('latest.no-create')
                ->refreshAfterCreate(false)
                ->cache(function() {
                    return Article::query()->latest()->first();
                }),

            CacheEntity::make('latest.no-update')
                ->refreshAfterUpdate(false)
                ->cache(function() {
                    return Article::query()->latest()->first();
                }),

            CacheEntity::make('latest.no-delete')
                ->refreshAfterDelete(false)
                ->cache(function() {
                    return Article::query()->latest()->first();
                }),

            CacheEntity::make('latest.no-restore')
                ->refreshAfterRestore(false)
                ->cache(function() {
                    return Article::query()->latest()->first();
                }),

            CacheEntity::make('empty.array')
                ->setDefault('empty value')
                ->cache(fn() => []),
        ];
    }
}
shell
    php artisan vendor:publish --provider="Mostafaznv\LaraCache\LaraCacheServiceProvider"