PHP code example of foxws / laravel-modelcache

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

    

foxws / laravel-modelcache example snippets


use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    use InteractsWithModelCache;
}

Video::first()->modelCache('currentTime', 20);
Video::first()->modelCache('randomSeed', 20, now()->addDay()); // cache for one day

Video::first()->modelCached('currentTime');
Video::first()->modelCached('randomSeed', $default); // with fallback

Video::first()->modelCacheForget('currentTime');
Video::first()->modelCacheForget('randomSeed');

Video::modelClassCache('randomSeed', 0.1);
Video::modelClassCache('randomSeed', 0.1, now()->addDay()); // cache for one day

Video::modelClassCached('randomSeed');
Video::modelClassCached('randomSeed', $default);

Video::modelClassCacheForget('randomSeed');

use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    use InteractsWithModelCache;

    /**
     * @doc When using a overule, it doesn't create a separated cache by default for authenticated users.
     */
    protected function cacheNameSuffix(string $key): string
    {
        return "{$key}:my-modelcache-prefix";
    }
}
bash
php artisan vendor:publish --tag="modelcache-config"