PHP code example of lotestudio / model-cache

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

    

lotestudio / model-cache example snippets


Lotestudio\ModelCache\ModelCacheServiceProvider::class,

'ModelCache' => Lotestudio\ModelCache\Facades\ModelCache::class,

return [
    'driver' => env('MODEL_CACHE_DRIVER', env('CACHE_DRIVER', 'file')),
    'ttl' => env('MODEL_CACHE_TTL', 86400), // 24 hours
    'prefix' => env('MODEL_CACHE_PREFIX', 'model_cache_'),

    'models' => [
        'stores' => [
            'model' => App\Models\Store::class,
            'key' => 'stores',
            'ttl' => 86400,
            'order_by' => 'name',
            'order_direction' => 'asc',
            'fields' => ['id', 'name', 'address', 'phone', 'is_active'],
            'conditions' => [
                ['is_active', '=', true],
            ],
        ],
    ],

    'auto_clear' => true,
    'warmup_on_boot' => env('MODEL_CACHE_WARMUP', false),
    'logging' => env('MODEL_CACHE_LOGGING', false),
    'use_tags' => env('MODEL_CACHE_TAGS', false),
    'request_cache' => true,
    'fallback_to_db' => true,
    'id_field' => 'id',
];

use Lotestudio\ModelCache\Facades\ModelCache;

// Get all cached stores
$stores = ModelCache::all('stores');

// Find a specific store by ID
$store = ModelCache::find('stores', 1);

// Find by custom field
$store = ModelCache::findBy('stores', 'email', '[email protected]');

// Find all matching a condition
$activeStores = ModelCache::findAllBy('stores', 'is_active', true);

// Check existence
if (ModelCache::exists('stores', 1)) {
    // ...
}

// Clear cache for a specific model
ModelCache::clear('stores');

// Clear all caches
ModelCache::clearAll();

// Warm up cache
ModelCache::warmup('stores');
ModelCache::warmupAll();

// Equivalent to ModelCache::find('stores', $id)
ModelCache::findStores($id);

// Equivalent to ModelCache::all('stores')
ModelCache::allStores();

// Equivalent to ModelCache::clear('stores')
ModelCache::clearStores();

// Get the cache instance
$cache = model_cache();

// Find a cached model by ID
$store = model_cached('stores', 1);

// Get all cached models
$stores = model_cached_all('stores');

// Find by custom field
$store = model_cached_find_by('stores', 'slug', 'main-store');

// Find all matching
$items = model_cached_find_all_by('stores', 'is_active', true);

// Check existence
$exists = model_cached_exists('stores', 1);

// Clear cache
model_cache_clear('stores');
model_cache_clear_all();

// Warm up cache
model_cache_warmup('stores');
model_cache_warmup_all();

// Get statistics
$stats = model_cache_stats();
$storeStats = model_cache_stats('stores');

// Register a model dynamically
model_cache_register('products', [
    'model' => App\Models\Product::class,
    'key' => 'products',
    'order_by' => 'name',
]);

use Lotestudio\ModelCache\Contracts\ModelCacheInterface;

class StoreController
{
    public function __construct(
        protected ModelCacheInterface $cache
    ) {}

    public function index()
    {
        $stores = $this->cache->all('stores');
        return view('stores.index', compact('stores'));
    }
}
bash

php artisan vendor:publish --provider="Lotestudio\ModelCache\ModelCacheServiceProvider" --tag="model-cache-config"