# user-access-manager/trunk/src/Cache/RedisCacheProvider.php

User Access Manager, version trunk. 99 lines.

- Page: https://pluginprobe.com/plugins/user-access-manager/trunk/code/src/Cache/RedisCacheProvider.php
- Raw: https://pluginprobe.com/plugins/user-access-manager/trunk/raw/src/Cache/RedisCacheProvider.php
- Modified: 2026-08-06T11:11:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/user-access-manager/trunk/code/src/Cache/RedisCacheProvider.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace UserAccessManager\Cache;

use Exception;
use UserAccessManager\Config\Config;
use UserAccessManager\Config\ConfigFactory;
use UserAccessManager\Config\Parameter\ConfigParameterFactory;
use UserAccessManager\Wrapper\Wordpress;

class RedisCacheProvider implements CacheProviderInterface
{
    public const ID = 'RedisCacheProvider';
    public const CONFIG_KEY = 'uam_redis_cache_provider';
    public const CONFIG_PREFIX = 'redis_prefix';
    public const CONFIG_TTL = 'redis_ttl';
    public const DEFAULT_PREFIX = 'uam_cache';
    public const DEFAULT_TTL = 0;

    private ?Config $config = null;

    public function __construct(
        private Wordpress $wordpress,
        private ConfigFactory $configFactory,
        private ConfigParameterFactory $configParameterFactory
    ) {
    }

    public function getId(): string
    {
        return self::ID;
    }

    public function init(): void
    {
        // WordPress object cache is already set up — nothing to initialize here.
    }

    /**
     * @throws Exception
     */
    public function getConfig(): Config
    {
        if ($this->config === null) {
            $this->config = $this->configFactory->createConfig(self::CONFIG_KEY);

            $configParameters = [
                self::CONFIG_PREFIX => $this->configParameterFactory->createStringConfigParameter(
                    self::CONFIG_PREFIX,
                    self::DEFAULT_PREFIX
                ),
                self::CONFIG_TTL => $this->configParameterFactory->createStringConfigParameter(
                    self::CONFIG_TTL,
                    (string) self::DEFAULT_TTL
                ),
            ];

            $this->config->setDefaultConfigParameters($configParameters);
        }

        return $this->config;
    }

    private function getPrefix(): string
    {
        return (string) ($this->config?->getParameterValue(self::CONFIG_PREFIX) ?? self::DEFAULT_PREFIX);
    }

    private function getTtl(): int
    {
        return (int) ($this->config?->getParameterValue(self::CONFIG_TTL) ?? self::DEFAULT_TTL);
    }

    private function buildKey(string $key): string
    {
        return $this->getPrefix() . '|' . $key;
    }

    public function add(string $key, mixed $value): void
    {
        $this->wordpress->wpCacheSet($this->buildKey($key), $value, self::ID, $this->getTtl());
    }

    public function get(string $key): mixed
    {
        $value = $this->wordpress->wpCacheGet($this->buildKey($key), self::ID);

        // wp_cache_get returns false on a miss; normalize to null per interface contract.
        return ($value === false) ? null : $value;
    }

    public function invalidate(string $key): void
    {
        $this->wordpress->wpCacheDelete($this->buildKey($key), self::ID);
    }
}

```
