Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
72.73% |
16 / 22 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
CacheTrait | |
72.73% |
16 / 22 |
|
25.00% |
1 / 4 |
10.64 | |
0.00% |
0 / 1 |
setCacheStorage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCachedData | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
putCachedData | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
removeCachedData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Trait for caching data. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2007. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Cache |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development:plugins:ils_drivers Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Cache; |
31 | |
32 | use Laminas\Cache\Storage\StorageInterface; |
33 | |
34 | /** |
35 | * Trait for caching data. |
36 | * |
37 | * @category VuFind |
38 | * @package Cache |
39 | * @author Demian Katz <demian.katz@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/development:plugins:ils_drivers Wiki |
42 | */ |
43 | trait CacheTrait |
44 | { |
45 | use KeyGeneratorTrait; |
46 | |
47 | /** |
48 | * Cache for storing data temporarily (e.g. patron blocks with the ILS driver) |
49 | * |
50 | * @var StorageInterface |
51 | */ |
52 | protected $cache = null; |
53 | |
54 | /** |
55 | * Lifetime of cache (in seconds). |
56 | * |
57 | * @var int |
58 | */ |
59 | protected $cacheLifetime = 30; |
60 | |
61 | /** |
62 | * Set a cache storage object. |
63 | * |
64 | * @param StorageInterface $cache Cache storage interface |
65 | * |
66 | * @return void |
67 | */ |
68 | public function setCacheStorage(StorageInterface $cache = null) |
69 | { |
70 | $this->cache = $cache; |
71 | } |
72 | |
73 | /** |
74 | * Helper function for fetching cached data. |
75 | * |
76 | * Data is cached for up to $this->cacheLifetime. |
77 | * |
78 | * @param string $key Cache entry key |
79 | * |
80 | * @return mixed|null Cached entry or null if not cached or expired |
81 | */ |
82 | protected function getCachedData($key) |
83 | { |
84 | // No cache object, no cached results! |
85 | if (null === $this->cache) { |
86 | return null; |
87 | } |
88 | |
89 | $fullKey = $this->getCacheKey($key); |
90 | $item = $this->cache->getItem($fullKey); |
91 | if (null !== $item) { |
92 | // Return value if still valid: |
93 | $lifetime = $item['lifetime'] ?? $this->cacheLifetime; |
94 | if (time() - $item['time'] <= $lifetime) { |
95 | return $item['entry']; |
96 | } |
97 | // Clear expired item from cache: |
98 | $this->cache->removeItem($fullKey); |
99 | } |
100 | return null; |
101 | } |
102 | |
103 | /** |
104 | * Helper function for storing cached data. |
105 | * |
106 | * Data is cached for up to $this->cacheLifetime seconds. |
107 | * |
108 | * @param string $key Cache entry key |
109 | * @param mixed $entry Entry to be cached |
110 | * @param int $lifetime Optional lifetime for the entry in seconds |
111 | * |
112 | * @return void |
113 | */ |
114 | protected function putCachedData($key, $entry, $lifetime = null) |
115 | { |
116 | // Don't write to cache if we don't have a cache! |
117 | if (null === $this->cache) { |
118 | return; |
119 | } |
120 | $item = [ |
121 | 'time' => time(), |
122 | 'lifetime' => $lifetime, |
123 | 'entry' => $entry, |
124 | ]; |
125 | $this->cache->setItem($this->getCacheKey($key), $item); |
126 | } |
127 | |
128 | /** |
129 | * Helper function for removing cached data. |
130 | * |
131 | * @param string $key Cache entry key |
132 | * |
133 | * @return void |
134 | */ |
135 | protected function removeCachedData($key) |
136 | { |
137 | // Don't write to cache if we don't have a cache! |
138 | if (null === $this->cache) { |
139 | return; |
140 | } |
141 | $this->cache->removeItem($this->getCacheKey($key)); |
142 | } |
143 | } |