| @@ -1,36 +1,88 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * Cache.php | |
| 4 | + * | |
| 5 | + * The Cache class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 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 | + */ | |
| 5 | 15 | namespace UserAccessManager\Cache; |
| 6 | 16 | |
| 7 | 17 | use UserAccessManager\Wrapper\Wordpress; |
| 8 | 18 | |
| 19 | +/** | |
| 20 | + * Class Cache | |
| 21 | + * | |
| 22 | + * @package UserAccessManager\Cache | |
| 23 | + */ | |
| 9 | 24 | class Cache |
| 10 | 25 | { |
| 11 | - private ?CacheProviderInterface $cacheProvider = null; | |
| 12 | - private array $cache = []; | |
| 13 | - private array $runtimeCache = []; | |
| 26 | + /** | |
| 27 | + * @var Wordpress | |
| 28 | + */ | |
| 29 | + private $wordpress; | |
| 14 | 30 | |
| 15 | - public function __construct( | |
| 16 | - private Wordpress $wordpress, | |
| 17 | - private CacheProviderFactory $cacheProviderFactory | |
| 18 | - ) { | |
| 19 | - } | |
| 31 | + /** | |
| 32 | + * @var CacheProviderFactory | |
| 33 | + */ | |
| 34 | + private $cacheProviderFactory; | |
| 20 | 35 | |
| 21 | - public function getCacheProvider(): ?CacheProviderInterface | |
| 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) | |
| 22 | 58 | { |
| 23 | - return $this->cacheProvider; | |
| 59 | + $this->wordpress = $wordpress; | |
| 60 | + $this->cacheProviderFactory = $cacheProviderFactory; | |
| 24 | 61 | } |
| 25 | 62 | |
| 26 | - public function setActiveCacheProvider(?string $key): void | |
| 63 | + /** | |
| 64 | + * Sets a cache provider | |
| 65 | + * | |
| 66 | + * @param string $key | |
| 67 | + */ | |
| 68 | + public function setActiveCacheProvider($key) | |
| 27 | 69 | { |
| 28 | - $this->cacheProvider = $this->getRegisteredCacheProviders()[$key] ?? null; | |
| 29 | - $this->cacheProvider?->init(); | |
| 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 | + } | |
| 30 | 77 | } |
| 31 | 78 | |
| 32 | - public function generateCacheKey(): string | |
| 79 | + /** | |
| 80 | + * Returns a generated cache key. | |
| 81 | + * | |
| 82 | + * @return string | |
| 83 | + */ | |
| 84 | + public function generateCacheKey() | |
| 33 | 85 | { |
| 34 | 86 | $arguments = func_get_args(); |
| 35 | 87 | |
| 36 | 88 | return implode('|', $arguments); |
| @@ -35,15 +87,31 @@ | ||
| 35 | 87 | |
| 36 | 88 | return implode('|', $arguments); |
| 37 | 89 | } |
| 38 | 90 | |
| 39 | - public function add(string $key, mixed $value): void | |
| 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) | |
| 40 | 98 | { |
| 41 | - $this->cacheProvider?->add($key, $value); | |
| 99 | + if ($this->cacheProvider !== null) { | |
| 100 | + $this->cacheProvider->add($key, $value); | |
| 101 | + } | |
| 102 | + | |
| 42 | 103 | $this->cache[$key] = $value; |
| 43 | 104 | } |
| 44 | 105 | |
| 45 | - public function get(string $key): mixed | |
| 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) | |
| 46 | 114 | { |
| 47 | 115 | if (isset($this->cache[$key]) === false) { |
| 48 | 116 | $this->cache[$key] = ($this->cacheProvider !== null) ? $this->cacheProvider->get($key) : null; |
| 49 | 117 | } |
| @@ -50,20 +118,41 @@ | ||
| 50 | 118 | |
| 51 | 119 | return $this->cache[$key]; |
| 52 | 120 | } |
| 53 | 121 | |
| 54 | - public function invalidate(string $key): void | |
| 122 | + /** | |
| 123 | + * Invalidates the cached object. | |
| 124 | + * | |
| 125 | + * @param string $key | |
| 126 | + */ | |
| 127 | + public function invalidate($key) | |
| 55 | 128 | { |
| 56 | - $this->cacheProvider?->invalidate($key); | |
| 129 | + if ($this->cacheProvider !== null) { | |
| 130 | + $this->cacheProvider->invalidate($key); | |
| 131 | + } | |
| 132 | + | |
| 57 | 133 | unset($this->cache[$key]); |
| 58 | 134 | } |
| 59 | 135 | |
| 60 | - public function addToRuntimeCache(string $key, mixed $value): void | |
| 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) | |
| 61 | 143 | { |
| 62 | 144 | $this->runtimeCache[$key] = $value; |
| 63 | 145 | } |
| 64 | 146 | |
| 65 | - public function getFromRuntimeCache(string $key): mixed | |
| 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) | |
| 66 | 155 | { |
| 67 | 156 | if (isset($this->runtimeCache[$key]) === true) { |
| 68 | 157 | return $this->runtimeCache[$key]; |
| 69 | 158 | } |
| @@ -70,10 +159,12 @@ | ||
| 70 | 159 | |
| 71 | 160 | return null; |
| 72 | 161 | } |
| 73 | 162 | |
| 74 | - | |
| 75 | - public function flushCache(): void | |
| 163 | + /** | |
| 164 | + * Flushes the cache. | |
| 165 | + */ | |
| 166 | + public function flushCache() | |
| 76 | 167 | { |
| 77 | 168 | $this->cache = []; |
| 78 | 169 | $this->runtimeCache = []; |
| 79 | 170 | } |
| @@ -78,19 +169,18 @@ | ||
| 78 | 169 | $this->runtimeCache = []; |
| 79 | 170 | } |
| 80 | 171 | |
| 81 | 172 | /** |
| 173 | + * Returns a list of the registered cache handlers. | |
| 174 | + * | |
| 82 | 175 | * @return CacheProviderInterface[] |
| 83 | 176 | */ |
| 84 | - public function getRegisteredCacheProviders(): array | |
| 177 | + public function getRegisteredCacheProviders() | |
| 85 | 178 | { |
| 86 | 179 | $fileSystemCacheProvider = $this->cacheProviderFactory->createFileSystemCacheProvider(); |
| 87 | - $providers = [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider]; | |
| 88 | 180 | |
| 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); | |
| 181 | + return $this->wordpress->applyFilters( | |
| 182 | + 'uam_registered_cache_handlers', | |
| 183 | + [$fileSystemCacheProvider->getId() => $fileSystemCacheProvider] | |
| 184 | + ); | |
| 95 | 185 | } |
| 96 | 186 | } |