PHPackages                             jonathan-neugber/cake-variable-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jonathan-neugber/cake-variable-cache

ActiveCakephp-plugin

jonathan-neugber/cake-variable-cache
====================================

A simple config based variable cache plugin for CakePHP 3

v0.1.1(8y ago)01MITPHPPHP &gt;=7.1

Since Jun 10Pushed 8y ago1 watchersCompare

[ Source](https://github.com/jonathan-neugber/cake-variable-cache)[ Packagist](https://packagist.org/packages/jonathan-neugber/cake-variable-cache)[ RSS](/packages/jonathan-neugber-cake-variable-cache/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

CakePHP 3 cake-variable-cache
=============================

[](#cakephp-3-cake-variable-cache)

[![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)

Description
-----------

[](#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
------------

[](#installation)

#### 1. require the plugin in your `composer.json`

[](#1-require-the-plugin-in-your-composerjson)

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

```

#### 2. Include the plugin using composer

[](#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`

[](#3-load-the-plugin-in-your-configbootstrapphp)

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

```

#### 4. Add configuration to your `config/app.php`

[](#4-add-configuration-to-your-configappphp)

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

[](#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
---------------

[](#usage--example)

#### 1. Create a callback

[](#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

[](#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

[](#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

[](#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

[](#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
----------------------

[](#additional-information)

#### DynamicCalculationTrait

[](#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` in the class.

##### Example:

[](#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

[](#reset)

This will reset all main cached variables so that a new execution is required.

```
$ bin/cake VariableCache.CachedVariables reset

```

TODO
----

[](#todo)

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

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3260d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ca1dbb6ed1d663949e32a423f817513cf382994aae658267e173bc3dfeb5f497?d=identicon)[jonathan-neugber](/maintainers/jonathan-neugber)

---

Top Contributors

[![jonathan-neugber](https://avatars.githubusercontent.com/u/17924468?v=4)](https://github.com/jonathan-neugber "jonathan-neugber (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jonathan-neugber-cake-variable-cache/health.svg)

```
[![Health](https://phpackages.com/badges/jonathan-neugber-cake-variable-cache/health.svg)](https://phpackages.com/packages/jonathan-neugber-cake-variable-cache)
```

###  Alternatives

[friendsofcake/cakepdf

CakePHP plugin for creating and/or rendering Pdfs, several Pdf engines supported.

3752.1M3](/packages/friendsofcake-cakepdf)[cakephp/app

CakePHP skeleton app

3831.7M1](/packages/cakephp-app)[cakephp/bake

Bake plugin for CakePHP

11211.2M158](/packages/cakephp-bake)[dereuromark/cakephp-tools

A CakePHP plugin containing lots of useful and reusable tools

338920.1k32](/packages/dereuromark-cakephp-tools)[dereuromark/cakephp-queue

The Queue plugin for CakePHP provides deferred task execution.

308850.3k14](/packages/dereuromark-cakephp-queue)[dereuromark/cakephp-ide-helper

CakePHP IdeHelper Plugin to improve auto-completion

1862.1M27](/packages/dereuromark-cakephp-ide-helper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
