jonathan-neugber / cake-variable-cache
为 CakePHP 3 开发的一个基于配置的简单变量缓存插件
v0.1.1
2017-06-10 18:42 UTC
Requires
- php: >=7.1
- cakephp/cakephp: ~3.4
- cakephp/plugin-installer: *
- codekanzlei/cake-cktools: 1.2.*
- josegonzalez/cakephp-queuesadilla: *
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-29 04:03:27 UTC
README
描述
此插件旨在异步执行耗时较长的计算,并保存结果(例如:统计信息)。
它允许您创建一个简单的时间列表,这些变量每隔 n 量时间就要计算一次。
它还支持依赖变量(例如:变量 bar
需要在变量 foo
计算之后才能计算)。
安装
1. 在您的 composer.json
中引入插件
"require": {
...
"jonathan-neugber/cake-variable-cache": "dev-master",
...
}
2. 使用 composer 包含插件
在您的项目文件夹中打开终端并运行以下命令
$ composer update
$ composer install
3. 在您的 config/bootstrap.php
中加载插件
Plugin::load('VariableCache', ['bootstrap' => true]);
4. 在您的 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. 迁移
在您的项目文件夹中打开终端并运行此命令
$ bin/cake migrations migrate --source=../vendor/jonathan-neugber/cake-variable-cache/config/Migrations/
使用示例
1. 创建一个回调
假设您想要计算一个统计信息
class Statistic { public static function calculateStatistic($name, $amount) { return 20000 * $amount; } }
2. 创建一个配置
在配置的 VariableCache.variables
部分添加以下内容
'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. 导入配置
在您的项目文件夹中打开终端并运行此命令
$ bin/cake VariableCache.CachedVariables update
这将创建数据库中的缓存变量。
4. 运行队列
在您的项目文件夹中的终端并行执行以下命令并运行此命令
$ bin/cake VariableCache.CachedVariables
和
$ bin/cake queuesadilla
5. 访问变量
$foo = CachedVariableUtility::get('foo'); $foo->content; // value
或者
$data = CachedVariableUtility::getMultiple(['foo', 'bar']);
或者
// Returns an array with name => value $data = CachedVariableUtility::getAsKeyValue(['foo', 'bar']);
更多信息
DynamicCalculationTrait
DynamicCalculationTrait
允许您轻松创建一个缓存变量的回调库。使用 DynamicCalculationTrait::calculate()
作为回调将自动调用类中的 calculate<CachedVariableName>
函数。
示例
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 ] ] ] ]
重置
这将重置所有主要缓存变量,以便需要新的执行。
$ bin/cake VariableCache.CachedVariables reset
待办事项
- 编写更多测试
- 使用模拟类对 CacheProviderInterface 进行测试
- 单独测试缓存提供者
- 更新文档