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

PSR-16 Simple Cache Interface implementation #126

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ storage adapters (DB, File, Memcache, etc).
- File issues at https://github.com/zendframework/zend-cache/issues
- Documentation is at https://zendframework.github.io/zend-cache/

`Zend\Cache` implements [PSR-16 Simple Cache](https://github.com/php-fig/simple-cache).

## Benchmarks

We provide scripts for benchmarking zend-cache using the
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"php": "^5.5 || ^7.0",
"zendframework/zend-stdlib": "^2.7 || ^3.0",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0"
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0",
"psr/simple-cache": "^1.0.0"
},
"require-dev": {
"zendframework/zend-serializer": "^2.6",
Expand Down
52 changes: 50 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Cache\Exception;

interface ExceptionInterface
use Psr\SimpleCache\CacheException as Psr16CacheException;

interface ExceptionInterface extends Psr16CacheException
{
}
5 changes: 4 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace Zend\Cache\Exception;

use Psr\SimpleCache\InvalidArgumentException as Psr16InvalidArgumentException;

class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
ExceptionInterface,
Psr16InvalidArgumentException
{
}
89 changes: 88 additions & 1 deletion src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
namespace Zend\Cache\Storage\Adapter;

use ArrayObject;
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
use SplObjectStorage;
use stdClass;
use Traversable;
use Zend\Cache\Exception;
use Zend\Cache\Exception\BadMethodCallException;
use Zend\Cache\Storage\Capabilities;
use Zend\Cache\Storage\Event;
use Zend\Cache\Storage\ExceptionEvent;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\Plugin;
use Zend\Cache\Storage\PostEvent;
use Zend\Cache\Storage\StorageInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventsCapableInterface;

abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface
abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface, Psr16CacheInterface
{
/**
* The used EventManager if any
Expand Down Expand Up @@ -1577,4 +1580,88 @@ protected function normalizeKeyValuePairs(array & $keyValuePairs)
}
$keyValuePairs = $normalizedKeyValuePairs;
}

/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
$result = $this->getItem($key);
return null === $result ? $default : $result;
}

/**
* {@inheritDoc}
*/
public function set($key, $value, $ttl = null)
{
$previousTtl = $this->getOptions()->getTtl();
$this->getOptions()->setTtl($ttl);
$result = $this->setItem($key, $value);
$this->getOptions()->setTtl($previousTtl);
return $result;
}

/**
* {@inheritDoc}
*/
public function delete($key)
{
return $this->removeItem($key);
}

/**
* {@inheritDoc}
*/
public function clear()
{
if (! $this instanceof FlushableInterface) {
return false;
}
return $this->flush();
}

/**
* {@inheritDoc}
*/
public function getMultiple($keys, $default = null)
{
$results = $this->getItems($keys);

foreach ($keys as $key) {
if (! isset($results[$key]) && null !== $default) {
$results[$key] = $default;
}
}

return $results;
}

/**
* {@inheritDoc}
*/
public function setMultiple($values, $ttl = null)
{
$previousTtl = $this->getOptions()->getTtl();
$this->getOptions()->setTtl($ttl);
$result = $this->setItems($values);
$this->getOptions()->setTtl($previousTtl);
return $result;
}

/**
* {@inheritDoc}
*/
public function deleteMultiple($keys)
{
return ! $this->removeItems($keys);
}

/**
* {@inheritDoc}
*/
public function has($key)
{
return $this->hasItem($key);
}
}
106 changes: 105 additions & 1 deletion test/Storage/Adapter/CommonAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Cache\Storage\Adapter;

use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
use Zend\Cache\Storage\AdapterPluginManager;
use Zend\Cache\Storage\AvailableSpaceCapableInterface;
use Zend\Cache\Storage\IterableInterface;
Expand Down Expand Up @@ -42,7 +43,7 @@ abstract class CommonAdapterTest extends \PHPUnit_Framework_TestCase
/**
* The storage adapter
*
* @var StorageInterface
* @var StorageInterface|Psr16CacheInterface
*/
protected $_storage;

Expand Down Expand Up @@ -1252,4 +1253,107 @@ protected function waitForFullSecond()
$interval = (microtime(true) - time()) * 1000000;
usleep((int) $interval);
}

public function testItIsAPsr16Adapter()
{
$this->assertInstanceOf(Psr16CacheInterface::class, $this->_storage);
}

public function testPsr16GetImplementation()
{
$this->_storage->set('key', 'value');
$this->assertEquals('value', $this->_storage->get('key'));
$this->assertEquals('defaultValue', $this->_storage->get('keyThatDoesntExist', 'defaultValue'));
}

public function testPsr16SetImplementation()
{
$this->_storage->set('key', 'value');
$this->assertTrue($this->_storage->has('key'));
$this->assertEquals('value', $this->_storage->get('key'));
}

public function testPsr16DeleteImplementation()
{
$this->_storage->set('key', 'value');
$this->_storage->delete('key');
$this->assertFalse($this->_storage->has('key'));
}

public function testPsr16ClearImplementation()
{
if (! $this->_storage instanceof FlushableInterface) {
$this->markTestSkipped();
}
$this->_storage->set('key', 'value');
$this->_storage->clear();
$this->assertFalse($this->_storage->has('key'));
}

public function testPsr16GetMultipleImplementation()
{
$this->_storage->set('key1', 'value1');
$this->_storage->set('key2', 'value2');

$result = $this->_storage->getMultiple(['key1', 'key2']);

$expectedResult = [
'key1' => 'value1',
'key2' => 'value2',
];

$this->assertInternalType('array', $result);
$this->assertEquals($expectedResult, $result);
}

public function testPsr16GetMultipleImplementationWithDefaults()
{
$this->_storage->set('key1', 'value1');

$result = $this->_storage->getMultiple(['key1', 'key2'], 'default');

$expectedResult = [
'key1' => 'value1',
'key2' => 'default',
];

$this->assertInternalType('array', $result);
$this->assertEquals($expectedResult, $result);
}

public function testPsr16SetMultipleImplementation()
{
$cachableValuesToSet = [
'key1' => 'value1',
'key2' => 'value2',
];

$this->_storage->setMultiple($cachableValuesToSet);

$this->assertEquals('value1', $this->_storage->get('key1'));
$this->assertEquals('value2', $this->_storage->get('key2'));
}

public function testPsr16DeleteMultipleImplementation()
{
$cachableValuesToSet = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];

$this->_storage->setMultiple($cachableValuesToSet);
$this->_storage->deleteMultiple(['key1', 'key2']);

$this->assertFalse($this->_storage->has('key1'));
$this->assertFalse($this->_storage->has('key2'));
$this->assertTrue($this->_storage->has('key3'));
}

public function testPsr16HasImplementation()
{
$this->_storage->setItem('keyThatExists', 'value');
$this->assertTrue($this->_storage->has('keyThatExists'));
$this->assertFalse($this->_storage->has('keyThatDoesntExist'));
}
}
14 changes: 14 additions & 0 deletions test/Storage/Adapter/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,18 @@ public function tearDown()

parent::tearDown();
}

public function testPsr16SetMultipleImplementationWithTtl()
{
$this->_storage->setMultiple(['key' => 'value'], 1);
usleep(2000001);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a good test solution IMO :|

$this->assertFalse($this->_storage->has('key'));
}

public function testPsr16SetImplementationWithTtl()
{
$this->_storage->set('key', 'value', 1);
usleep(2000001);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a good test solution IMO :|

$this->assertFalse($this->_storage->has('key'));
}
}