1 | <?php |
||
42 | class LocalCacheAdapter implements CacheAdapterInterface |
||
|
|||
43 | { |
||
44 | |||
45 | /** |
||
46 | * Trait that provides custom cache adapter functionality. |
||
47 | * |
||
48 | * @var TechDivision\Import\Cache\CacheAdapterTrait |
||
49 | */ |
||
50 | use CacheAdapterTrait; |
||
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) |
|
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) |
|
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) |
|
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) |
||
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) |
|
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) |
|
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) |
||
216 | |||
217 | /** |
||
218 | * Flush the cache and remove the references. |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function flushCache() |
||
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) |
||
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) |
|
284 | } |
||
285 |
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.