Skip to content

Commit b1e39c4

Browse files
committed
Added cache conditional helper
1 parent e313887 commit b1e39c4

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ EventManager::getInstance()->unbindEventCallback('onCacheGetItem', 'myCallbackNa
212212

213213
More information about the implementation and the events available on the [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Introducing-to-events)
214214

215+
---------------------------
216+
Introducing new helpers
217+
---------------------------
218+
:books: As of the V6, PhpFastCache provides some helpers to make your code easier.
219+
220+
- The [ActOnAll Helper](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Act-on-all-instances) to help you to act on multiple instance at once.
221+
- The [CacheConditional Helper](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Cache-Conditional) to help you to make the basic conditional statement more easier.
222+
- The [Psr16 adapter](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Psr16-adapter) to keep it simple as explained above.
223+
224+
May more will come in the future, feel free to contribute !
225+
215226
---------------------------
216227
As Fast To Implement As Opening a Beer
217228
---------------------------
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
*
13+
*/
14+
namespace phpFastCache\Helper;
15+
16+
use Psr\Cache\CacheItemPoolInterface;
17+
18+
/**
19+
* Class CacheConditional
20+
* @package phpFastCache\Helper
21+
*/
22+
class CacheConditionalHelper
23+
{
24+
/**
25+
* @var CacheItemPoolInterface
26+
*/
27+
protected $cacheInstance;
28+
29+
/**
30+
* CachePromise constructor.
31+
* @param CacheItemPoolInterface $cacheInstance
32+
*/
33+
public function __construct(CacheItemPoolInterface $cacheInstance)
34+
{
35+
$this->cacheInstance = $cacheInstance;
36+
}
37+
38+
/**
39+
* @param string $cacheKey
40+
* @param callable $callback
41+
*/
42+
public function get($cacheKey, callable $callback)
43+
{
44+
$cacheItem = $this->cacheInstance->getItem($cacheKey);
45+
46+
if(!$cacheItem->isHit())
47+
{
48+
$cacheItem->set($callback());
49+
$this->cacheInstance->save($cacheItem);
50+
}
51+
52+
return $cacheItem->get();
53+
}
54+
}

tests/CacheConditional.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <contact@geolim4.com>
6+
*/
7+
8+
use phpFastCache\CacheManager;
9+
use phpFastCache\Helper\CacheConditionalHelper as CacheConditional;
10+
use phpFastCache\Helper\TestHelper;
11+
use Psr\Cache\CacheItemPoolInterface;
12+
13+
chdir(__DIR__);
14+
require_once __DIR__ . '/../src/autoload.php';
15+
$testHelper = new TestHelper('Cache Promise');
16+
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
17+
$cacheInstance = CacheManager::getInstance($defaultDriver, []);
18+
$cacheKey = 'cacheKey';
19+
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
20+
21+
22+
/**
23+
* Missing cache item test
24+
*/
25+
$cacheValue = (new CacheConditional($cacheInstance))->get($cacheKey, function() use ($cacheKey, $testHelper, $RandomCacheValue){
26+
/**
27+
* No parameter are passed
28+
* to this closure
29+
*/
30+
$testHelper->printText('Entering in closure as the cache item does not come from the cache backend.');
31+
32+
/**
33+
* Here's your database/webservice/etc stuff
34+
*/
35+
36+
return $RandomCacheValue . '-1337';
37+
});
38+
39+
if($cacheValue === $RandomCacheValue . '-1337'){
40+
$testHelper->printPassText(sprintf('The cache promise successfully returned expected value "%s".', $cacheValue));
41+
}else{
42+
$testHelper->printFailText(sprintf('The cache promise returned an unexpected value "%s".', $cacheValue));
43+
}
44+
45+
/**
46+
* Existing cache item test
47+
*/
48+
$cacheItem = $cacheInstance->getItem($cacheKey);
49+
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
50+
$cacheItem->set($RandomCacheValue);
51+
$cacheInstance->save($cacheItem);
52+
53+
/**
54+
* Remove objects references
55+
*/
56+
$cacheInstance->detachAllItems();
57+
unset($cacheItem);
58+
59+
$cacheValue = (new CacheConditional($cacheInstance))->get($cacheKey, function() use ($cacheKey, $testHelper, $RandomCacheValue){
60+
/**
61+
* No parameter are passed
62+
* to this closure
63+
*/
64+
$testHelper->printFailText('Unexpected closure call.');
65+
return $RandomCacheValue . '-1337';
66+
});
67+
68+
if($cacheValue === $RandomCacheValue){
69+
$testHelper->printPassText(sprintf('The cache promise successfully returned expected value "%s".', $cacheValue));
70+
}else{
71+
$testHelper->printFailText(sprintf('The cache promise returned an unexpected value "%s".', $cacheValue));
72+
}
73+
74+
$cacheInstance->clear();
75+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)