定制 koine/delayed-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

koine/delayed-cache

最新稳定版本:v1.0.2

Composer 安装命令:

composer require koine/delayed-cache

包简介

Delayed cache for php

README 文档

README

Delayed Cache is a wrapper for Zend Cache. Sometimes you have parallel requests want a cached result that was already started, but is still under construction. Delayed cache will wait until it is ready and then it will return the result for you.

Code information:

Build Status Coverage Status Code Climate Scrutinizer Code Quality

Package information:

Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

Installing

Installing via Composer

Append the lib to your requirements key in your composer.json.

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

Alternative install

Usage

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

Alternatively you can use the short method:

$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

Issues/Features proposals

Here is the issue tracker.

Contributing

Only TDD code will be accepted. Please follow the PSR-2 code standard.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

How to run the tests:

phpunit

To check the code standard run:

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

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

Lincense

MIT

Authors

统计信息

  • 总下载量: 526
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-27