Skip to content

Added support for Doctrine\Cache 1.7 with deleteMultiple #22

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 3 commits into from
Sep 14, 2017
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Create your Doctrine Cache the usual way and inject it into `SimpleCacheAdapter`

namespace App;

use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
use Doctrine\Common\Cache\RedisCache;
Expand All @@ -40,3 +40,12 @@ final class MyCacheFactory
}
```

## Upgrade Guide

### BC Breaks in 2.0

* Support for `MultiOperationCache` added, includes support for `deleteMultiple` in Doctrine 1.7 breaks:
* `CacheException` static constructors `fromNonMultiGetCache` and `fromNonMultiPutCache` have been replaced with
`fromNonMultiOperationCache`.
* `SimpleCacheAdapter` now requires an adapter implementing `MultiOperationCache`, and no longer specifically
requires a cache implementing `MultiGetCache` or `MultiPutCache` explicitly.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"require": {
"php": "~7.1.0",
"doctrine/cache": "^1.6",
"doctrine/cache": "^1.7",
"psr/simple-cache": "^1.0"
},
"require-dev": {
Expand Down
13 changes: 3 additions & 10 deletions src/Exception/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ public static function fromNonClearableCache(DoctrineCache $cache) : self
));
}

public static function fromNonMultiGetCache(DoctrineCache $cache) : self
public static function fromNonMultiOperationCache(DoctrineCache $cache) : self
{
return new self(sprintf(
'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.',
get_class($cache)
));
}

public static function fromNonMultiPutCache(DoctrineCache $cache) : self
Copy link
Member

Choose a reason for hiding this comment

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

Minor BC break: requires UPGRADE.md notes or revert + deprecation

{
return new self(sprintf(
'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.',
'The given cache %s does not support multiple operations, '
. 'but you tried to use a feature that requires a multi-operation cache.',
get_class($cache)
));
}
Expand Down
34 changes: 11 additions & 23 deletions src/SimpleCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

use Doctrine\Common\Cache\Cache as DoctrineCache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\MultiGetCache;
use Doctrine\Common\Cache\MultiPutCache;
use Doctrine\Common\Cache\MultiOperationCache;
use Psr\SimpleCache\CacheInterface as PsrCache;
use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException;

final class SimpleCacheAdapter implements PsrCache
{
/**
* @var DoctrineCache|ClearableCache|MultiGetCache|MultiPutCache
* @var DoctrineCache|ClearableCache|MultiOperationCache
*/
private $doctrineCache;

Expand All @@ -26,13 +25,11 @@ public function __construct(DoctrineCache $doctrineCache)
$this->doctrineCache = $doctrineCache;

if (!$this->doctrineCache instanceof ClearableCache) {
throw Exception\CacheException::fromNonClearableCache($this->doctrineCache);
throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache);
}
if (!$this->doctrineCache instanceof MultiGetCache) {
throw Exception\CacheException::fromNonMultiGetCache($this->doctrineCache);
}
if (!$this->doctrineCache instanceof MultiPutCache) {
throw Exception\CacheException::fromNonMultiPutCache($this->doctrineCache);

if (!$this->doctrineCache instanceof MultiOperationCache) {
throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache);
}
}

Expand Down Expand Up @@ -110,7 +107,7 @@ public function set($key, $value, $ttl = null) : bool
$ttl = $this->convertDateIntervalToInteger($ttl);
}

if (!is_integer($ttl)) {
if (!is_int($ttl)) {
throw InvalidArgumentException::fromKeyAndInvalidTTL($key, $ttl);
}

Expand Down Expand Up @@ -144,7 +141,7 @@ public function clear() : bool
* @return array
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException
*/
public function getMultiple($keys, $default = null)
public function getMultiple($keys, $default = null) : array
{
$keys = $this->filterValidateMultipleKeys($keys);
return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys));
Expand Down Expand Up @@ -173,10 +170,10 @@ public function setMultiple($values, $ttl = null) : bool
}

if ($ttl instanceof \DateInterval) {
$ttl = self::convertDateIntervalToInteger($ttl);
$ttl = $this->convertDateIntervalToInteger($ttl);
}

if (!is_integer($ttl)) {
if (!is_int($ttl)) {
throw InvalidArgumentException::fromKeyAndInvalidTTL(key($validatedValues), $ttl);
}

Expand All @@ -194,16 +191,7 @@ public function setMultiple($values, $ttl = null) : bool
*/
public function deleteMultiple($keys) : bool
{
$keys = $this->filterValidateMultipleKeys($keys);

$success = true;
foreach ($keys as $key) {
if (!$this->delete($key)) {
$success = false;
}
}

return $success;
return $this->doctrineCache->deleteMultiple($this->filterValidateMultipleKeys($keys));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions test/asset/FullyImplementedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\MultiGetCache;
use Doctrine\Common\Cache\MultiPutCache;
use Doctrine\Common\Cache\MultiOperationCache;

interface FullyImplementedCache extends Cache, ClearableCache, MultiGetCache, MultiPutCache
interface FullyImplementedCache extends Cache, ClearableCache, MultiOperationCache
{
}
5 changes: 2 additions & 3 deletions test/asset/NotClearableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
namespace RoaveTestAsset\DoctrineSimpleCache;

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\MultiGetCache;
use Doctrine\Common\Cache\MultiPutCache;
use Doctrine\Common\Cache\MultiOperationCache;

interface NotClearableCache extends Cache, MultiGetCache, MultiPutCache
interface NotClearableCache extends Cache, MultiOperationCache
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\MultiPutCache;

interface NotMultiGettableCache extends Cache, ClearableCache, MultiPutCache
interface NotMultiOperationCache extends Cache, ClearableCache
{
}
12 changes: 0 additions & 12 deletions test/asset/NotMultiPuttableCache.php

This file was deleted.

23 changes: 4 additions & 19 deletions test/unit/Exception/CacheExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,19 @@ public function testFromNonClearableCache()
);
}

public function testFromNonMultiGetCache()
public function testFromNonMultiOperationCache()
{
/** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(DoctrineCache::class);

$exception = CacheException::fromNonMultiGetCache($doctrineCache);
$exception = CacheException::fromNonMultiOperationCache($doctrineCache);

self::assertInstanceOf(CacheException::class, $exception);
self::assertInstanceOf(PsrCacheException::class, $exception);

self::assertStringMatchesFormat(
'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.',
$exception->getMessage()
);
}

public function testFromNonMultiPutCache()
{
/** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(DoctrineCache::class);

$exception = CacheException::fromNonMultiPutCache($doctrineCache);

self::assertInstanceOf(CacheException::class, $exception);
self::assertInstanceOf(PsrCacheException::class, $exception);

self::assertStringMatchesFormat(
'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.',
'The given cache %s does not support multiple operations, '
. 'but you tried to use a feature that requires a multi-operation cache.',
$exception->getMessage()
);
}
Expand Down
24 changes: 6 additions & 18 deletions test/unit/SimpleCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache;
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache;
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache;
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache;
use RoaveTestAsset\DoctrineSimpleCache\NotMultiOperationCache;

/**
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
Expand Down Expand Up @@ -65,10 +64,10 @@ public function invalidKeys()
];
}

public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
public function testConstructorThrowsExceptionWhenNotMultiOperationCacheIsUsed()
{
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiPuttableCache::class);
/** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiOperationCache::class);

$this->expectException(CacheException::class);
new SimpleCacheAdapter($doctrineCache);
Expand All @@ -83,15 +82,6 @@ public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
new SimpleCacheAdapter($doctrineCache);
}

public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
{
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(NotMultiGettableCache::class);

$this->expectException(CacheException::class);
new SimpleCacheAdapter($doctrineCache);
}

public function testGetProxiesToDoctrineFetch()
{
$key = uniqid('key', true);
Expand Down Expand Up @@ -407,8 +397,7 @@ public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed()

/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(FullyImplementedCache::class);
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true);
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
$doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->willReturn(true);

$psrCache = new SimpleCacheAdapter($doctrineCache);
self::assertTrue($psrCache->deleteMultiple($keys));
Expand All @@ -423,8 +412,7 @@ public function testDeleteMultipleReturnsFalseWhenOneDeleteFails()

/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
$doctrineCache = $this->createMock(FullyImplementedCache::class);
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false);
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
$doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->willReturn(false);

$psrCache = new SimpleCacheAdapter($doctrineCache);
self::assertFalse($psrCache->deleteMultiple($keys));
Expand Down