| | |
| | | |
| | | private static array $instances = []; |
| | | private bool $hasRedis; |
| | | private bool $varyByAuth = false; |
| | | private bool $varyByUser = false; |
| | | |
| | | private function __construct(string $group, int $ttl) |
| | | { |
| | |
| | | add_action('deleted_user_meta', [self::class, 'onUserMetaDelete'], 10, 2); |
| | | } |
| | | |
| | | public function auth(): static |
| | | { |
| | | $this->varyByAuth = true; |
| | | return $this; |
| | | } |
| | | |
| | | public function user(): static |
| | | { |
| | | $this->varyByUser = true; |
| | | return $this; |
| | | } |
| | | |
| | | private function resolveKey(int|string $key): string |
| | | { |
| | | if ($this->varyByUser) { |
| | | return $key . ':u' . get_current_user_id(); |
| | | } |
| | | if ($this->varyByAuth) { |
| | | return $key . ':' . (is_user_logged_in() ? 'auth' : 'guest'); |
| | | } |
| | | return (string) $key; |
| | | } |
| | | |
| | | /* --------------------------------------------------------------------- |
| | | * Factory |
| | | * ------------------------------------------------------------------- */ |
| | |
| | | ?int $ttl = null |
| | | ): mixed { |
| | | if (is_array($key)) { |
| | | $id = $this->generateKey($key); |
| | | $key = $this->generateKey($key); |
| | | } |
| | | $instanceTags = array_map(fn($tag) => [$this->group, $tag], $this->getTags()); |
| | | $tags = array_unique(array_merge( |
| | | $this->getTags(), |
| | | array_map('sanitize_key', $tags) |
| | | )); |
| | | $instanceTags, |
| | | array_map(fn($tag) => [sanitize_key($tag[0]), $tag[1]], $tags) |
| | | ), SORT_REGULAR); |
| | | |
| | | |
| | | $value = wp_cache_get($key, $this->group); |