Laravel EnsoLaravel Enso
Guide
Back End
Front End
GitHub
Guide
Back End
Front End
GitHub
  • Back End

    • Action Logger
    • Addresses
    • Algolia
    • Algolia Webshop
    • API
    • Audits
    • Avatars
    • Cache Chain
    • Calendar
    • Cargo Partner
    • Categories
    • Charts
    • CI/CD
    • Cli
    • CNP Validator
    • Comments
    • Commercial
    • Companies
    • Core
    • Countries
    • Currencies
    • Data Export
    • Data Import
    • Discounts
    • Documents
    • Dynamic Methods
    • EAV
    • Emag
    • Enums
    • Excel
    • Facebook
    • Files
    • Filters
    • Financials
    • Forms
    • Frisbo
    • Google
    • Helpers
    • Holidays
    • How-to
    • Image Transformer
    • Impersonate
    • Interactions
    • Inventory
    • IO
    • Localisation
    • Lockable Models
    • Logs
    • Measurement Units
    • Meili Search
    • Meili Search Webshop
    • Menus
    • Migrator
    • Monitored Emails
    • Notifications
    • Packaging Units
    • PDF
    • People
    • Permissions
    • Product Eav
    • Product Lots
    • Products
    • Questionnaires
    • Rememberable
    • Roles
    • Sale Channels
    • Searchable
    • Select
    • Sentry
    • Services
    • Smart Bill
    • Sms Advert
    • Stripe
    • Tables
    • Tasks
    • Teams
    • Ticketing
    • Track Who
    • Tutorials
    • Typesense
    • Typesense Webshop
    • Unit Conversion
    • Upgrade
    • UPS
    • User Groups
    • Users
    • Versions
    • Virtual Call Center
    • Vouchers
    • Webshop
    • Webshop Commercial

Rememberable

LicenseStableDownloadsPHPIssuesMerge Requests

Description

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.

Installation

This package is typically installed transitively by Enso packages that rely on cached lookup models.

For standalone installation:

composer require laravel-enso/rememberable

The 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-config

The default configuration is:

return [
    'cacheLifetime' => (int) env('CACHE_LIFETIME', 3600),
    'keys' => ['id'],
];

Features

  • Caches models automatically on created and updated.
  • 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.

Usage

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';
}

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.

API

Configuration

config/rememberable.php

Keys:

  • cacheLifetime
  • keys

Trait

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
  • $rememberableKeys
  • getCacheLifetime()
  • cacheableKeys()

Cache key format:

<model-class>:<key>:<value>

Exception

LaravelEnso\Rememberable\Exceptions\Rememberable

Currently exposes:

  • missingKey(string $key): self

Depends On

Framework dependency:

  • laravel/framework ↗

Contributions

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

Edit this page on GitHub
Last Updated: 4/19/2026, 7:25:56 PM
Prev
Questionnaires
Next
Roles