|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the CacheServiceDecorator class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @version //autogentag// |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace eZ\Publish\Core\Persistence\Cache; |
|
12
|
|
|
|
|
13
|
|
|
use Stash\Interfaces\PoolInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CacheServiceDecorator. |
|
17
|
|
|
* |
|
18
|
|
|
* Wraps the Cache Service for Spi cache to apply key prefix for the cache |
|
19
|
|
|
*/ |
|
20
|
|
|
class CacheServiceDecorator |
|
21
|
|
|
{ |
|
22
|
|
|
const SPI_CACHE_KEY_PREFIX = 'ez_spi'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Stash\Interfaces\PoolInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cachePool; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructs the cache service decorator. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Stash\Interfaces\PoolInterface $cachePool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(PoolInterface $cachePool) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->cachePool = $cachePool; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Prepend key with prefix and support array format Stash supported before. |
|
41
|
|
|
* |
|
42
|
|
|
* {@see \Psr\Cache\CacheItemPoolInterface} |
|
43
|
|
|
* |
|
44
|
|
|
* @internal param array|string $key , $key, $key... |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Stash\Interfaces\ItemInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getItem() |
|
49
|
|
|
{ |
|
50
|
|
|
$args = func_get_args(); |
|
51
|
|
|
|
|
52
|
|
|
if (empty($args)) { |
|
53
|
|
|
return $this->cachePool->getItem(self::SPI_CACHE_KEY_PREFIX); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Upstream seems to no longer support array, so we flatten it |
|
57
|
|
|
if (!isset($args[1]) && is_array($args[0])) { |
|
58
|
|
|
$key = implode('/', $args[0]); |
|
59
|
|
|
} else { |
|
60
|
|
|
$key = '' . implode('/', $args); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$key = trim($key, '/'); |
|
64
|
|
|
$key = $key === '' ? self::SPI_CACHE_KEY_PREFIX : self::SPI_CACHE_KEY_PREFIX . '/' . $key; |
|
65
|
|
|
|
|
66
|
|
|
return $this->cachePool->getItem($key); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Prepend keys with prefix |
|
71
|
|
|
* |
|
72
|
|
|
* {@see \Psr\Cache\CacheItemPoolInterface} |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $keys |
|
75
|
|
|
* @return \Stash\Interfaces\ItemInterface[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getItems(array $keys = array()) |
|
78
|
|
|
{ |
|
79
|
|
|
$prefix = self::SPI_CACHE_KEY_PREFIX; |
|
80
|
|
|
$keys = array_map( |
|
81
|
|
|
function($key) use ($prefix) { |
|
82
|
|
|
$key = trim($key, '/'); |
|
83
|
|
|
return $key === '' ? $prefix : $prefix . '/' . $key; |
|
84
|
|
|
}, |
|
85
|
|
|
$keys |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return $this->cachePool->getItems($keys); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Clears the cache for the key, or if none is specified clears the entire cache. The key can be either |
|
93
|
|
|
* a series of string arguments, or an array. |
|
94
|
|
|
* |
|
95
|
|
|
* @internal param array|null|string $key , $key, $key... |
|
96
|
|
|
*/ |
|
97
|
|
|
public function clear() |
|
98
|
|
|
{ |
|
99
|
|
|
$item = call_user_func_array(array($this, 'getItem'), func_get_args()); |
|
100
|
|
|
|
|
101
|
|
|
return $item->clear(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|