Skip to content

Commit c787d27

Browse files
authored
Merge pull request #22 from Roave/support-doctrine-delete-multiple
Added support for Doctrine\Cache 1.7 with deleteMultiple
2 parents e45ace2 + cc24b6b commit c787d27

10 files changed

+40
-92
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Create your Doctrine Cache the usual way and inject it into `SimpleCacheAdapter`
2626

2727
namespace App;
2828

29-
use Interop\Container\ContainerInterface;
29+
use Psr\Container\ContainerInterface;
3030
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
3131
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
3232
use Doctrine\Common\Cache\RedisCache;
@@ -40,3 +40,12 @@ final class MyCacheFactory
4040
}
4141
```
4242

43+
## Upgrade Guide
44+
45+
### BC Breaks in 2.0
46+
47+
* Support for `MultiOperationCache` added, includes support for `deleteMultiple` in Doctrine 1.7 breaks:
48+
* `CacheException` static constructors `fromNonMultiGetCache` and `fromNonMultiPutCache` have been replaced with
49+
`fromNonMultiOperationCache`.
50+
* `SimpleCacheAdapter` now requires an adapter implementing `MultiOperationCache`, and no longer specifically
51+
requires a cache implementing `MultiGetCache` or `MultiPutCache` explicitly.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"require": {
66
"php": "~7.1.0",
7-
"doctrine/cache": "^1.6",
7+
"doctrine/cache": "^1.7",
88
"psr/simple-cache": "^1.0"
99
},
1010
"require-dev": {

src/Exception/CacheException.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@ public static function fromNonClearableCache(DoctrineCache $cache) : self
1616
));
1717
}
1818

19-
public static function fromNonMultiGetCache(DoctrineCache $cache) : self
19+
public static function fromNonMultiOperationCache(DoctrineCache $cache) : self
2020
{
2121
return new self(sprintf(
22-
'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.',
23-
get_class($cache)
24-
));
25-
}
26-
27-
public static function fromNonMultiPutCache(DoctrineCache $cache) : self
28-
{
29-
return new self(sprintf(
30-
'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.',
22+
'The given cache %s does not support multiple operations, '
23+
. 'but you tried to use a feature that requires a multi-operation cache.',
3124
get_class($cache)
3225
));
3326
}

src/SimpleCacheAdapter.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
use Doctrine\Common\Cache\Cache as DoctrineCache;
77
use Doctrine\Common\Cache\ClearableCache;
8-
use Doctrine\Common\Cache\MultiGetCache;
9-
use Doctrine\Common\Cache\MultiPutCache;
8+
use Doctrine\Common\Cache\MultiOperationCache;
109
use Psr\SimpleCache\CacheInterface as PsrCache;
1110
use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException;
1211

1312
final class SimpleCacheAdapter implements PsrCache
1413
{
1514
/**
16-
* @var DoctrineCache|ClearableCache|MultiGetCache|MultiPutCache
15+
* @var DoctrineCache|ClearableCache|MultiOperationCache
1716
*/
1817
private $doctrineCache;
1918

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

2827
if (!$this->doctrineCache instanceof ClearableCache) {
29-
throw Exception\CacheException::fromNonClearableCache($this->doctrineCache);
28+
throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache);
3029
}
31-
if (!$this->doctrineCache instanceof MultiGetCache) {
32-
throw Exception\CacheException::fromNonMultiGetCache($this->doctrineCache);
33-
}
34-
if (!$this->doctrineCache instanceof MultiPutCache) {
35-
throw Exception\CacheException::fromNonMultiPutCache($this->doctrineCache);
30+
31+
if (!$this->doctrineCache instanceof MultiOperationCache) {
32+
throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache);
3633
}
3734
}
3835

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

113-
if (!is_integer($ttl)) {
110+
if (!is_int($ttl)) {
114111
throw InvalidArgumentException::fromKeyAndInvalidTTL($key, $ttl);
115112
}
116113

@@ -144,7 +141,7 @@ public function clear() : bool
144141
* @return array
145142
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException
146143
*/
147-
public function getMultiple($keys, $default = null)
144+
public function getMultiple($keys, $default = null) : array
148145
{
149146
$keys = $this->filterValidateMultipleKeys($keys);
150147
return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys));
@@ -173,10 +170,10 @@ public function setMultiple($values, $ttl = null) : bool
173170
}
174171

175172
if ($ttl instanceof \DateInterval) {
176-
$ttl = self::convertDateIntervalToInteger($ttl);
173+
$ttl = $this->convertDateIntervalToInteger($ttl);
177174
}
178175

179-
if (!is_integer($ttl)) {
176+
if (!is_int($ttl)) {
180177
throw InvalidArgumentException::fromKeyAndInvalidTTL(key($validatedValues), $ttl);
181178
}
182179

@@ -194,16 +191,7 @@ public function setMultiple($values, $ttl = null) : bool
194191
*/
195192
public function deleteMultiple($keys) : bool
196193
{
197-
$keys = $this->filterValidateMultipleKeys($keys);
198-
199-
$success = true;
200-
foreach ($keys as $key) {
201-
if (!$this->delete($key)) {
202-
$success = false;
203-
}
204-
}
205-
206-
return $success;
194+
return $this->doctrineCache->deleteMultiple($this->filterValidateMultipleKeys($keys));
207195
}
208196

209197
/**

test/asset/FullyImplementedCache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
use Doctrine\Common\Cache\Cache;
77
use Doctrine\Common\Cache\ClearableCache;
8-
use Doctrine\Common\Cache\MultiGetCache;
9-
use Doctrine\Common\Cache\MultiPutCache;
8+
use Doctrine\Common\Cache\MultiOperationCache;
109

11-
interface FullyImplementedCache extends Cache, ClearableCache, MultiGetCache, MultiPutCache
10+
interface FullyImplementedCache extends Cache, ClearableCache, MultiOperationCache
1211
{
1312
}

test/asset/NotClearableCache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
namespace RoaveTestAsset\DoctrineSimpleCache;
55

66
use Doctrine\Common\Cache\Cache;
7-
use Doctrine\Common\Cache\MultiGetCache;
8-
use Doctrine\Common\Cache\MultiPutCache;
7+
use Doctrine\Common\Cache\MultiOperationCache;
98

10-
interface NotClearableCache extends Cache, MultiGetCache, MultiPutCache
9+
interface NotClearableCache extends Cache, MultiOperationCache
1110
{
1211
}

test/asset/NotMultiGettableCache.php renamed to test/asset/NotMultiOperationCache.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
use Doctrine\Common\Cache\Cache;
77
use Doctrine\Common\Cache\ClearableCache;
8-
use Doctrine\Common\Cache\MultiPutCache;
98

10-
interface NotMultiGettableCache extends Cache, ClearableCache, MultiPutCache
9+
interface NotMultiOperationCache extends Cache, ClearableCache
1110
{
1211
}

test/asset/NotMultiPuttableCache.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/unit/Exception/CacheExceptionTest.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,19 @@ public function testFromNonClearableCache()
2828
);
2929
}
3030

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

36-
$exception = CacheException::fromNonMultiGetCache($doctrineCache);
36+
$exception = CacheException::fromNonMultiOperationCache($doctrineCache);
3737

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

4141
self::assertStringMatchesFormat(
42-
'The given cache %s cannot multi-get, but you tried to use a feature that requires a multi-get cache.',
43-
$exception->getMessage()
44-
);
45-
}
46-
47-
public function testFromNonMultiPutCache()
48-
{
49-
/** @var DoctrineCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
50-
$doctrineCache = $this->createMock(DoctrineCache::class);
51-
52-
$exception = CacheException::fromNonMultiPutCache($doctrineCache);
53-
54-
self::assertInstanceOf(CacheException::class, $exception);
55-
self::assertInstanceOf(PsrCacheException::class, $exception);
56-
57-
self::assertStringMatchesFormat(
58-
'The given cache %s cannot multi-put, but you tried to use a feature that requires a multi-put cache.',
42+
'The given cache %s does not support multiple operations, '
43+
. 'but you tried to use a feature that requires a multi-operation cache.',
5944
$exception->getMessage()
6045
);
6146
}

test/unit/SimpleCacheAdapterTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
1010
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache;
1111
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache;
12-
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache;
13-
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache;
12+
use RoaveTestAsset\DoctrineSimpleCache\NotMultiOperationCache;
1413

1514
/**
1615
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
@@ -65,10 +64,10 @@ public function invalidKeys()
6564
];
6665
}
6766

68-
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
67+
public function testConstructorThrowsExceptionWhenNotMultiOperationCacheIsUsed()
6968
{
70-
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
71-
$doctrineCache = $this->createMock(NotMultiPuttableCache::class);
69+
/** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
70+
$doctrineCache = $this->createMock(NotMultiOperationCache::class);
7271

7372
$this->expectException(CacheException::class);
7473
new SimpleCacheAdapter($doctrineCache);
@@ -83,15 +82,6 @@ public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
8382
new SimpleCacheAdapter($doctrineCache);
8483
}
8584

86-
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
87-
{
88-
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
89-
$doctrineCache = $this->createMock(NotMultiGettableCache::class);
90-
91-
$this->expectException(CacheException::class);
92-
new SimpleCacheAdapter($doctrineCache);
93-
}
94-
9585
public function testGetProxiesToDoctrineFetch()
9686
{
9787
$key = uniqid('key', true);
@@ -407,8 +397,7 @@ public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed()
407397

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

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

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

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

0 commit comments

Comments
 (0)