承接 laravel-enso/rememberable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-enso/rememberable

Composer 安装命令:

composer require laravel-enso/rememberable

包简介

Model caching dependency for Laravel Enso

README 文档

README

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!

统计信息

  • 总下载量: 64.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 0
  • 依赖项目数: 18
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 5
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固