laravel-enso/rememberable
Composer 安装命令:
composer require laravel-enso/rememberable
包简介
Model caching dependency for Laravel Enso
README 文档
README
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
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.
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:
cacheLifetimekeys
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$rememberableKeysgetCacheLifetime()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!
laravel-enso/rememberable 适用场景与选型建议
laravel-enso/rememberable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 64.78k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2017 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel-cache」 「laravel-enso」 「rememberable」 「model-cache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laravel-enso/rememberable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravel-enso/rememberable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laravel-enso/rememberable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Company management dependency for Laravel Enso
Person management for Laravel Enso
Image transformer dependency for Laravel Enso
Search and interval helpers for Laravel Enso
Tutorial video management for Laravel Enso
统计信息
- 总下载量: 64.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 26
- 依赖项目数: 18
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-13