Rememberable adds model-level caching to Eloquent models that are frequently resolved by stable lookup keys.
The package hooks into model lifecycle events, stores models in Laravel's cache when they are created or updated, clears cached entries when they are deleted, and exposes convenience accessors for resolving records by id or by any configured alternate key.
In the Enso ecosystem it is used for lookup-oriented models such as countries, categories, measurement units, settings, products, and other entities that benefit from repeated reads by code, name, slug, or external identifiers.
This package is typically installed transitively by Enso packages that rely on cached lookup models.
For standalone installation:
composer require laravel-enso/rememberableThe package auto-registers its service provider and merges the enso.rememberable configuration.
If you want to publish the config locally:
php artisan vendor:publish --tag=rememberable-configThe default configuration is:
return [
'cacheLifetime' => (int) env('CACHE_LIFETIME', 3600),
'keys' => ['id'],
];- Caches models automatically on
createdandupdated. - Removes cached entries automatically on
deleted. - Supports cached lookup by primary key through
cacheGet(). - Supports cached lookup by additional configured keys through
cacheGetBy(). - Lets each model override the cache lifetime.
- Lets each model declare its own rememberable keys.
- Rehydrates cache transparently when a key is not already cached.
- Keeps subclasses isolated because cache keys are built from the static model class.
Apply the trait to an Eloquent model:
use Illuminate\Database\Eloquent\Model;
use LaravelEnso\Rememberable\Traits\Rememberable;
class Company extends Model
{
use Rememberable;
protected $rememberableKeys = ['id', 'name', 'fiscal_code'];
}Resolve a model by primary key:
$company = Company::cacheGet(1);Resolve a model by another configured key:
$company = Company::cacheGetBy('name', 'Earthlink');Customize the cache lifetime per model:
class Product extends Model
{
use Rememberable;
protected $cacheLifetime = 100;
}You can also cache records forever:
class Setting extends Model
{
use Rememberable;
protected $cacheLifetime = 'forever';
}::: warning Note
cacheGetBy() only works for keys declared in the model's rememberableKeys property or in the global enso.rememberable.keys config.
If a key is not allowed, the package throws a LaravelEnso\Rememberable\Exceptions\Rememberable exception.
:::
config/rememberable.php
Keys:
cacheLifetimekeys
LaravelEnso\Rememberable\Traits\Rememberable
Lifecycle hooks:
- caches the model on
created - refreshes the cached model on
updated - removes cached entries on
deleted
Public methods:
cacheGet($id)cacheGetBy(string $key, $value)cachePut()cacheForget()getCacheKey(string $key, $value = null): string
Protected extension points:
$cacheLifetime$rememberableKeysgetCacheLifetime()cacheableKeys()
Cache key format:
<model-class>:<key>:<value>
LaravelEnso\Rememberable\Exceptions\Rememberable
Currently exposes:
missingKey(string $key): self
Framework dependency:
are welcome. Pull requests are great, but issues are good too.
Thank you to all the people who already contributed to Enso!