-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storing items into previous cache pool (#158)
* fix merging loaded items, when return type of getItems method from chained pool is Generator instead of array add unsetting loaded key from next chain pool loading for better performance fix psr2 code style * change array_merge($loadedItems, $hits); * add storing items into previous cache pool in getItems method * change code style * change code style * added test for CachePoolChain * change code style * added testGetItemsWithEmptyCache
- Loading branch information
Showing
2 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache organization. | ||
* | ||
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\Adapter\Chain\Tests; | ||
|
||
use Cache\Adapter\Chain\CachePoolChain; | ||
use Cache\Adapter\Common\CacheItem; | ||
use Cache\Adapter\PHPArray\ArrayCachePool; | ||
|
||
/** | ||
* Class ChainPoolTest. | ||
*/ | ||
class CachePoolChainTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGetItemStoreToPrevious() | ||
{ | ||
$firstPool = new ArrayCachePool(); | ||
$secondPool = new ArrayCachePool(); | ||
$chainPool = new CachePoolChain([$firstPool, $secondPool]); | ||
|
||
$key = 'test_key'; | ||
$item = new CacheItem($key, true, 'value'); | ||
$item->expiresAfter(60); | ||
$secondPool->save($item); | ||
|
||
$loadedItem = $firstPool->getItem($key); | ||
$this->assertFalse($loadedItem->isHit()); | ||
|
||
$loadedItem = $secondPool->getItem($key); | ||
$this->assertTrue($loadedItem->isHit()); | ||
|
||
$loadedItem = $chainPool->getItem($key); | ||
$this->assertTrue($loadedItem->isHit()); | ||
|
||
$loadedItem = $firstPool->getItem($key); | ||
$this->assertTrue($loadedItem->isHit()); | ||
} | ||
|
||
public function testGetItemsStoreToPrevious() | ||
{ | ||
$firstPool = new ArrayCachePool(); | ||
$secondPool = new ArrayCachePool(); | ||
$chainPool = new CachePoolChain([$firstPool, $secondPool]); | ||
|
||
$key = 'test_key'; | ||
$item = new CacheItem($key, true, 'value'); | ||
$item->expiresAfter(60); | ||
$secondPool->save($item); | ||
$firstExpirationTime = $item->getExpirationTimestamp(); | ||
|
||
$key2 = 'test_key2'; | ||
$item = new CacheItem($key2, true, 'value2'); | ||
$item->expiresAfter(60); | ||
$secondPool->save($item); | ||
$secondExpirationTime = $item->getExpirationTimestamp(); | ||
|
||
$loadedItem = $firstPool->getItem($key); | ||
$this->assertFalse($loadedItem->isHit()); | ||
|
||
$loadedItem = $firstPool->getItem($key2); | ||
$this->assertFalse($loadedItem->isHit()); | ||
|
||
$loadedItem = $secondPool->getItem($key); | ||
$this->assertTrue($loadedItem->isHit()); | ||
|
||
$loadedItem = $secondPool->getItem($key2); | ||
$this->assertTrue($loadedItem->isHit()); | ||
|
||
$items = $chainPool->getItems([$key, $key2]); | ||
|
||
$this->assertArrayHasKey($key, $items); | ||
$this->assertArrayHasKey($key2, $items); | ||
|
||
$this->assertTrue($items[$key]->isHit()); | ||
$this->assertTrue($items[$key2]->isHit()); | ||
|
||
$loadedItem = $firstPool->getItem($key); | ||
$this->assertTrue($loadedItem->isHit()); | ||
$this->assertEquals($firstExpirationTime, $loadedItem->getExpirationTimestamp()); | ||
|
||
$loadedItem = $firstPool->getItem($key2); | ||
$this->assertTrue($loadedItem->isHit()); | ||
$this->assertEquals($secondExpirationTime, $loadedItem->getExpirationTimestamp()); | ||
} | ||
|
||
public function testGetItemsWithEmptyCache() | ||
{ | ||
$firstPool = new ArrayCachePool(); | ||
$secondPool = new ArrayCachePool(); | ||
$chainPool = new CachePoolChain([$firstPool, $secondPool]); | ||
|
||
$key = 'test_key'; | ||
$key2 = 'test_key2'; | ||
|
||
$items = $chainPool->getItems([$key, $key2]); | ||
|
||
$this->assertArrayHasKey($key, $items); | ||
$this->assertArrayHasKey($key2, $items); | ||
|
||
$this->assertFalse($items[$key]->isHit()); | ||
$this->assertFalse($items[$key2]->isHit()); | ||
} | ||
} |