Skip to content

laravel-enso/rememberable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

130 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rememberable

License Stable Downloads PHP Issues Merge 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';
}

::: 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. :::

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:

Contributions

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

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

Packages

 
 
 

Contributors

Languages