Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge c4560d9 into d60a7f2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatosp committed Jan 13, 2016
2 parents d60a7f2 + c4560d9 commit de294db
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Vagrant.configure(2) do |config|
sudo apt-get install php5-curl php5-apcu php5-redis redis-server memcached php5-dev php5-memcached -y
sudo pecl install runkit
sudo sh -c "echo 'apc.enable_cli=1' >> /etc/php5/cli/conf.d/20-apcu.ini"
sudo sh -c "echo 'extension=runkit.so=1' >> /etc/php5/cli/php.ini"
sudo sh -c "echo 'extension=runkit.so' >> /etc/php5/cli/php.ini"
sudo sh -c "echo 'runkit.internal_override=1' >> /etc/php5/cli/php.ini"
SHELL
end
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require-dev": {
"phpunit/phpunit": "5.1.*",
"satooshi/php-coveralls": "~1.0.0",
"cache/integration-tests": "0.3.0",
"cache/integration-tests": "0.*",
"fabpot/php-cs-fixer": "^1.11"
},
"autoload": {
Expand Down
16 changes: 15 additions & 1 deletion src/APCuCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function hasItem($key)
$exists = apcu_exists($key);
}

return isset($this->deferredStack[$key]) || $exists;
return $this->isItemInDeferred($key) || $exists;
}

/**
Expand Down Expand Up @@ -219,6 +219,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if (! $item->isHit()) {
return false;
}

if ($this->legacy) {
$store = apc_store($item->getKey(), serialize($item));
} else {
Expand Down Expand Up @@ -279,4 +283,14 @@ public function __destruct()
{
$this->commit();
}

/**
* @param $key
* @return bool
*/
private function isItemInDeferred($key)
{
// is in stack and not expired
return isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit();
}
}
15 changes: 9 additions & 6 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public function hasItem($key)
{
$this->assertValidKey($key);

return isset($this->deferredStack[$key]) || file_exists($this->filenameFor($key));
$itemInDeferredNotExpired = isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit();

return $itemInDeferredNotExpired || file_exists($this->filenameFor($key));
}

/**
Expand Down Expand Up @@ -176,6 +178,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if ( ! $item->isHit()) {
return false;
}

$bytes = file_put_contents($this->filenameFor($item->getKey()), serialize($item));

return (false !== $bytes);
Expand Down Expand Up @@ -228,12 +234,9 @@ private function filenameFor($key)
*/
private function assertValidKey($key)
{
$invalid = '{}()/\@:';
if (is_string($key) && ! preg_match('/['.preg_quote($invalid, '/').']/', $key)) {
return;
if ( ! Item::isValidKey($key)) {
throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/MemcachedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public function hasItem($key)

$item = $this->getItem($key);

return (isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit()) || $item->isHit();
$itemInDeferredNotExpired = isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit();

return ($itemInDeferredNotExpired) || $item->isHit();
}

/**
Expand Down Expand Up @@ -180,7 +182,7 @@ public function deleteItem($key)
public function deleteItems(array $keys)
{
foreach ($keys as $key) {
if (! Item::isValidKey($key)) {
if ( ! Item::isValidKey($key)) {
throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

Expand All @@ -189,9 +191,11 @@ public function deleteItems(array $keys)

// HHVM memcached doesn't have this method
if (defined('HHVM_VERSION')) {
// @codeCoverageIgnoreStart
foreach ($keys as $key) {
$this->deleteItem($key);
}
// @codeCoverageIgnoreEnd
} else {
$this->cacheClient->deleteMulti($keys);
}
Expand All @@ -210,6 +214,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if ( ! $item->isHit()) {
return false;
}

return $this->cacheClient->add($item->getKey(), serialize($item));
}

Expand Down
18 changes: 12 additions & 6 deletions src/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ public function hasItem($key)
{
$this->assertValidKey($key);

return isset($this->stack[$key]) || isset($this->deferredStack[$key]);
$itemInDeferredNotExpired = isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit();

return (
$itemInDeferredNotExpired ||
(isset($this->stack[$key]) && $this->stack[$key]->isHit())
);
}

/**
Expand Down Expand Up @@ -177,6 +182,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if ( ! $item->isHit()) {
return false;
}

$this->stack[$item->getKey()] = $item;

return true;
Expand Down Expand Up @@ -222,11 +231,8 @@ public function commit()
*/
private function assertValidKey($key)
{
$invalid = '{}()/\@:';
if (is_string($key) && ! preg_match('/['.preg_quote($invalid, '/').']/', $key)) {
return;
if ( ! Item::isValidKey($key)) {
throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}
}
15 changes: 9 additions & 6 deletions src/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public function hasItem($key)
{
$this->assertValidKey($key);

return isset($this->deferredStack[$key]) || $this->redis->exists($key);
$itemInDeferredNotExpired = isset($this->deferredStack[$key]) && $this->deferredStack[$key]->isHit();

return $itemInDeferredNotExpired || $this->redis->exists($key);
}

/**
Expand Down Expand Up @@ -187,6 +189,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if ( ! $item->isHit()) {
return false;
}

return $this->redis->set($item->getKey(), serialize($item));
}

Expand Down Expand Up @@ -232,12 +238,9 @@ public function commit()
*/
private function assertValidKey($key)
{
$invalid = '{}()/\@:';
if (is_string($key) && ! preg_match('/['.preg_quote($invalid, '/').']/', $key)) {
return;
if ( ! Item::isValidKey($key)) {
throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

throw new InvalidArgumentException('invalid key: ' . var_export($key, true));
}

public function __destruct()
Expand Down
4 changes: 4 additions & 0 deletions src/TwoLevelCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public function deleteItems(array $keys)
*/
public function save(CacheItemInterface $item)
{
if ( ! $item->isHit()) {
return false;
}

return ($this->local->save($item) && $this->remote->save($item));
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testConstructorExpiresAfterSeconds()
$this->assertTrue($item->isHit());
}

public function testExpiresAfterDateInterval()
{
$item = new Item('my_key');
$item->set(new stdClass());
$oneDay = DateInterval::createFromDateString('1 day');
$item->expiresAfter($oneDay);
$this->assertTrue($item->isHit());
}

public function testExpiresAtTomorrowIsHit()
{
$item = new Item('my_key');
Expand Down
22 changes: 21 additions & 1 deletion tests/integration/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,29 @@ public function testInvalidInstancesToRedisCache()
$cache = CacheFactory::make(CacheFactory::REDIS, 'this is not a Redis instance');
}

public function testFileCacgeFactory()
public function testFileCacheFactory()
{
$cache = CacheFactory::make(CacheFactory::FILE);
$this->assertInstanceOf('Psr\Cache\CacheItemPoolInterface', $cache);
}

public function testTwoLevelCacheSaveAndFetch()
{
$memory1 = CacheFactory::make(CacheFactory::MEMORY);
$memory2 = CacheFactory::make(CacheFactory::MEMORY);

$cache = CacheFactory::make(CacheFactory::TWO_LEVEL, $memory1, $memory2);

$item = $cache->getItem('key')->set('value');
$cache->save($item);

// assert that both caches have the item
$this->assertTrue($memory1->getItem('key')->isHit());
$this->assertTrue($memory2->getItem('key')->isHit());

// hack and delete on first cache
$memory1->clear();

$this->assertTrue($cache->getItem('key')->isHit());
}
}
2 changes: 1 addition & 1 deletion travis-php.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
extension="redis.so"
extension="memcached.so"
apc.enable_cli = 1
apc.enable_cli = 1

0 comments on commit de294db

Please sign in to comment.