riesenia/cakephp-fetchable

CakePHP ORM 插件,用于从缓存中获取实体

安装次数: 22,474

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:cakephp-plugin

v2.0.1 2019-12-26 01:29 UTC

This package is auto-updated.

Last update: 2024-09-20 14:55:47 UTC


README

Build Status Latest Version Total Downloads Software License

此插件适用于 CakePHP 3.x,包含处理从缓存/内存存储中获取实体的行为。适用于包含中等数量行且在应用程序的许多部分中常用的表。

安装

使用 composer

composer require riesenia/cakephp-fetchable

用法

此行为适用于包含中等数量行且在应用程序的许多部分中常用的表。Fetchable 会检查它们是否已经缓存,并使用 Configure 类将它们存储在内存中。这减少了它们的数据库查询次数。

class StatusesTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->addBehavior('Fetchable.Fetchable', [
            // can use custom finder
            'finder' => 'active',
            // cache config
            'cache' => 'statuses_cache',
            // can contain another data
            'contain' => ['StatusProperties'],
            // if i.e. status name is translatable
            'key' => function ($name) {
                return $name . '-' . I18n::getLocale();
            }
        ]);
    }
}

// fetch all active statuses
$this->Statuses->fetch();

配置选项

  • finder - 用于获取实体的查找器。默认为 "all"。
  • cache - 要使用的缓存配置。默认为 "default"。
  • contain - 设置要获取的相关实体。
  • key - 用于 CacheConfigure 调用的键。可以设置为可调用(即对于 I18n 依赖的数据)。