|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the MIT license. For more information, see |
|
18
|
|
|
* <http://www.doctrine-project.org>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Doctrine\ORM\Cache\Region; |
|
22
|
|
|
|
|
23
|
|
|
use Doctrine\Common\Cache\Cache as CacheAdapter; |
|
24
|
|
|
use Doctrine\Common\Cache\ClearableCache; |
|
25
|
|
|
use Doctrine\ORM\Cache\CacheEntry; |
|
26
|
|
|
use Doctrine\ORM\Cache\CacheKey; |
|
27
|
|
|
use Doctrine\ORM\Cache\CollectionCacheEntry; |
|
28
|
|
|
use Doctrine\ORM\Cache\Lock; |
|
29
|
|
|
use Doctrine\ORM\Cache\Region; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The simplest cache region compatible with all doctrine-cache drivers. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 2.5 |
|
35
|
|
|
* @author Fabio B. Silva <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class DefaultRegion implements Region |
|
38
|
|
|
{ |
|
39
|
|
|
const REGION_KEY_SEPARATOR = '_'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var CacheAdapter |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $cache; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $name; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var integer |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $lifetime = 0; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* @param CacheAdapter $cache |
|
59
|
|
|
* @param integer $lifetime |
|
60
|
|
|
*/ |
|
61
|
271 |
|
public function __construct($name, CacheAdapter $cache, $lifetime = 0) |
|
62
|
|
|
{ |
|
63
|
271 |
|
$this->cache = $cache; |
|
64
|
271 |
|
$this->name = (string) $name; |
|
65
|
271 |
|
$this->lifetime = (integer) $lifetime; |
|
66
|
271 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
137 |
|
public function getName() |
|
72
|
|
|
{ |
|
73
|
137 |
|
return $this->name; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \Doctrine\Common\Cache\CacheProvider |
|
78
|
|
|
*/ |
|
79
|
6 |
|
public function getCache() |
|
80
|
|
|
{ |
|
81
|
6 |
|
return $this->cache; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
127 |
|
public function contains(CacheKey $key) |
|
88
|
|
|
{ |
|
89
|
127 |
|
return $this->cache->contains($this->getCacheEntryKey($key)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
95 |
|
public function get(CacheKey $key) |
|
96
|
|
|
{ |
|
97
|
95 |
|
$entry = $this->cache->fetch($this->getCacheEntryKey($key)); |
|
98
|
|
|
|
|
99
|
95 |
|
if (! $entry instanceof CacheEntry) { |
|
100
|
76 |
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
79 |
|
return $entry; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function getMultiple(CollectionCacheEntry $collection) |
|
110
|
|
|
{ |
|
111
|
2 |
|
$result = []; |
|
112
|
|
|
|
|
113
|
2 |
|
foreach ($collection->identifiers as $key) { |
|
114
|
2 |
|
$entryKey = $this->getCacheEntryKey($key); |
|
115
|
2 |
|
$entryValue = $this->cache->fetch($entryKey); |
|
116
|
|
|
|
|
117
|
2 |
|
if (! $entryValue instanceof CacheEntry) { |
|
118
|
1 |
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$result[] = $entryValue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param CacheKey $key |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
139 |
|
protected function getCacheEntryKey(CacheKey $key) |
|
132
|
|
|
{ |
|
133
|
139 |
|
return $this->name . self::REGION_KEY_SEPARATOR . $key->hash; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
132 |
|
public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null) |
|
140
|
|
|
{ |
|
141
|
132 |
|
return $this->cache->save($this->getCacheEntryKey($key), $entry, $this->lifetime); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
37 |
|
public function evict(CacheKey $key) |
|
148
|
|
|
{ |
|
149
|
37 |
|
return $this->cache->delete($this->getCacheEntryKey($key)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
*/ |
|
155
|
84 |
|
public function evictAll() |
|
156
|
|
|
{ |
|
157
|
84 |
|
if (! $this->cache instanceof ClearableCache) { |
|
158
|
1 |
|
throw new \BadMethodCallException(sprintf( |
|
159
|
1 |
|
'Clearing all cache entries is not supported by the supplied cache adapter of type %s', |
|
160
|
1 |
|
get_class($this->cache) |
|
161
|
|
|
)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
83 |
|
return $this->cache->deleteAll(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|