Completed
Push — 16.x ( 7c4702...19349c )
by
unknown
23s queued 10s
created

LocalCacheAdapter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 243
ccs 27
cts 57
cp 0.4737
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A cacheKey() 0 4 1
A isCached() 0 4 1
A notCached() 0 4 1
A addReference() 0 4 1
A resolveReference() 0 11 2
A fromCache() 0 6 2
A flushCache() 0 6 1
B invalidateTags() 0 24 6
A removeCache() 0 15 3
A toCache() 0 31 5
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\LocalCacheAdapter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cache;
22
23
use TechDivision\Import\Utils\CacheKeyUtilInterface;
24
25
/**
26
 * Local cache adapter implementation.
27
 *
28
 * This cache adapter guarantees maximum performance but can eventually not be used
29
 * in a distributed environemnt where you want to use e. g. Redis for caching.
30
 *
31
 * If you are in a distributed environment, have a look at the GenericCacheAdapter
32
 * that can wrap any PSR-6 compatible cache implementations.
33
 *
34
 * @author     Tim Wagner <[email protected]>
35
 * @copyright  2021 TechDivision GmbH <[email protected]>
36
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link       https://github.com/techdivision/import
38
 * @link       http://www.techdivision.com
39
 * @deprecated Since 16.8.3
40
 * @see        \TechDivision\Import\Cache\Collection\LocalCacheAdapter
41
 */
42
class LocalCacheAdapter implements CacheAdapterInterface
0 ignored issues
show
Deprecated Code introduced by
The interface TechDivision\Import\Cache\CacheAdapterInterface has been deprecated with message: Since 16.8.3

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
43
{
44
45
    /**
46
     * Trait that provides custom cache adapter functionality.
47
     *
48
     * @var TechDivision\Import\Cache\CacheAdapterTrait
49
     */
50
    use CacheAdapterTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait TechDivision\Import\Cache\CacheAdapterTrait has been deprecated with message: Since 16.8.3

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
51
52
    /**
53
     * The array with the tags.
54
     *
55
     * @var array
56
     */
57
    protected $tags = array();
58
59
    /**
60
     * The cache for the query results.
61
     *
62
     * @var array
63
     */
64
    protected $cache = array();
65
66
    /**
67
     * References that links to another cache entry.
68
     *
69
     * @var array
70
     */
71
    protected $references = array();
72
73
    /**
74
     * The cache key utility instance.
75
     *
76
     * @var \TechDivision\Import\Utils\CacheKeyUtilInterface
77
     */
78
    protected $cacheKeyUtil;
79
80
    /**
81
     * Initialize the cache handler with the passed cache and configuration instances.
82
     *
83
     * @param \TechDivision\Import\Utils\CacheKeyUtilInterface $cacheKeyUtil The cache key utility instance
84
     */
85 20
    public function __construct(CacheKeyUtilInterface $cacheKeyUtil)
86
    {
87 20
        $this->cacheKeyUtil = $cacheKeyUtil;
88 20
    }
89
90
    /**
91
     * Creates a unique cache key from the passed data.
92
     *
93
     * @param mixed   $data      The date to create the cache key from
94
     * @param boolean $usePrefix Flag to signal using the prefix or not
95
     *
96
     * @return string The generated cache key
97
     */
98 20
    public function cacheKey($data, $usePrefix = true)
99
    {
100 20
        return $this->cacheKeyUtil->cacheKey($data, $usePrefix);
101
    }
102
103
    /**
104
     * Query whether or not a cache value for the passed cache key is available.
105
     *
106
     * @param string $key The cache key to query for
107
     *
108
     * @return boolean TRUE if the a value is available, else FALSE
109
     */
110 20
    public function isCached($key)
111
    {
112 20
        return isset($this->cache[$this->resolveReference($this->cacheKey($key))]);
113
    }
114
115
    /**
116
     * Inversion of the isCached() method.
117
     *
118
     * @param string $key The cache key to query for
119
     *
120
     * @return boolean TRUE if the value is not available, else FALSE
121
     */
122
    public function notCached($key)
123
    {
124
        return !$this->isCached($key);
125
    }
126
127
    /**
128
     * Add's a cache reference from one key to another.
129
     *
130
     * @param string $from The key to reference from
131
     * @param string $to   The key to reference to
132
     *
133
     * @return void
134
     */
135 20
    public function addReference($from, $to)
136
    {
137 20
        $this->references[$this->cacheKey($from)] = $this->cacheKey($to);
138 20
    }
139
140
    /**
141
     * Resolve's the cache key.
142
     *
143
     * @param string $from The cache key to resolve
144
     *
145
     * @return string The resolved reference
146
     */
147 20
    protected function resolveReference($from)
148
    {
149
150
        // query whether or not a reference exists
151 20
        if (isset($this->references[$from])) {
152 20
            return $this->references[$from];
153
        }
154
155
        // return the passed reference
156 20
        return $from;
157
    }
158
159
    /**
160
     * Add the passed item to the cache.
161
     *
162
     * @param string  $key        The cache key to use
163
     * @param mixed   $value      The value that has to be cached
164
     * @param array   $references An array with references to add
165
     * @param array   $tags       An array with tags to add
166
     * @param boolean $override   Flag that allows to override an exising cache entry
167
     * @param integer $time       The TTL in seconds for the passed item
168
     *
169
     * @return void
170
     */
171 20
    public function toCache($key, $value, array $references = array(), array $tags = array(), $override = true, $time = null)
172
    {
173
174
        // query whether or not the key has already been used
175 20
        if (isset($this->cache[$this->resolveReference($uniqueKey = $this->cacheKey($key))]) && $override === false) {
176
            throw new \Exception(
177
                sprintf(
178
                    'Try to override data with key "%s"',
179
                    $uniqueKey
180
                )
181
            );
182
        }
183
184
        // set the attribute in the registry
185 20
        $this->cache[$uniqueKey] = $value;
186
187
        // prepend the tags with the cache key
188 20
        array_walk($tags, function (&$tag) {
189
            $tag = $this->cacheKey($tag);
190 20
        });
191
192
        // tag the unique key
193 20
        foreach ($tags as $tag) {
194
            $this->tags[$tag][] = $uniqueKey;
195
        }
196
197
        // also register the references if given
198 20
        foreach ($references as $from => $to) {
199 20
            $this->addReference($from, $to);
200
        }
201 20
    }
202
203
    /**
204
     * Returns a new cache item for the passed key
205
     *
206
     * @param string $key The cache key to return the item for
207
     *
208
     * @return mixed The value for the passed key
209
     */
210
    public function fromCache($key)
211
    {
212
        if (isset($this->cache[$uniqueKey = $this->resolveReference($this->cacheKey($key))])) {
213
            return $this->cache[$uniqueKey];
214
        }
215
    }
216
217
    /**
218
     * Flush the cache and remove the references.
219
     *
220
     * @return void
221
     */
222
    public function flushCache()
223
    {
224
        $this->tags = array();
225
        $this->cache = array();
226
        $this->references = array();
227
    }
228
229
    /**
230
     * Invalidate the cache entries for the passed tags.
231
     *
232
     * @param array $tags The tags to invalidate the cache for
233
     *
234
     * @return void
235
     */
236
    public function invalidateTags(array $tags)
237
    {
238
239
        // prepend the tags with the cache key
240
        array_walk($tags, function (&$tag) {
241
            $tag = $this->cacheKey($tag);
242
        });
243
244
        // remove all the references of items that has one of the passed tags
245
        foreach ($tags as $tag) {
246
            if (isset($this->tags[$tag])) {
247
                foreach ($this->tags[$tag] as $to) {
248
                    // clean-up the references that reference to the key
249
                    if ($from = array_search($to, $this->references)) {
250
                        unset($this->references[$from]);
251
                    }
252
                    // clean-up the cache entry itself
253
                    if (isset($this->cache[$to])) {
254
                        unset($this->cache[$to]);
255
                    }
256
                }
257
            }
258
        }
259
    }
260
261
    /**
262
     * Remove the item with the passed key and all its references from the cache.
263
     *
264
     * @param string $key               The key of the cache item to Remove
265
     * @param bool   $cleanUpReferences TRUE if the references has to be cleaned-up, else FALSE (default)
266
     *
267
     * @return void
268
     */
269 10
    public function removeCache($key, $cleanUpReferences = false)
270
    {
271 10
        unset($this->cache[$this->resolveReference($uniqueKey = $this->cacheKey($key))]);
272
273
        // query whether or not we've to clean-up references
274 10
        if ($cleanUpReferences === true) {
275
            // load the keys of the references we want to remove
276
            $references = array_keys($this->references, $uniqueKey);
0 ignored issues
show
Bug introduced by
The variable $uniqueKey seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
277
278
            // remove ALL references to the passed unique key
279
            foreach ($references as $reference) {
280
                unset($this->references[$reference]);
281
            }
282
        }
283 10
    }
284
}
285