### Stack Trace Example Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/ISSUE_TEMPLATE.md Paste the relevant, complete stack trace here when reporting an issue. ```text *paste the relevant, complete stack trace here* ``` -------------------------------- ### Install laravel-model-caching Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Install the package using Composer. The service provider is auto-discovered, requiring no additional setup. ```bash composer require genealabs/laravel-model-caching ``` -------------------------------- ### Manual Caching Example (Without Package) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Demonstrates the manual approach to caching Eloquent query results, including the need for manual cache key management and invalidation. ```php $posts = Cache::remember('posts:active:page:1', 3600, function () { return Post::where('active', true)->with('comments')->paginate(); }); // And in every observer or event listener… Cache::forget('posts:active:page:1'); // Hope you remembered every key variant! 😅 ``` -------------------------------- ### Install AWS SDK for PHP Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md If your application doesn't already use the AWS SDK, install it using Composer. This is required for the DynamoDB cache store to function. ```sh composer require aws/aws-sdk-php ``` -------------------------------- ### Automatic Caching Example (With Package) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Shows how to use the package for automatic caching. Simply add the trait to your model and query normally; caching and invalidation are handled automatically. ```php // Just query. Caching and invalidation happen automatically. ✨ $posts = Post::where('active', true)->with('comments')->paginate(); ``` -------------------------------- ### Real-World Example: Blog Posts with Relationships Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Demonstrates caching for a Post model with eager-loaded comments and tags. Cache invalidation is automatic when related data changes, such as a new comment being created. ```php class Post extends BaseModel { public function comments() { return $this->hasMany(Comment::class); } public function tags() { return $this->belongsToMany(Tag::class); } } // All cached automatically — the query, the eager loads, everything. 🪄 $posts = Post::with('comments', 'tags') ->where('published', true) ->latest() ->paginate(15); ``` -------------------------------- ### Disable Model Caching in Tests Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md To disable model caching entirely during tests, set the 'enabled' configuration key to false. This can be done in your TestCase setUp() method or within your phpunit.xml configuration file. ```php config(['laravel-model-caching.enabled' => false]); ``` -------------------------------- ### Publish Configuration File Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Use this Artisan command to publish the configuration file for the model caching package. ```sh php artisan modelCache:publish --config ``` -------------------------------- ### Use Array Cache Driver for Testing Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md For testing cache behavior, configure a dedicated 'model-test' cache store using the 'array' driver. Then, set the package to use this store by updating the 'store' configuration key. ```php config(['cache.stores.model-test' => ['driver' => 'array']]); config(['laravel-model-caching.store' => 'model-test']); ``` -------------------------------- ### Set Global Cache Key Prefix Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Configure a global cache key prefix in your configuration to isolate cache entries for multi-tenant applications. ```php 'cache-prefix' => 'tenant-123', ``` -------------------------------- ### Manual Cache Flushing via Artisan Command (All Models) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Execute `php artisan modelCache:clear` without any arguments to flush the cache for all models. Be aware of the scope of this command depending on your cache driver. ```sh php artisan modelCache:clear ``` -------------------------------- ### Set Custom Cache Store via Environment Variable Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md To use a dedicated cache store for model caching, define it in `config/cache.php` and reference it using this environment variable. ```env MODEL_CACHE_STORE=model-cache ``` -------------------------------- ### Define DynamoDB Cache Store in config/cache.php Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Configure the 'dynamodb-model' cache store in your `config/cache.php` file. This specifies the driver and connection details for DynamoDB. ```php 'stores' => [ 'dynamodb-model' => [ 'driver' => 'dynamodb', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'table' => env('AWS_DYNAMODB_CACHE_TABLE', 'cache'), 'endpoint' => env('AWS_DYNAMODB_CACHE_ENDPOINT'), 'attributes' => [ 'key' => 'key', 'value' => 'value', 'expiration' => 'expires_at', ], ], ], ``` -------------------------------- ### Manual Cache Flushing via Artisan Command (Single Model) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Use the `modelCache:clear` Artisan command with the `--model` option to flush the cache for a specific model. ```sh php artisan modelCache:clear --model='App\Models\Post' ``` -------------------------------- ### Manual Cache Flushing via Facade (Multiple Models) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Use `ModelCache::invalidate()` with an array of model class names to programmatically flush the cache for multiple models simultaneously. ```php use GeneaLabs\LaravelModelCaching\Facades\ModelCache; // Multiple models ModelCache::invalidate([ App\Models\Post::class, App\Models\Comment::class, ]); ``` -------------------------------- ### Extend CachedModel Directly Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Alternatively, you can extend the included CachedModel directly if you do not need a custom base model. This provides caching capabilities out-of-the-box. ```php get(); ``` ```php // Or override with a specific duration Comment::withCacheCooldownSeconds(30)->get(); ``` -------------------------------- ### Default Configuration File Content Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md This is the content of the default `config/laravel-model-caching.php` file. It shows the available configuration options and their default values. ```php return [ 'cache-prefix' => '', 'enabled' => env('MODEL_CACHE_ENABLED', true), 'use-database-keying' => env('MODEL_CACHE_USE_DATABASE_KEYING', true), 'store' => env('MODEL_CACHE_STORE'), 'fallback-to-database' => env('MODEL_CACHE_FALLBACK_TO_DB', false), ]; ``` -------------------------------- ### Configure DynamoDB Cache Store Environment Variables Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Set these environment variables to configure the DynamoDB cache store. Ensure you replace placeholders with your actual AWS credentials and region. ```env MODEL_CACHE_STORE=dynamodb-model AWS_ACCESS_KEY_ID=your-access-key AWS_SECRET_ACCESS_KEY=your-secret-key AWS_DEFAULT_REGION=us-east-1 AWS_DYNAMODB_CACHE_ENDPOINT= AWS_DYNAMODB_CACHE_TABLE=cache ``` -------------------------------- ### Manual Cache Flushing via Facade (Single Model) Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Use `ModelCache::invalidate()` with the model's class name to programmatically flush the cache for a single model. ```php use GeneaLabs\LaravelModelCaching\Facades\ModelCache; // Single model ModelCache::invalidate(App\Models\Post::class); ``` -------------------------------- ### Set Per-Model Cache Key Prefix Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Define a `cachePrefix` property on your Eloquent model to set a specific cache prefix for that model, useful in multi-tenant scenarios. ```php */ class Post extends Model { use Cachable; } ``` -------------------------------- ### Disable Cache Globally via Environment Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Set `MODEL_CACHE_ENABLED` to `false` in your environment file to disable caching application-wide. ```env MODEL_CACHE_ENABLED=false ``` -------------------------------- ### Declare Cache Cool-Down Period on Model Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Define `cacheCooldownSeconds` on your model to set the default duration for the cache cool-down period. This property alone does not activate the cool-down. ```php runDisabled(function () { return MyModel::get(); }); ``` ```php use GeneaLabs\LaravelModelCaching\Facades\ModelCache; ModelCache::runDisabled(function () { return MyModel::get(); }); ``` -------------------------------- ### Disable Cache Per-Query Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Use `disableCache()` on a query chain to affect only that specific query. This is useful in seeders to prevent stale data. ```php $results = MyModel::disableCache()->where('active', true)->get(); ``` -------------------------------- ### Add Cachable Trait to Base Model Source: https://github.com/mike-bronner/laravel-model-caching/blob/master/README.md Extend Eloquent's Model and use the Cachable trait to enable caching for all models that extend this base model. This is the recommended approach for consistent caching across your application. ```php