koine / delayed-cache
Delayed cache for php
v1.0.2
2015-09-24 11:29 UTC
Requires
- php: >=5.5
- zendframework/zend-cache: ~2.5
Requires (Dev)
- fabpot/php-cs-fixer: 1.7.*
- phpunit/phpunit: *
- satooshi/php-coveralls: dev-master
- symfony/console: ~2.7
This package is not auto-updated.
Last update: 2024-12-21 18:45:26 UTC
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:
Package information:
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
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
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;
Issues/Features proposals
Here is the issue tracker.
Contributing
Only TDD code will be accepted. Please follow the PSR-2 code standard.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - 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