PluginProbe
User Access Manager / 2.1.8
User Access Manager v2.1.8
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Cache / Cache.php

Cache.php in User Access Manager 2.1.8, at src/Cache/Cache.php

187 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cache.php
4 *
5 * The Cache class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15 namespace UserAccessManager\Cache;
16
17 use UserAccessManager\Wrapper\Wordpress;
18
19 /**
20 * Class Cache
21 *
22 * @package UserAccessManager\Cache
23 */
24 class Cache
25 {
26 /**
27 * @var Wordpress
28 */
29 private $wordpress;
30
31 /**
32 * @var CacheProviderFactory
33 */
34 private $cacheProviderFactory;
35
36 /**
37 * @var null|CacheProviderInterface
38 */
39 private $cacheProvider = null;
40
41 /**
42 * @var array
43 */
44 private $cache = [];
45
46 /**
47 * @var array
48 */
49 private $runtimeCache = [];
50
51 /**
52 * Cache constructor.
53 *
54 * @param Wordpress $wordpress
55 * @param CacheProviderFactory $cacheProviderFactory
56 */
57 public function __construct(Wordpress $wordpress, CacheProviderFactory $cacheProviderFactory)
58 {
59 $this->wordpress = $wordpress;
60 $this->cacheProviderFactory = $cacheProviderFactory;
61 }
62
63 /**
64 * Sets a cache provider
65 *
66 * @param string $key
67 */
68 public function setActiveCacheProvider($key)
69 {
70 $cacheProviders = $this->getRegisteredCacheProviders();
71
72 $this->cacheProvider = (isset($cacheProviders[$key]) === true) ? $cacheProviders[$key] : null;
73
74 if ($this->cacheProvider !== null) {
75 $this->cacheProvider->init();
76 }
77 }
78
79 /**
80 * Returns a generated cache key.
81 *
82 * @return string
83 */
84 public function generateCacheKey()
85 {
86 $arguments = func_get_args();
87
88 return implode('|', $arguments);
89 }
90
91 /**
92 * Adds the variable to the cache.
93 *
94 * @param string $key The cache key
95 * @param mixed $value The value.
96 */
97 public function add($key, $value)
98 {
99 if ($this->cacheProvider !== null) {
100 $this->cacheProvider->add($key, $value);
101 }
102
103 $this->cache[$key] = $value;
104 }
105
106 /**
107 * Returns a value from the cache by the given key.
108 *
109 * @param string $key
110 *
111 * @return mixed
112 */
113 public function get($key)
114 {
115 if (isset($this->cache[$key]) === false) {
116 $this->cache[$key] = ($this->cacheProvider !== null) ? $this->cacheProvider->get($key) : null;
117 }
118
119 return $this->cache[$key];
120 }
121
122 /**
123 * Invalidates the cached object.
124 *
125 * @param string $key
126 */
127 public function invalidate($key)
128 {
129 if ($this->cacheProvider !== null) {
130 $this->cacheProvider->invalidate($key);
131 }
132
133 unset($this->cache[$key]);
134 }
135
136 /**
137 * Adds the variable to the runtime cache.
138 *
139 * @param string $key The cache key
140 * @param mixed $value The value.
141 */
142 public function addToRuntimeCache($key, $value)
143 {
144 $this->runtimeCache[$key] = $value;
145 }
146
147 /**
148 * Returns a value from the runtime cache by the given key.
149 *
150 * @param string $key
151 *
152 * @return mixed
153 */
154 public function getFromRuntimeCache($key)
155 {
156 if (isset($this->runtimeCache[$key]) === true) {
157 return $this->runtimeCache[$key];
158 }
159
160 return null;
161 }
162
163 /**
164 * Flushes the cache.
165 */
166 public function flushCache()
167 {
168 $this->cache = [];
169 $this->runtimeCache = [];
170 }
171
172 /**
173 * Returns a list of the registered cache handlers.
174 *
175 * @return CacheProviderInterface[]
176 */
177 public function getRegisteredCacheProviders()
178 {
179 $fileSystemCacheProvider = $this->cacheProviderFactory->createFileSystemCacheProvider();
180
181 return $this->wordpress->applyFilters(
182 'uam_registered_cache_handlers',
183 [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider]
184 );
185 }
186 }
187