PluginProbe
User Access Manager / trunk
User Access Manager vtrunk
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 / RedisCacheProvider.php

RedisCacheProvider.php in User Access Manager trunk, at src/Cache/RedisCacheProvider.php

99 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Cache;
6
7 use Exception;
8 use UserAccessManager\Config\Config;
9 use UserAccessManager\Config\ConfigFactory;
10 use UserAccessManager\Config\Parameter\ConfigParameterFactory;
11 use UserAccessManager\Wrapper\Wordpress;
12
13 class RedisCacheProvider implements CacheProviderInterface
14 {
15 public const ID = 'RedisCacheProvider';
16 public const CONFIG_KEY = 'uam_redis_cache_provider';
17 public const CONFIG_PREFIX = 'redis_prefix';
18 public const CONFIG_TTL = 'redis_ttl';
19 public const DEFAULT_PREFIX = 'uam_cache';
20 public const DEFAULT_TTL = 0;
21
22 private ?Config $config = null;
23
24 public function __construct(
25 private Wordpress $wordpress,
26 private ConfigFactory $configFactory,
27 private ConfigParameterFactory $configParameterFactory
28 ) {
29 }
30
31 public function getId(): string
32 {
33 return self::ID;
34 }
35
36 public function init(): void
37 {
38 // WordPress object cache is already set up — nothing to initialize here.
39 }
40
41 /**
42 * @throws Exception
43 */
44 public function getConfig(): Config
45 {
46 if ($this->config === null) {
47 $this->config = $this->configFactory->createConfig(self::CONFIG_KEY);
48
49 $configParameters = [
50 self::CONFIG_PREFIX => $this->configParameterFactory->createStringConfigParameter(
51 self::CONFIG_PREFIX,
52 self::DEFAULT_PREFIX
53 ),
54 self::CONFIG_TTL => $this->configParameterFactory->createStringConfigParameter(
55 self::CONFIG_TTL,
56 (string) self::DEFAULT_TTL
57 ),
58 ];
59
60 $this->config->setDefaultConfigParameters($configParameters);
61 }
62
63 return $this->config;
64 }
65
66 private function getPrefix(): string
67 {
68 return (string) ($this->config?->getParameterValue(self::CONFIG_PREFIX) ?? self::DEFAULT_PREFIX);
69 }
70
71 private function getTtl(): int
72 {
73 return (int) ($this->config?->getParameterValue(self::CONFIG_TTL) ?? self::DEFAULT_TTL);
74 }
75
76 private function buildKey(string $key): string
77 {
78 return $this->getPrefix() . '|' . $key;
79 }
80
81 public function add(string $key, mixed $value): void
82 {
83 $this->wordpress->wpCacheSet($this->buildKey($key), $value, self::ID, $this->getTtl());
84 }
85
86 public function get(string $key): mixed
87 {
88 $value = $this->wordpress->wpCacheGet($this->buildKey($key), self::ID);
89
90 // wp_cache_get returns false on a miss; normalize to null per interface contract.
91 return ($value === false) ? null : $value;
92 }
93
94 public function invalidate(string $key): void
95 {
96 $this->wordpress->wpCacheDelete($this->buildKey($key), self::ID);
97 }
98 }
99