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

Fixes cache entry deletion directly after creation #184

Merged
merged 5 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public function set($key, $value, $ttl = null)

$options = $this->storage->getOptions();
$previousTtl = $options->getTtl();
$options->setTtl($ttl);

if ($ttl !== null) {
$options->setTtl($ttl);
}

try {
$result = $this->storage->setItem($key, $value);
Expand Down Expand Up @@ -213,7 +216,10 @@ public function setMultiple($values, $ttl = null)

$options = $this->storage->getOptions();
$previousTtl = $options->getTtl();
$options->setTtl($ttl);

if ($ttl !== null) {
$options->setTtl($ttl);
}

try {
$result = $this->storage->setItems($values);
Expand Down
47 changes: 45 additions & 2 deletions test/Psr/SimpleCache/SimpleCacheDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function setUp()
/**
* @param bool $staticTtl
* @param int $minTtl
* @return ObjectProphecy
*/
public function mockCapabilities(
ObjectProphecy $storage,
private function getMockCapabilities(
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
Expand All @@ -69,6 +69,22 @@ public function mockCapabilities(
$capabilities->getSupportedDatatypes()->willReturn($supportedDataTypes);
$capabilities->getStaticTtl()->willReturn($staticTtl);
$capabilities->getMinTtl()->willReturn($minTtl);

return $capabilities;
}

/**
* @param bool $staticTtl
* @param int $minTtl
*/
public function mockCapabilities(
ObjectProphecy $storage,
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
) {
$capabilities = $this->getMockCapabilities($supportedDataTypes, $staticTtl, $minTtl);

$storage->getCapabilities()->will([$capabilities, 'reveal']);
}

Expand Down Expand Up @@ -739,4 +755,31 @@ public function testHasReRaisesExceptionThrownByStorage()
$this->assertSame($exception, $e->getPrevious());
}
}

public function testUseTtlFromOptionsWhenNotProvidedOnSet()
{
$capabilities = $this->getMockCapabilities();

$storage = new TestAsset\TtlStorage(['ttl' => 20]);
$storage->setCapabilities($capabilities->reveal());
$cache = new SimpleCacheDecorator($storage);

$cache->set('foo', 'bar');
self::assertSame(20, $storage->ttl['foo']);
self::assertSame(20, $storage->getOptions()->getTtl());
}

public function testUseTtlFromOptionsWhenNotProvidedOnSetMultiple()
{
$capabilities = $this->getMockCapabilities();

$storage = new TestAsset\TtlStorage(['ttl' => 20]);
$storage->setCapabilities($capabilities->reveal());
$cache = new SimpleCacheDecorator($storage);

$cache->setMultiple(['foo' => 'bar', 'bar' => 'baz']);
self::assertSame(20, $storage->ttl['foo']);
self::assertSame(20, $storage->ttl['bar']);
self::assertSame(20, $storage->getOptions()->getTtl());
}
}
46 changes: 46 additions & 0 deletions test/Psr/SimpleCache/TestAsset/TtlStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Cache\Psr\SimpleCache\TestAsset;

use Zend\Cache\Storage\Adapter;
use Zend\Cache\Storage\Capabilities;

class TtlStorage extends Adapter\AbstractAdapter
{
/** @var array */
private $data = [];

/** @var array */
public $ttl = [];

protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null)
{
$success = isset($this->data[$normalizedKey]);

return $success ? $this->data[$normalizedKey] : null;
}

protected function internalSetItem(& $normalizedKey, & $value)
{
$this->ttl[$normalizedKey] = $this->getOptions()->getTtl();

$this->data[$normalizedKey] = $value;
return true;
}

protected function internalRemoveItem(& $normalizedKey)
{
unset($this->data[$normalizedKey]);
return true;
}

public function setCapabilities(Capabilities $capabilities)
{
$this->capabilities = $capabilities;
}
}