jonathan-neugber/cake-variable-cache

A simple config based variable cache plugin for CakePHP 3

v0.1.1 2017-06-10 18:42 UTC

This package is not auto-updated.

Last update: 2024-04-14 01:36:08 UTC


README

License

Description

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

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

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

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:

$ 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:

$ 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:

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:

'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:

$ bin/cake VariableCache.CachedVariables update

This will create the cached variables in the Database.

4. Run the queues

Execute the following commands in parallel in a terminal in your project-folder and run this command:

$ bin/cake VariableCache.CachedVariables

and

$ bin/cake queuesadilla

5. Access the variable

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

OR

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

OR

// 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.

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.

$ bin/cake VariableCache.CachedVariables reset

TODO

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