PHP code example of jonathan-neugber / cake-variable-cache

1. Go to this page and download the library: Download jonathan-neugber/cake-variable-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jonathan-neugber / cake-variable-cache example snippets


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' => []
        ]

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

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

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

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

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

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
            ]
        ]
    ]
]