koine/delayed-cache

PHP的延迟缓存

v1.0.2 2015-09-24 11:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:26:48 UTC


README

Delayed Cache 是 Zend Cache 的包装器。有时你会有并行请求需要已经启动但仍在构建中的缓存结果。延迟缓存将等待它准备就绪,然后返回结果给你。

代码信息

Build Status Coverage Status Code Climate Scrutinizer Code Quality

包信息

Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

安装

通过 Composer 安装

将库添加到你的 composer.json 中的 requirements 键。

{
    // composer.json
    // [..]
    require: {
        // append this line to your requirements
        "koine/delayed-cache": "dev-master"
    }
}

替代安装

  • 学习 composer。你不应该寻找替代安装。这值得你的时间。相信我 ;-)
  • 遵循 这些说明

用法

$zendCache = $cache  = \Zend\Cache\StorageFactory::adapterFactory(
    'apc',
    array('ttl' => 3600)
);

$delayedCache = new \Koine\DelayedCache\DelayedCache($zendCache);
// index.php, second 10:00:00 am

$cacheKey = 'veryExpansiveCalculation';

$veryExpansiveCalculation = function () {
    sleep(60);

    return '42';
};

// hasItem returns true in the false time
if (!$delayedCache->hasItem($cacheKey)) {
    $delayedCache->setItem($cacheKey, $veryExpansiveCalculation);
}

$answer = $delayedCache->getItem($cacheKey);
echo 'answer is: ' . $answer;
// index.php, 10:00:10 am

$cacheKey = 'veryExpansiveCalculation';

$veryExpansiveCalculation = function () {
    sleep(60);

    return '42';
};

// although the result is not ready yet, hasItem will return true
if (!$delayedCache->hasItem($cacheKey)) {
    $delayedCache->setItem($cacheKey, $veryExpansiveCalculation);
}

// Waits 50 seconds until the building of the cache is done and then returns
// The $veryExpansiveCalculation callback will not be executed twice, unless the
// cache is cleared
$answer = $delayedCache->getItem($cacheKey);
echo 'answer is: ' . $answer;

或者你可以使用简短的方法

$cacheKey = 'veryExpansiveCalculation';

$veryExpansiveCalculation = function () {
    sleep(60);

    return '42';
};

// if cache is not set, it will set and then return the cached value
$answer = $delayedCache->getCachedItem($cacheKey, $veryExpansiveCalculation);
echo 'answer is: ' . $answer;

When it shines

问题/特性建议

问题跟踪器在这里 Here

贡献

只有 TDD 代码将被接受。请遵循 PSR-2 代码标准

  1. 分支它
  2. 创建你的功能分支 (git checkout -b my-new-feature)
  3. 提交你的更改 (git commit -am 'Add some feature')
  4. 推送到分支 (git push origin my-new-feature)
  5. 创建新的 Pull Request

如何运行测试

phpunit

要检查代码标准,运行

# Fixes code
./bin/php-cs-fix.sh

# outputs error
./bin/php-cs-fix.sh src true
./bin/php-cs-fix.sh test true

许可证

MIT

作者