Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\Common\Cache;
4
 
5
use function array_combine;
6
use function array_key_exists;
7
use function array_map;
8
use function sprintf;
9
 
10
/**
11
 * Base class for cache provider implementations.
12
 */
13
abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache
14
{
15
    public const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
16
 
17
    /**
18
     * The namespace to prefix all cache ids with.
19
     *
20
     * @var string
21
     */
22
    private $namespace = '';
23
 
24
    /**
25
     * The namespace version.
26
     *
27
     * @var int|null
28
     */
29
    private $namespaceVersion;
30
 
31
    /**
32
     * Sets the namespace to prefix all cache ids with.
33
     *
34
     * @param string $namespace
35
     *
36
     * @return void
37
     */
38
    public function setNamespace($namespace)
39
    {
40
        $this->namespace        = (string) $namespace;
41
        $this->namespaceVersion = null;
42
    }
43
 
44
    /**
45
     * Retrieves the namespace that prefixes all cache ids.
46
     *
47
     * @return string
48
     */
49
    public function getNamespace()
50
    {
51
        return $this->namespace;
52
    }
53
 
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function fetch($id)
58
    {
59
        return $this->doFetch($this->getNamespacedId($id));
60
    }
61
 
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function fetchMultiple(array $keys)
66
    {
67
        if (empty($keys)) {
68
            return [];
69
        }
70
 
71
        // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
72
        $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys));
73
        $items          = $this->doFetchMultiple($namespacedKeys);
74
        $foundItems     = [];
75
 
76
        // no internal array function supports this sort of mapping: needs to be iterative
77
        // this filters and combines keys in one pass
78
        foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
79
            if (! isset($items[$namespacedKey]) && ! array_key_exists($namespacedKey, $items)) {
80
                continue;
81
            }
82
 
83
            $foundItems[$requestedKey] = $items[$namespacedKey];
84
        }
85
 
86
        return $foundItems;
87
    }
88
 
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function saveMultiple(array $keysAndValues, $lifetime = 0)
93
    {
94
        $namespacedKeysAndValues = [];
95
        foreach ($keysAndValues as $key => $value) {
96
            $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
97
        }
98
 
99
        return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime);
100
    }
101
 
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function contains($id)
106
    {
107
        return $this->doContains($this->getNamespacedId($id));
108
    }
109
 
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function save($id, $data, $lifeTime = 0)
114
    {
115
        return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
116
    }
117
 
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function deleteMultiple(array $keys)
122
    {
123
        return $this->doDeleteMultiple(array_map([$this, 'getNamespacedId'], $keys));
124
    }
125
 
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function delete($id)
130
    {
131
        return $this->doDelete($this->getNamespacedId($id));
132
    }
133
 
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getStats()
138
    {
139
        return $this->doGetStats();
140
    }
141
 
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function flushAll()
146
    {
147
        return $this->doFlush();
148
    }
149
 
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function deleteAll()
154
    {
155
        $namespaceCacheKey = $this->getNamespaceCacheKey();
156
        $namespaceVersion  = $this->getNamespaceVersion() + 1;
157
 
158
        if ($this->doSave($namespaceCacheKey, $namespaceVersion)) {
159
            $this->namespaceVersion = $namespaceVersion;
160
 
161
            return true;
162
        }
163
 
164
        return false;
165
    }
166
 
167
    /**
168
     * Prefixes the passed id with the configured namespace value.
169
     *
170
     * @param string $id The id to namespace.
171
     *
172
     * @return string The namespaced id.
173
     */
174
    private function getNamespacedId(string $id): string
175
    {
176
        $namespaceVersion = $this->getNamespaceVersion();
177
 
178
        return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
179
    }
180
 
181
    /**
182
     * Returns the namespace cache key.
183
     */
184
    private function getNamespaceCacheKey(): string
185
    {
186
        return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
187
    }
188
 
189
    /**
190
     * Returns the namespace version.
191
     */
192
    private function getNamespaceVersion(): int
193
    {
194
        if ($this->namespaceVersion !== null) {
195
            return $this->namespaceVersion;
196
        }
197
 
198
        $namespaceCacheKey      = $this->getNamespaceCacheKey();
199
        $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1;
200
 
201
        return $this->namespaceVersion;
202
    }
203
 
204
    /**
205
     * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
206
     *
207
     * @param string[] $keys Array of keys to retrieve from cache
208
     *
209
     * @return mixed[] Array of values retrieved for the given keys.
210
     */
211
    protected function doFetchMultiple(array $keys)
212
    {
213
        $returnValues = [];
214
 
215
        foreach ($keys as $key) {
216
            $item = $this->doFetch($key);
217
            if ($item === false && ! $this->doContains($key)) {
218
                continue;
219
            }
220
 
221
            $returnValues[$key] = $item;
222
        }
223
 
224
        return $returnValues;
225
    }
226
 
227
    /**
228
     * Fetches an entry from the cache.
229
     *
230
     * @param string $id The id of the cache entry to fetch.
231
     *
232
     * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
233
     */
234
    abstract protected function doFetch($id);
235
 
236
    /**
237
     * Tests if an entry exists in the cache.
238
     *
239
     * @param string $id The cache id of the entry to check for.
240
     *
241
     * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
242
     */
243
    abstract protected function doContains($id);
244
 
245
    /**
246
     * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
247
     *
248
     * @param mixed[] $keysAndValues Array of keys and values to save in cache
249
     * @param int     $lifetime      The lifetime. If != 0, sets a specific lifetime for these
250
     *                               cache entries (0 => infinite lifeTime).
251
     *
252
     * @return bool TRUE if the operation was successful, FALSE if it wasn't.
253
     */
254
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
255
    {
256
        $success = true;
257
 
258
        foreach ($keysAndValues as $key => $value) {
259
            if ($this->doSave($key, $value, $lifetime)) {
260
                continue;
261
            }
262
 
263
            $success = false;
264
        }
265
 
266
        return $success;
267
    }
268
 
269
    /**
270
     * Puts data into the cache.
271
     *
272
     * @param string $id       The cache id.
273
     * @param string $data     The cache entry/data.
274
     * @param int    $lifeTime The lifetime. If != 0, sets a specific lifetime for this
275
     *                           cache entry (0 => infinite lifeTime).
276
     *
277
     * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
278
     */
279
    abstract protected function doSave($id, $data, $lifeTime = 0);
280
 
281
    /**
282
     * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it.
283
     *
284
     * @param string[] $keys Array of keys to delete from cache
285
     *
286
     * @return bool TRUE if the operation was successful, FALSE if it wasn't
287
     */
288
    protected function doDeleteMultiple(array $keys)
289
    {
290
        $success = true;
291
 
292
        foreach ($keys as $key) {
293
            if ($this->doDelete($key)) {
294
                continue;
295
            }
296
 
297
            $success = false;
298
        }
299
 
300
        return $success;
301
    }
302
 
303
    /**
304
     * Deletes a cache entry.
305
     *
306
     * @param string $id The cache id.
307
     *
308
     * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
309
     */
310
    abstract protected function doDelete($id);
311
 
312
    /**
313
     * Flushes all cache entries.
314
     *
315
     * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
316
     */
317
    abstract protected function doFlush();
318
 
319
    /**
320
     * Retrieves cached information from the data store.
321
     *
322
     * @return mixed[]|null An associative array with server's statistics if available, NULL otherwise.
323
     */
324
    abstract protected function doGetStats();
325
}