Download the PHP package foxws/laravel-modelcache without Composer
On this page you can find all versions of the php package foxws/laravel-modelcache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download foxws/laravel-modelcache
More information about foxws/laravel-modelcache
Files in foxws/laravel-modelcache
Package laravel-modelcache
Short Description Cache helpers for Laravel Eloquent models
License MIT
Homepage https://github.com/foxws/laravel-modelcache
Informations about the package laravel-modelcache
Laravel model cache helper
Attach arbitrary cached values to Eloquent model instances or classes using any Laravel cache driver. By default each authenticated user gets an isolated cache namespace, so two users never share the same cached value for the same model.
See the full documentation (or browse Customization.
Installation
Install the package via Composer:
Optionally publish the config file:
Environment variables
| Variable | Default | Description |
|---|---|---|
MODEL_CACHE_ENABLED |
true |
Toggle caching on/off globally |
MODEL_CACHE_STORE |
CACHE_STORE |
Cache store to use (any store from config/cache.php) |
MODEL_CACHE_LIFETIME |
604800 (1 week) |
Default TTL in seconds |
[!TIP] Let your AI agent do the hard parts. This package ships a Laravel Boost skill,
modelcache-development, that teaches your agent how to install, configure and use Laravel Model Cache. Install Boost and select the skill when prompted:Already using Boost? Run
php artisan boost:update --discoverto pick up the skill. Then just ask, for example: "Remember each user's playback position on my Video model."
Usage
1. Add the trait to your model
That is all the setup required. Every public method below becomes available on the model.
2. Instance cache
These methods are scoped to a specific model record (e.g. Video with id = 5).
Store a value:
Retrieve a value:
Check existence:
Forget a value:
Remember a value (fetch or store):
modelCacheRemember returns the cached value if it exists, otherwise resolves the closure (or uses the plain value), stores it, and returns it.
Practical example — lazy-load an expensive computed value:
3. Class cache (global)
These static methods are scoped to the model class rather than a specific record. Useful for values shared across all instances, such as global seeds or configuration.
Store a value:
Retrieve a value:
Check existence:
Forget a value:
4. Facade
Use the ModelCache facade when you need to interact with caching outside of a model — for example in an action class or a service.
5. Controlling which values get cached (shouldModelCache)
Override shouldModelCache on the model to conditionally skip caching certain keys or values:
6. Custom cache profile
A cache profile controls global caching behaviour: whether caching is enabled, when values expire, and which per-user namespace suffix to use. The default is CacheAllSuccessful, which caches all values for all users.
Create your own by implementing CacheProfile:
Register it in config/modelcache.php:
7. Per-model cache namespace (cacheNameSuffix)
By default, BaseCacheProfile::useCacheNameSuffix returns the authenticated user's ID, isolating each user's cache. You can override this per model:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
This package is heavily inspired by and based on the spatie/laravel-responsecache package by Spatie.
If you find their work valuable, please consider sponsoring Spatie or purchasing one of their courses and products.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-modelcache with dependencies
illuminate/cache Version ^12.0|^13.0
illuminate/console Version ^12.0|^13.0
illuminate/contracts Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
spatie/laravel-package-tools Version ^1.9