PluginProbe
User Access Manager / 2.2.1
User Access Manager v2.2.1
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.2.1, at src/Cache/Cache.php

188 lines 4.2 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
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Cache;
19
20 use UserAccessManager\Wrapper\Wordpress;
21
22 /**
23 * Class Cache
24 *
25 * @package UserAccessManager\Cache
26 */
27 class Cache
28 {
29 /**
30 * @var Wordpress
31 */
32 private $wordpress;
33
34 /**
35 * @var CacheProviderFactory
36 */
37 private $cacheProviderFactory;
38
39 /**
40 * @var null|CacheProviderInterface
41 */
42 private $cacheProvider = null;
43
44 /**
45 * @var array
46 */
47 private $cache = [];
48
49 /**
50 * @var array
51 */
52 private $runtimeCache = [];
53
54 /**
55 * Cache constructor.
56 * @param Wordpress $wordpress
57 * @param CacheProviderFactory $cacheProviderFactory
58 */
59 public function __construct(Wordpress $wordpress, CacheProviderFactory $cacheProviderFactory)
60 {
61 $this->wordpress = $wordpress;
62 $this->cacheProviderFactory = $cacheProviderFactory;
63 }
64
65 /**
66 * Return the cache provider.
67 * @return CacheProviderInterface|null
68 */
69 public function getCacheProvider(): ?CacheProviderInterface
70 {
71 return $this->cacheProvider;
72 }
73
74 /**
75 * Sets a cache provider
76 * @param null|string $key
77 */
78 public function setActiveCacheProvider(?string $key)
79 {
80 $cacheProviders = $this->getRegisteredCacheProviders();
81
82 $this->cacheProvider = (isset($cacheProviders[$key]) === true) ? $cacheProviders[$key] : null;
83
84 if ($this->cacheProvider !== null) {
85 $this->cacheProvider->init();
86 }
87 }
88
89 /**
90 * Returns a generated cache key.
91 * @return string
92 */
93 public function generateCacheKey(): string
94 {
95 $arguments = func_get_args();
96
97 return implode('|', $arguments);
98 }
99
100 /**
101 * Adds the variable to the cache.
102 * @param string $key The cache key
103 * @param mixed $value The value.
104 */
105 public function add(string $key, $value)
106 {
107 if ($this->cacheProvider !== null) {
108 $this->cacheProvider->add($key, $value);
109 }
110
111 $this->cache[$key] = $value;
112 }
113
114 /**
115 * Returns a value from the cache by the given key.
116 * @param string $key
117 * @return mixed
118 */
119 public function get(string $key)
120 {
121 if (isset($this->cache[$key]) === false) {
122 $this->cache[$key] = ($this->cacheProvider !== null) ? $this->cacheProvider->get($key) : null;
123 }
124
125 return $this->cache[$key];
126 }
127
128 /**
129 * Invalidates the cached object.
130 * @param string $key
131 */
132 public function invalidate(string $key)
133 {
134 if ($this->cacheProvider !== null) {
135 $this->cacheProvider->invalidate($key);
136 }
137
138 unset($this->cache[$key]);
139 }
140
141 /**
142 * Adds the variable to the runtime cache.
143 * @param string $key The cache key
144 * @param mixed $value The value.
145 */
146 public function addToRuntimeCache(string $key, $value)
147 {
148 $this->runtimeCache[$key] = $value;
149 }
150
151 /**
152 * Returns a value from the runtime cache by the given key.
153 * @param string $key
154 * @return mixed
155 */
156 public function getFromRuntimeCache(string $key)
157 {
158 if (isset($this->runtimeCache[$key]) === true) {
159 return $this->runtimeCache[$key];
160 }
161
162 return null;
163 }
164
165 /**
166 * Flushes the cache.
167 */
168 public function flushCache()
169 {
170 $this->cache = [];
171 $this->runtimeCache = [];
172 }
173
174 /**
175 * Returns a list of the registered cache handlers.
176 * @return CacheProviderInterface[]
177 */
178 public function getRegisteredCacheProviders(): array
179 {
180 $fileSystemCacheProvider = $this->cacheProviderFactory->createFileSystemCacheProvider();
181
182 return $this->wordpress->applyFilters(
183 'uam_registered_cache_handlers',
184 [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider]
185 );
186 }
187 }
188