riesenia/cakephp-fetchable

CakePHP ORM plugin for fetching entities from cache

Installs: 23 075

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:cakephp-plugin

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

This package is auto-updated.

Last update: 2024-11-20 15:18:11 UTC


README

Build Status Latest Version Total Downloads Software License

This plugin is for CakePHP 3.x and contains behavior that handles fetching entities from cache / memory storage. Relevant for tables that contain moderate number of rows and are used commonly in many parts of application.

Installation

Using composer

composer require riesenia/cakephp-fetchable

Usage

This behavior is suitable for tables that contain moderate number of rows and are used commonly in many parts of application. Fetchable checks if they are already cached and also stores them in memory using Configure class. This lowers the number of database queries for them.

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();

Configuration options:

  • finder - finder to use to get entities. Defaults to "all".
  • cache - cache config to use. Defaults to "default".
  • contain - set related entities that will be fetched.
  • key - key used for Cache and Configure calls. Can be set to callable (i.e. for I18n dependend data).