Passed
Pull Request — master (#1)
by Siim
12:30
created

TagAwareCacheAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 231
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A saveDeferred() 0 3 1
A invalidateTags() 0 3 1
A hasItem() 0 3 1
A get() 0 3 1
A clear() 0 3 1
A getCache() 0 3 1
A commit() 0 3 1
A init() 0 13 1
A getItems() 0 3 1
A delete() 0 3 1
A save() 0 3 1
A deleteItem() 0 3 1
A getItem() 0 3 1
A deleteItems() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace Sf4\Api\Services;
4
5
use Psr\Cache\CacheItemInterface;
6
use Psr\Cache\InvalidArgumentException;
7
use Symfony\Component\Cache\Adapter\RedisAdapter;
8
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
9
use Symfony\Contracts\Cache\CallbackInterface;
10
use Symfony\Contracts\Cache\ItemInterface;
11
12
class TagAwareCacheAdapter implements CacheAdapterInterface
13
{
14
    protected $cache;
15
16
    public function __construct()
17
    {
18
        $this->init();
19
    }
20
21
    protected function init()
22
    {
23
        $redisConnection = RedisAdapter::createConnection(
24
            'redis://localhost'
25
        );
26
        $redisAdapter = new RedisAdapter(
27
            $redisConnection,
28
            '',
29
            0
30
        );
31
        $this->cache = new TagAwareAdapter(
32
            $redisAdapter,
33
            $redisAdapter
34
        );
35
    }
36
37
    /**
38
     * Returns a Cache Item representing the specified key.
39
     *
40
     * This method must always return a ItemInterface object, even in case of
41
     * a cache miss. It MUST NOT return null.
42
     *
43
     * @param string $key
44
     *   The key for which to return the corresponding Cache Item.
45
     *
46
     * @throws InvalidArgumentException
47
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
48
     *   MUST be thrown.
49
     *
50
     * @return ItemInterface
51
     *   The corresponding Cache Item.
52
     */
53
    public function getItem($key)
54
    {
55
        return $this->getCache()->getItem($key);
56
    }
57
58
    /**
59
     * @return TagAwareAdapter
60
     */
61
    public function getCache(): TagAwareAdapter
62
    {
63
        return $this->cache;
64
    }
65
66
    /**
67
     * Returns a traversable set of cache items.
68
     *
69
     * @param string[] $keys
70
     *   An indexed array of keys of items to retrieve.
71
     *
72
     * @throws InvalidArgumentException
73
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
74
     *   MUST be thrown.
75
     *
76
     * @return array|\Traversable
77
     *   A traversable collection of Cache Items keyed by the cache keys of
78
     *   each item. A Cache item will be returned for each key, even if that
79
     *   key is not found. However, if no keys are specified then an empty
80
     *   traversable MUST be returned instead.
81
     */
82
    public function getItems(array $keys = array())
83
    {
84
        return $this->getCache()->getItems($keys);
85
    }
86
87
    /**
88
     * Confirms if the cache contains specified cache item.
89
     *
90
     * Note: This method MAY avoid retrieving the cached value for performance reasons.
91
     * This could result in a race condition with ItemInterface::get(). To avoid
92
     * such situation use ItemInterface::isHit() instead.
93
     *
94
     * @param string $key
95
     *   The key for which to check existence.
96
     *
97
     * @throws InvalidArgumentException
98
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
99
     *   MUST be thrown.
100
     *
101
     * @return bool
102
     *   True if item exists in the cache, false otherwise.
103
     */
104
    public function hasItem($key)
105
    {
106
        return $this->getCache()->hasItem($key);
107
    }
108
109
    /**
110
     * Deletes all items in the pool.
111
     *
112
     * @return bool
113
     *   True if the pool was successfully cleared. False if there was an error.
114
     */
115
    public function clear()
116
    {
117
        return $this->getCache()->clear();
118
    }
119
120
    /**
121
     * Removes the item from the pool.
122
     *
123
     * @param string $key
124
     *   The key to delete.
125
     *
126
     * @throws InvalidArgumentException
127
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
128
     *   MUST be thrown.
129
     *
130
     * @return bool
131
     *   True if the item was successfully removed. False if there was an error.
132
     */
133
    public function deleteItem($key)
134
    {
135
        return $this->getCache()->deleteItem($key);
136
    }
137
138
    /**
139
     * Removes multiple items from the pool.
140
     *
141
     * @param string[] $keys
142
     *   An array of keys that should be removed from the pool.
143
     * @throws InvalidArgumentException
144
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
145
     *   MUST be thrown.
146
     *
147
     * @return bool
148
     *   True if the items were successfully removed. False if there was an error.
149
     */
150
    public function deleteItems(array $keys)
151
    {
152
        return $this->getCache()->deleteItems($keys);
153
    }
154
155
    /**
156
     * Persists a cache item immediately.
157
     *
158
     * @param CacheItemInterface $item
159
     *   The cache item to save.
160
     *
161
     * @return bool
162
     *   True if the item was successfully persisted. False if there was an error.
163
     */
164
    public function save(CacheItemInterface $item)
165
    {
166
        return $this->getCache()->save($item);
167
    }
168
169
    /**
170
     * Sets a cache item to be persisted later.
171
     *
172
     * @param CacheItemInterface $item
173
     *   The cache item to save.
174
     *
175
     * @return bool
176
     *   False if the item could not be queued or if a commit was attempted and failed. True otherwise.
177
     */
178
    public function saveDeferred(CacheItemInterface $item)
179
    {
180
        return $this->getCache()->saveDeferred($item);
181
    }
182
183
    /**
184
     * Persists any deferred cache items.
185
     *
186
     * @return bool
187
     *   True if all not-yet-saved items were successfully saved or there were none. False otherwise.
188
     */
189
    public function commit()
190
    {
191
        return $this->getCache()->commit();
192
    }
193
194
    /**
195
     * Fetches a value from the pool or computes it if not found.
196
     *
197
     * On cache misses, a callback is called that should return the missing value.
198
     * This callback is given a PSR-6 ItemInterface instance corresponding to the
199
     * requested key, that could be used e.g. for expiration control. It could also
200
     * be an ItemInterface instance when its additional features are needed.
201
     *
202
     * @param string $key The key of the item to retrieve from the cache
203
     * @param callable|CallbackInterface $callback Should return the computed value for the given key/item
204
     * @param float|null $beta A float that, as it grows, controls the likeliness of triggering
205
     *        early expiration. 0 disables it, INF forces immediate expiration.
206
     *        The default (or providing null) is implementation dependent but should
207
     *        typically be 1.0, which should provide optimal stampede protection.
208
     *        See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
209
     * @param array &$metadata The metadata of the cached item {@see ItemInterface::getMetadata()}
210
     *
211
     * @return mixed The value corresponding to the provided key
212
     *
213
     */
214
    public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
215
    {
216
        return $this->getCache()->get($key, $callback, $beta, $metadata);
217
    }
218
219
    /**
220
     * Removes an item from the pool.
221
     *
222
     * @param string $key The key to delete
223
     *
224
     * @return bool True if the item was successfully removed, false if there was any error
225
     */
226
    public function delete(string $key): bool
227
    {
228
        return $this->getCache()->delete($key);
229
    }
230
231
    /**
232
     * Invalidates cached items using tags.
233
     *
234
     * @param string[] $tags An array of tags to invalidate
235
     *
236
     * @return bool True on success
237
     *
238
     * @throws InvalidArgumentException When $tags is not valid
239
     */
240
    public function invalidateTags(array $tags)
241
    {
242
        return $this->getCache()->invalidateTags($tags);
243
    }
244
}
245