2017 © Pedro Peláez
 

cakephp-plugin cake-variable-cache

A simple config based variable cache plugin for CakePHP 3

image

jonathan-neugber/cake-variable-cache

A simple config based variable cache plugin for CakePHP 3

  • Saturday, June 10, 2017
  • by jonathan-neugber
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

CakePHP 3 cake-variable-cache

License, (*1)

Description

This plugin is designed to asynchronously execute calculations that take a long amount of time and save the results (e.g.: Statistics)., (*2)

It allows you to create a simple list of variables that are to be calculated every n-amount of time., (*3)

It also supports dependent variables (e.g.: variable bar requires variable foo to be calculated first)., (*4)

Installation

1. require the plugin in your composer.json

    "require": {
        ...
        "jonathan-neugber/cake-variable-cache": "dev-master",
        ...
    }

2. Include the plugin using composer

Open a terminal in your project-folder and run these commands:, (*5)

$ composer update
$ composer install

3. Load the plugin in your config/bootstrap.php

Plugin::load('VariableCache', ['bootstrap' => true]);

4. Add configuration to your config/app.php

use Josegonzalez\CakeQueuesadilla\Queue\Queue;
use VariableCache\Lib\Engine\DatabaseCacheProvider;
use VariableCache\Lib\QueuesadillaCallbacks;
use VariableCache\Model\Entity\CachedVariable;
        'VariableCache' => [
            'DataProvider' => [
                'className' => DatabaseCacheProvider::class
            ],
            'Queue' => [
                'callback' => function (CachedVariable $variable) {
                    return Queue::push([
                        QueuesadillaCallbacks::class,
                        'executeJob'
                    ], [
                        'name' => $variable->name
                    ]);
                }
            ],
            'variables' => []
        ]

5. Migrations

Open a terminal in your project-folder and run this command:, (*6)

$ bin/cake migrations migrate --source=../vendor/jonathan-neugber/cake-variable-cache/config/Migrations/

Usage / Example

1. Create a callback

Lets say you want to calculate a statistic:, (*7)

class Statistic
{
    public static function calculateStatistic($name, $amount)
    {
        return 20000 * $amount;
    }
}

2. Create a config

In the VariableCache.variables section of the configuration add the following:, (*8)

'foo' => [
    'callback' => ['\Statistics', 'calculateStatistic'],
    'interval' => '5 minutes',
    'args' => [
        200 // this will be passed as the first argument
    ],
    'variables' => [
        'bar' => [
            'callback' => ['\Statistics', 'calculateStatistic'],
            'interval' => '30 seconds',
            'args' => [
                100
            ]
        ]
    ]
]

3. Import the config

Open a terminal in your project-folder and run this command:, (*9)

$ bin/cake VariableCache.CachedVariables update

This will create the cached variables in the Database., (*10)

4. Run the queues

Execute the following commands in parallel in a terminal in your project-folder and run this command:, (*11)

$ bin/cake VariableCache.CachedVariables

and, (*12)

$ bin/cake queuesadilla

5. Access the variable

$foo = CachedVariableUtility::get('foo');
$foo->content; // value

OR, (*13)

$data = CachedVariableUtility::getMultiple(['foo', 'bar']);

OR, (*14)

// Returns an array with name => value
$data = CachedVariableUtility::getAsKeyValue(['foo', 'bar']);

Additional Information

DynamicCalculationTrait

The DynamicCalculationTrait allows you to easily create a library of callbacks for cached variables. Using DynamicCalculationTrait::calculate() as the callback will automatically call the function calculate<CachedVariableName> in the class., (*15)

Example:
class Statistic
{
    use DynamicCalculationTrait;

    public static function calculateFoo($amount)
    {
        return 20000 * $amount;
    }

    public static function calculateBar($amount)
    {
        return CachedVariableUtility::get('foo')->content * $amount;
    }
}
'foo' => [
    'callback' => ['\Statistics', 'calculate'],
    'interval' => '5 minutes',
    'args' => [
        200 // this will be passed as the first argument
    ],
    'variables' => [
        'bar' => [
            'callback' => ['\Statistics', 'calculate'],
            'interval' => '30 seconds',
            'args' => [
                100
            ]
        ]
    ]
]

Reset

This will reset all main cached variables so that a new execution is required., (*16)

$ bin/cake VariableCache.CachedVariables reset

TODO

  • Write more tests
    • Use mock class for CacheProviderInterface
    • Test Cache Providers separately
  • Update documentation

The Versions

10/06 2017

dev-master

9999999-dev

A simple config based variable cache plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

10/06 2017

v0.1.1

0.1.1.0

A simple config based variable cache plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

10/06 2017

v0.1

0.1.0.0

A simple config based variable cache plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires