PHP code example of eusonlito / laravel-database-cache

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

    

eusonlito / laravel-database-cache example snippets


Articles::latest('published_at')->take(10)->cache()->get();

 declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Eusonlito\DatabaseCache\CacheBuilderTrait;

class User extends Model
{
    use CacheBuilderTrait;

return [
    'enabled' => (bool)env('DATABASE_CACHE_ENABLED', env('CACHE_ENABLED', true)),
    'driver' => env('DATABASE_CACHE_DRIVER', env('CACHE_DRIVER', 'redis')),
    'ttl' => (int)env('DATABASE_CACHE_TTL', env('CACHE_TTL', 3600)),
    'tag' => env('DATABASE_CACHE_TAG', 'database'),
    'prefix' => env('DATABASE_CACHE_PREFIX', 'database|'),
];

use App\Models\Article;

$articles = Article::latest('published_at')->take(10)->cache()->get();

Article::latest('published_at')->take(10)->cache(now()->addHour())->get();

Article::latest('published_at')->take(10)->cache(30, 'latest_articles')->get();

User::whereName('Joe')->whereAge(20)->cache()->first();
User::whereAge(20)->whereName('Joe')->cache()->first();

User::whereName('Joe')->whereAge(20)->cache(60, 'find_joe')->first();
User::whereAge(20)->whereName('Joe')->cache(60, 'find_joe')->first();

$joe = Cache::tags(['database', 'database|users'])->get('find_joe');

// Flush all database cache
Cache::tags('database')->flush();

// Flush only users table cache
Cache::tags('database|users')->flush();