PHP code example of mtgofa / laravel-query-cache

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

    

mtgofa / laravel-query-cache example snippets



namespace App;

use Illuminate\Database\Eloquent\Model;

use MTGofa\QueryCache\Traits\QueryCachable;

class User extends Model
{
    use QueryCachable;
}


// config('perfectly-cache.(name)')
// Eq: config('perfectly-cache.enabled')
return [

    "enabled" => true, // Is cache enabled? cache for production --> !env('APP_DEBUG', false)

    "minutes" => 1, // Cache minutes.

    /**
     * If this event is triggered on this model,
     * the cache of that table is deleted.
     */
    "clear_events" => [
        "created",
        "updated",
        "deleted",
        "restored"
    ],
];


// Basic cache
$results = \App\Category::find($id);

// Basic cache skip
$results = \App\Category::skipCache()->find($id);

// Basic usage with eager load
$results = \App\Category::with("_list_category_tags")->find($id);

// Basic cache skip usage with eager load
$results = \App\Category::with("^_list_category_tags")->find($id);


    // ->skipCache();
    $result = Category::select("id", "name")->skipCache()->get();

    /**
    * Thanks to the ^ sign, you can prevent your relationships from being cached.
    */
    $results = Category::select("id", "name")->with([
        "^_list_category_tags:id,category_id,name,slug"
    ])->find($id);
    
    /**
    * It will no longer be hidden in the cache for the tag table of the categories.
    */
   


namespace App;

class Category extends BaseModel
{
    /* Cache disabled by this variable */
    protected $isCacheEnable = false;
}



namespace App;


class Module extends BaseModel
{
    protected $table = "modules";

    protected $cacheMinutes = 20; // Now cache time 20 minutes.
}

$modules = \App\Module::remember(10)->select("id", "name")->get();

$modules = \App\Module::with([
    "(10)categories:id,name,module_id"
])->select("id", "name")->get();
// Categories will be cached for 10 minutes.

$module = Module::select("id", "name", "need_cache_reload")->first();
if($module->need_cache_reload) { // simple true value
    $module->reloadCache();
}

$modules2 = Module::select("id", "name")
    ->where("created_time_unix", ">=", time())
    ->get();

php artisan vendor:publish --provider="MTGofa\QueryCache\Providers\QueryCacheServiceProvider"

...
"minutes" => 30,