-
Notifications
You must be signed in to change notification settings - Fork 8
add redis cache item pool adapter #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| language: php | ||
|
|
||
| php: | ||
| - 5.6 | ||
| - 5.5 | ||
| - 5.4 | ||
| - 7.1 | ||
| - 7.0 | ||
|
|
||
| before_script: | ||
| - wget http://getcomposer.org/composer.phar | ||
|
|
||
This file contains hidden or 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
185 changes: 185 additions & 0 deletions
185
src/M6Web/Bundle/RedisBundle/CacheAdapters/RedisCacheItemPoolAdapter.php
This file contains hidden or 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,185 @@ | ||
| <?php | ||
| namespace M6Web\Bundle\RedisBundle\CacheAdapters; | ||
|
|
||
| use M6Web\Bundle\RedisBundle\Redis\Redis as BaseRedis; | ||
|
|
||
| use Symfony\Component\Cache\CacheItem; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
| use Psr\Cache\CacheItemInterface; | ||
|
|
||
| /** | ||
| * Class RedisCacheItemPoolAdapter | ||
| */ | ||
| class RedisCacheItemPoolAdapter extends BaseRedis implements CacheItemPoolInterface | ||
| { | ||
| /** | ||
| * @var \Closure | ||
| */ | ||
| private $createCacheItem; | ||
|
|
||
| /** | ||
| * @var \Closure | ||
| */ | ||
| private $getItemLifeTime; | ||
|
|
||
| /** | ||
| * @var bool | ||
| */ | ||
| protected $transactionInProgress = false; | ||
|
|
||
| public function __construct($redis, $defaultLifetime = 0) | ||
| { | ||
| $this->createCacheItem = \Closure::bind( | ||
| function ($key, $value, $isHit, $cacheExpire) use ($defaultLifetime) { | ||
| $cacheItem = new CacheItem(); | ||
| $cacheItem->key = $key; | ||
| $cacheItem->value = $value; | ||
| $cacheItem->isHit = $isHit; | ||
| $cacheItem->defaultLifetime = $defaultLifetime; | ||
|
|
||
| if (is_int($cacheExpire) && $cacheExpire > time()) { | ||
| $cacheItem->expiry = $cacheExpire; | ||
| } | ||
|
|
||
| return $cacheItem; | ||
| }, | ||
| null, | ||
| CacheItem::class | ||
| ); | ||
|
|
||
| $this->getItemLifeTime = \Closure::bind( | ||
| function (CacheItem $item) { | ||
|
|
||
| return $item->expiry; | ||
| }, | ||
| null, | ||
| CacheItem::class | ||
| ); | ||
|
|
||
| return parent::__construct($redis); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $key | ||
| * | ||
| * @return CacheItem | ||
| */ | ||
| public function getItem($key) | ||
| { | ||
| $cacheValue = $this->get($key); | ||
| $cacheExpire = $this->ttl($key); | ||
| $isHit = ($cacheValue !== false) ? 1 : 0; | ||
|
|
||
| $cacheValue = is_string($cacheValue) ? unserialize($cacheValue) : null; | ||
|
|
||
| return ($this->createCacheItem)($key, $cacheValue, $isHit, $cacheExpire); | ||
| } | ||
|
|
||
| /** | ||
| * @param array $keys | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getItems(array $keys = array()) | ||
| { | ||
| $items = new \ArrayIterator(); | ||
| foreach ($keys as $key) { | ||
| $items->append($this->getItem($key)); | ||
| } | ||
|
|
||
| return $items; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $key | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function hasItem($key) | ||
| { | ||
| return $this->has($key); | ||
| } | ||
|
|
||
| /** | ||
| * @return bool | ||
| */ | ||
| public function clear() | ||
| { | ||
| $this->redis->discard(); | ||
| $this->endTransaction(); | ||
|
|
||
| // return true, we don't have the result from BaseRedis method | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $key | ||
| * | ||
| * @return int | ||
| */ | ||
| public function deleteItem($key) | ||
| { | ||
| return $this->remove($key); | ||
| } | ||
|
|
||
| /** | ||
| * @param array $keys | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function deleteItems(array $keys) | ||
| { | ||
| array_walk($keys, [$this, 'remove']); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param CacheItemInterface $item | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function save(CacheItemInterface $item) | ||
| { | ||
| return $this->set($item->getKey(), serialize($item->get()), ($this->getItemLifeTime)($item)); | ||
| } | ||
|
|
||
| /** | ||
| * @param CacheItemInterface $item | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function saveDeferred(CacheItemInterface $item) | ||
| { | ||
| $this->startTransaction(); | ||
| $this->save($item); | ||
|
|
||
| // return true, we don't have the result from BaseRedis method | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @return bool | ||
| */ | ||
| public function commit() | ||
| { | ||
| $this->redis->exec(); | ||
| $this->endTransaction(); | ||
|
|
||
| // return true, we don't have the result from BaseRedis method | ||
| return true; | ||
| } | ||
|
|
||
| protected function startTransaction() | ||
| { | ||
| if (!$this->transactionInProgress) { | ||
| $this->transactionInProgress = true; | ||
| $this->redis->multi(); | ||
| } | ||
| } | ||
|
|
||
| protected function endTransaction() | ||
| { | ||
| $this->transactionInProgress = false; | ||
| } | ||
| } | ||
This file contains hidden or 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,34 @@ | ||
| <?php | ||
| namespace M6Web\Bundle\RedisBundle\Tests\Units; | ||
|
|
||
| use M6Web\Component\RedisMock\RedisMockFactory; | ||
| use mageekguy\atoum; | ||
|
|
||
| class AbstractTest extends atoum\test | ||
| { | ||
| static protected $params = array( | ||
| 'namespace' => '__tt____', | ||
| 'timeout' => 2, | ||
| 'compress' => true, | ||
| 'server_config' => array( | ||
| 'local' => array( | ||
| 'ip' => 'localhost', | ||
| 'port' => 6379, | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| /** | ||
| * get a redis Instance | ||
| * | ||
| * @return BaseRedis | ||
| */ | ||
| protected function getRedisMock() | ||
| { | ||
| $factory = new RedisMockFactory(); | ||
| $myRedisMockClass = $factory->getAdapterClass('M6Web\Component\Redis\Cache', true, true); | ||
| $myRedisMock = new $myRedisMockClass(static::$params, true); | ||
|
|
||
| return $myRedisMock; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/M6Web/Bundle/RedisBundle/Tests/Units/CacheAdapters/RedisCacheItemPoolAdapter.php
This file contains hidden or 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,41 @@ | ||
| <?php | ||
| namespace M6Web\Bundle\RedisBundle\Tests\Units\CacheAdapters; | ||
|
|
||
| use M6Web\Bundle\RedisBundle\Tests\Units\AbstractTest; | ||
|
|
||
| /** | ||
| * Class RedisCacheItemPoolAdapter | ||
| */ | ||
| class RedisCacheItemPoolAdapter extends AbstractTest | ||
| { | ||
| public function testCreateItem() | ||
| { | ||
| $this | ||
| ->given( | ||
| $this->newTestedInstance($this->getRedisMock()), | ||
| $item = $this->testedInstance->getItem('myKey') | ||
| ) | ||
| ->then | ||
| ->object($item) | ||
| ->isInstanceof('Psr\Cache\CacheItemInterface') | ||
| ->variable($item->get()) | ||
| ->isNull() | ||
| ->given( | ||
| $item->set('myValue'), | ||
| $item->expiresAt(new \DateTime('+1 second')), | ||
| $this->testedInstance->save($item) | ||
| ) | ||
| ->then | ||
| ->boolean($this->testedInstance->hasItem('myKey')) | ||
| ->isTrue() | ||
| ->object($item) | ||
| ->isInstanceof('Psr\Cache\CacheItemInterface') | ||
| ->string($item->get()) | ||
| ->isEqualTo('myValue') | ||
| ->if($this->testedInstance->deleteItem('myKey')) | ||
| ->then | ||
| ->boolean($this->testedInstance->hasItem('myKey')) | ||
| ->isFalse() | ||
| ; | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we subclass
CacheItem, instead of using closure binding to access protected properties?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because we can't set the key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$keyis a protected property, so it seems to be designed to be subclassed:Then, instead of doing
You can use
Did I miss something? Seems more clear for me in this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheItem is a final class
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh… Ooooooh…
It seems to be the official way to set the key…
[Edit]
I'm not the only one to find this surprising
And here