PHP code example of aminshamim / laravel-model-cache

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

    

aminshamim / laravel-model-cache example snippets


'providers' => [
    // Other providers...
    AminShamim\LaravelModelCache\ModelCacheServiceProvider::class,
],



namespace App\Models;

use AminShamim\LaravelModelCache\Models\Traits\ModelCacheable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use ModelCacheable;
    
    // Your model code...
}

// Find with caching
$user = User::findCached(1);

// Find multiple with caching
$users = User::findManyCached([1, 2, 3]);

// Manual caching
$user = User::find(1);
$user->cacheRecord();

// Clear cache
$user->forgetCache();

// Enable query builder caching in your model
protected function getCustomCacheableProperties(): array
{
    return [
        'override_find_method' => true,
    ];
}

// Now all find operations use caching automatically
$user = User::find(1); // Uses cache
$users = User::findMany([1, 2, 3]); // Uses cache

// Additional query builder methods
User::query()->findCached(1);
User::query()->findManyCached([1, 2, 3]);
User::query()->warmCache(100); // Cache first 100 records
User::query()->clearCache(); // Clear all cached records



return [
    /*
    |--------------------------------------------------------------------------
    | Cache Driver
    |--------------------------------------------------------------------------
    | The cache driver to use for model caching. Defaults to your app's
    | default cache driver. Redis is recommended for production.
    */
    'driver' => env('MODEL_CACHE_DRIVER', null),

    /*
    |--------------------------------------------------------------------------
    | Default TTL (Time To Live)
    |--------------------------------------------------------------------------
    | The default cache expiration time in seconds.
    */
    'ttl' => env('MODEL_CACHE_TTL', 300), // 5 minutes

    /*
    |--------------------------------------------------------------------------
    | Cache Key Prefix
    |--------------------------------------------------------------------------
    | Prefix for all cache keys to avoid collisions.
    */
    'prefix' => env('MODEL_CACHE_PREFIX', 'model-cache'),

    /*
    |--------------------------------------------------------------------------
    | Auto Cache Invalidation
    |--------------------------------------------------------------------------
    | Automatically clear cache when models are updated or deleted.
    */
    'auto_invalidate' => env('MODEL_CACHE_AUTO_INVALIDATE', true),

    /*
    |--------------------------------------------------------------------------
    | Override Find Methods
    |--------------------------------------------------------------------------
    | Globally override Eloquent's find methods to use caching.
    */
    'override_find_method' => env('MODEL_CACHE_OVERRIDE_FIND', false),

    /*
    |--------------------------------------------------------------------------
    | Cache Relationships
    |--------------------------------------------------------------------------
    | Whether to cache model relationships by default.
    */
    'cache_relationships' => env('MODEL_CACHE_RELATIONSHIPS', false),

    /*
    |--------------------------------------------------------------------------
    | Performance Monitoring
    |--------------------------------------------------------------------------
    | Enable performance tracking and dynamic TTL adjustment.
    */
    'performance_monitoring' => [
        'enabled' => env('MODEL_CACHE_PERFORMANCE_MONITORING', true),
        'hit_rate_threshold' => 0.8, // Adjust TTL when hit rate is above this
        'ttl_multiplier' => 1.5, // Multiply TTL by this factor for high-performing cache
        'max_ttl' => 3600, // Maximum TTL in seconds
    ],

    /*
    |--------------------------------------------------------------------------
    | Logging Configuration
    |--------------------------------------------------------------------------
    | Configure logging for cache operations.
    */
    'logging' => [
        'enabled' => env('MODEL_CACHE_LOGGING', false),
        'channel' => env('MODEL_CACHE_LOG_CHANNEL', null),
        'level' => env('MODEL_CACHE_LOG_LEVEL', 'debug'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Debug Mode
    |--------------------------------------------------------------------------
    | Enable additional debugging information and logging.
    */
    'debug_mode_enabled' => env('MODEL_CACHE_DEBUG', false),
];

return [
    // How long should individual model records be cached (in seconds)?
    'ttl' => 300,

    // Cache prefix for model records
    'prefix' => 'model-cache',

    // Primary key field name (will be overridden by model's primaryKey)
    'primary_key' => 'id',

    // Whether to enable automatic cache invalidation on model events
    'auto_invalidate' => true,

    // Logging configuration
    'logging' => [
        'enabled' => false,
        'channel' => null, // null = use default channel
        'level' => 'debug',
    ],

    // Cache driver to use (null = use default cache driver)
    'driver' => null,

    // Whether to cache relationships when loading models
    'cache_relationships' => false,

    // Maximum number of cached records per model (0 = unlimited)
    'max_records_per_model' => 0,

    // Whether to override the default find() method with caching
    'override_find_method' => false,
];



namespace App\Models;

use AminShamim\ModelCache\Models\Traits\ModelCacheable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use ModelCacheable;

    protected $table = 'users';
    protected $primaryKey = 'id';
    
    // ... rest of your model
}

// Find a single record with caching
$user = User::findCached(1);

// Find multiple records with caching
$users = User::findManyCached([1, 2, 3, 4, 5]);

// Configure in your model or globally
public function getCacheableProperties(): array
{
    return [
        'override_find_method' => true, // This will make find() use caching
        // ... other properties
    ];
}

// Now regular find() calls will use caching automatically
$user = User::find(1); // This will use caching if override_find_method is true
$users = User::findMany([1, 2, 3]); // This will use caching if configured

$user = User::find(1);

// Cache the record manually
$user->cacheRecord();

// Forget the cache for this record
$user->forgetCache();

// Forget all cache for this model class
User::forgetAllCache();



namespace App\Models;

use AminShamim\ModelCache\Models\Traits\ModelCacheable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use ModelCacheable;

    public function getCacheableProperties(): array
    {
        return [
            'ttl' => 600, // 10 minutes
            'prefix' => 'users-cache',
            'primary_key' => $this->getKeyName(),
            'auto_invalidate' => true,
            'logging' => [
                'enabled' => true,
                'channel' => 'cache',
                'level' => 'info',
            ],
            'driver' => 'redis',
            'cache_relationships' => false,
            'max_records_per_model' => 1000,
            'override_find_method' => true, // Override find() with caching
        ];
    }
}

use AminShamim\ModelCache\Services\ModelCacheService;

// Warm up cache for all users
$cachedCount = ModelCacheService::warmCache(User::class);

// Warm up cache for specific users
$cachedCount = ModelCacheService::warmCache(User::class, [1, 2, 3, 4, 5]);

// Get cache statistics
$stats = ModelCacheService::getCacheStats(User::class);

// Clear cache for specific records
ModelCacheService::clearCacheForRecords(User::class, [1, 2, 3]);

// Optimize cache (remove old records if over limit)
$result = ModelCacheService::optimizeCache(User::class);

'logging' => [
    'enabled' => true,
    'channel' => 'cache',
    'level' => 'debug',
],

protected function getCacheKey(): string
{
    $properties = $this->getCacheableProperties();
    $prefix = $properties['prefix'];
    $modelClass = static::class;
    $primaryKey = $this->getKey();
    
    // Add additional context to cache key
    $context = $this->getCacheContext();
    
    return "{$prefix}:{$modelClass}:{$primaryKey}:{$context}";
}

protected function getCacheContext(): string
{
    // Add any additional context (e.g., user role, language, etc.)
    return auth()->user()?->role ?? 'guest';
}

protected function shouldCache(): bool
{
    // Don't cache if the record is soft deleted
    if ($this->trashed()) {
        return false;
    }
    
    // Don't cache if the user is not active
    if (!$this->is_active) {
        return false;
    }
    
    return parent::shouldCache();
}

use AminShamim\ModelCache\Services\ModelCacheService;

class UserTest extends TestCase
{
    public function test_user_caching()
    {
        $user = User::factory()->create();
        
        // Cache should be created automatically
        $this->assertTrue($user->cacheRecord());
        
        // Find from cache
        $cachedUser = User::findCached($user->id);
        $this->assertEquals($user->id, $cachedUser->id);
        
        // Update should refresh cache
        $user->update(['name' => 'Updated Name']);
        $updatedUser = User::findCached($user->id);
        $this->assertEquals('Updated Name', $updatedUser->name);
    }
}
bash
php artisan vendor:publish --provider="AminShamim\LaravelModelCache\ModelCacheServiceProvider"