Easily maintain a "forever" cache of your models.
cklmercer/laravel-model-stash is a Laravel package for easily maintain a "forever" cache of your models..
It currently has 3 GitHub stars and 24 downloads on Packagist (latest version v0.2.2).
Install it with composer require cklmercer/laravel-model-stash.
Discover more Laravel packages by cklmercer
or browse all Laravel packages to compare alternatives.
Last updated
Easily maintain a "forever" cache of your models.
Disclaimer: This trait is best used on models which you rarely create, update or delete.
composer require cklmercer/laravel-model-stash
Cklmercer\ModelStash\CacheForever within your model.// Role.php
use Cklmercer\ModelStash\CacheForever;
use Illuminate\Database\Eloquent\Model;
class Role extends Models
{
use CacheForever;
// truncated for brevity..
}
Now, whenever you create/update/delete/restore an instance of your model your cache will automatically be updated.
Note: The default cache name will be the plural form of your model's class name.
$roles = \Cache::get('roles')
You can change your model's cache name by defining a $cacheName property on your model.
// Role.php
use Cklmercer\ModelStash\CacheForever;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
use CacheForever;
/**
* The model's cache name.
*
* @var string
*/
protected $cacheName = 'cachedRoles';
// truncated for brevity..
}
$roles = \Cache::get('cachedRoles');
Note: The convention used to get a specific instance is "cache-name:cache-key", with cache-key defaulting to your model's route key.
$role = \Cache::get('roles:1')
You can change your model's cache key by defining a $cacheKey property on your model.
// Role.php
use Cklmercer\LaravelModelStash\CacheForever;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
use CacheForever;
/**
* The model's cache key.
*
* @var string
*/
protected $cacheKey = 'slug';
// truncated for brevity..
}
$role = \Cache::get('roles:admin')