PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
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
← All changes | src/Cache/Cache.php +117 -26 2.3.152.2.21 View file →
@@ -1,5 +1,18 @@
1 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Cache;
@@ -5,31 +18,79 @@
5 18 namespace UserAccessManager\Cache;
6 19
7 20 use UserAccessManager\Wrapper\Wordpress;
8 21
22 +/**
23 + * Class Cache
24 + *
25 + * @package UserAccessManager\Cache
26 + */
9 27 class Cache
10 28 {
11 - private ?CacheProviderInterface $cacheProvider = null;
12 - private array $cache = [];
13 - private array $runtimeCache = [];
29 + /**
30 + * @var Wordpress
31 + */
32 + private $wordpress;
14 33
15 - public function __construct(
16 - private Wordpress $wordpress,
17 - private CacheProviderFactory $cacheProviderFactory
18 - ) {
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;
19 63 }
20 64
65 + /**
66 + * Return the cache provider.
67 + * @return CacheProviderInterface|null
68 + */
21 69 public function getCacheProvider(): ?CacheProviderInterface
22 70 {
23 71 return $this->cacheProvider;
24 72 }
25 73
26 - public function setActiveCacheProvider(?string $key): void
74 + /**
75 + * Sets a cache provider
76 + * @param null|string $key
77 + */
78 + public function setActiveCacheProvider(?string $key)
27 79 {
28 - $this->cacheProvider = $this->getRegisteredCacheProviders()[$key] ?? null;
29 - $this->cacheProvider?->init();
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 + }
30 87 }
31 88
89 + /**
90 + * Returns a generated cache key.
91 + * @return string
92 + */
32 93 public function generateCacheKey(): string
33 94 {
34 95 $arguments = func_get_args();
35 96
@@ -35,15 +96,28 @@
35 96
36 97 return implode('|', $arguments);
37 98 }
38 99
39 - public function add(string $key, mixed $value): void
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)
40 106 {
41 - $this->cacheProvider?->add($key, $value);
107 + if ($this->cacheProvider !== null) {
108 + $this->cacheProvider->add($key, $value);
109 + }
110 +
42 111 $this->cache[$key] = $value;
43 112 }
44 113
45 - public function get(string $key): mixed
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)
46 120 {
47 121 if (isset($this->cache[$key]) === false) {
48 122 $this->cache[$key] = ($this->cacheProvider !== null) ? $this->cacheProvider->get($key) : null;
49 123 }
@@ -50,20 +124,37 @@
50 124
51 125 return $this->cache[$key];
52 126 }
53 127
54 - public function invalidate(string $key): void
128 + /**
129 + * Invalidates the cached object.
130 + * @param string $key
131 + */
132 + public function invalidate(string $key)
55 133 {
56 - $this->cacheProvider?->invalidate($key);
134 + if ($this->cacheProvider !== null) {
135 + $this->cacheProvider->invalidate($key);
136 + }
137 +
57 138 unset($this->cache[$key]);
58 139 }
59 140
60 - public function addToRuntimeCache(string $key, mixed $value): void
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)
61 147 {
62 148 $this->runtimeCache[$key] = $value;
63 149 }
64 150
65 - public function getFromRuntimeCache(string $key): mixed
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)
66 157 {
67 158 if (isset($this->runtimeCache[$key]) === true) {
68 159 return $this->runtimeCache[$key];
69 160 }
@@ -70,10 +161,12 @@
70 161
71 162 return null;
72 163 }
73 164
74 -
75 - public function flushCache(): void
165 + /**
166 + * Flushes the cache.
167 + */
168 + public function flushCache()
76 169 {
77 170 $this->cache = [];
78 171 $this->runtimeCache = [];
79 172 }
@@ -78,19 +171,17 @@
78 171 $this->runtimeCache = [];
79 172 }
80 173
81 174 /**
175 + * Returns a list of the registered cache handlers.
82 176 * @return CacheProviderInterface[]
83 177 */
84 178 public function getRegisteredCacheProviders(): array
85 179 {
86 180 $fileSystemCacheProvider = $this->cacheProviderFactory->createFileSystemCacheProvider();
87 - $providers = [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider];
88 181
89 - if ($this->wordpress->isUsingExtObjectCache() === true) {
90 - $redisCacheProvider = $this->cacheProviderFactory->createRedisCacheProvider();
91 - $providers[$redisCacheProvider->getId()] = $redisCacheProvider;
92 - }
93 -
94 - return $this->wordpress->applyFilters('uam_registered_cache_handlers', $providers);
182 + return $this->wordpress->applyFilters(
183 + 'uam_registered_cache_handlers',
184 + [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider]
185 + );
95 186 }
96 187 }