|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WP AI Client: WP_AI_Client_Cache class |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage AI |
| 7 | + * @since 7.0.0 |
| 8 | + */ |
| 9 | + |
| 10 | +use WordPress\AiClientDependencies\Psr\SimpleCache\CacheInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * WordPress-specific PSR-16 cache adapter for the AI Client. |
| 14 | + * |
| 15 | + * Bridges PSR-16 cache operations to WordPress object cache functions, |
| 16 | + * enabling the AI client to leverage WordPress caching infrastructure. |
| 17 | + * |
| 18 | + * @since 7.0.0 |
| 19 | + */ |
| 20 | +class WP_AI_Client_Cache implements CacheInterface { |
| 21 | + |
| 22 | + /** |
| 23 | + * Cache group used for all cache operations. |
| 24 | + * |
| 25 | + * @since 7.0.0 |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + private const CACHE_GROUP = 'wp_ai_client'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Fetches a value from the cache. |
| 32 | + * |
| 33 | + * @since 7.0.0 |
| 34 | + * |
| 35 | + * @param string $key The unique key of this item in the cache. |
| 36 | + * @param mixed $default_value Default value to return if the key does not exist. |
| 37 | + * @return mixed The value of the item from the cache, or $default_value in case of cache miss. |
| 38 | + */ |
| 39 | + public function get( $key, $default_value = null ) { |
| 40 | + $found = false; |
| 41 | + $value = wp_cache_get( $key, self::CACHE_GROUP, false, $found ); |
| 42 | + |
| 43 | + if ( ! $found ) { |
| 44 | + return $default_value; |
| 45 | + } |
| 46 | + |
| 47 | + return $value; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
| 52 | + * |
| 53 | + * @since 7.0.0 |
| 54 | + * |
| 55 | + * @param string $key The key of the item to store. |
| 56 | + * @param mixed $value The value of the item to store, must be serializable. |
| 57 | + * @param null|int|DateInterval $ttl Optional. The TTL value of this item. |
| 58 | + * @return bool True on success and false on failure. |
| 59 | + */ |
| 60 | + public function set( $key, $value, $ttl = null ): bool { |
| 61 | + $expire = $this->ttl_to_seconds( $ttl ); |
| 62 | + |
| 63 | + return wp_cache_set( $key, $value, self::CACHE_GROUP, $expire ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Delete an item from the cache by its unique key. |
| 68 | + * |
| 69 | + * @since 7.0.0 |
| 70 | + * |
| 71 | + * @param string $key The unique cache key of the item to delete. |
| 72 | + * @return bool True if the item was successfully removed. False if there was an error. |
| 73 | + */ |
| 74 | + public function delete( $key ): bool { |
| 75 | + return wp_cache_delete( $key, self::CACHE_GROUP ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Wipes clean the entire cache's keys. |
| 80 | + * |
| 81 | + * This method only clears the cache group used by this adapter. If the underlying |
| 82 | + * cache implementation does not support group flushing, this method returns false. |
| 83 | + * |
| 84 | + * @since 7.0.0 |
| 85 | + * |
| 86 | + * @return bool True on success and false on failure. |
| 87 | + */ |
| 88 | + public function clear(): bool { |
| 89 | + if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + return wp_cache_flush_group( self::CACHE_GROUP ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Obtains multiple cache items by their unique keys. |
| 98 | + * |
| 99 | + * @since 7.0.0 |
| 100 | + * |
| 101 | + * @param iterable<string> $keys A list of keys that can be obtained in a single operation. |
| 102 | + * @param mixed $default_value Default value to return for keys that do not exist. |
| 103 | + * @return array<string, mixed> A list of key => value pairs. |
| 104 | + */ |
| 105 | + public function getMultiple( $keys, $default_value = null ) { |
| 106 | + /** |
| 107 | + * Keys array. |
| 108 | + * |
| 109 | + * @var array<string> $keys_array |
| 110 | + */ |
| 111 | + $keys_array = $this->iterable_to_array( $keys ); |
| 112 | + $values = wp_cache_get_multiple( $keys_array, self::CACHE_GROUP ); |
| 113 | + $result = array(); |
| 114 | + |
| 115 | + foreach ( $keys_array as $key ) { |
| 116 | + $result[ $key ] = isset( $values[ $key ] ) && false !== $values[ $key ] ? $values[ $key ] : $default_value; |
| 117 | + } |
| 118 | + |
| 119 | + return $result; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Persists a set of key => value pairs in the cache, with an optional TTL. |
| 124 | + * |
| 125 | + * @since 7.0.0 |
| 126 | + * |
| 127 | + * @param iterable<string, mixed> $values A list of key => value pairs for a multiple-set operation. |
| 128 | + * @param null|int|DateInterval $ttl Optional. The TTL value of this item. |
| 129 | + * @return bool True on success and false on failure. |
| 130 | + */ |
| 131 | + public function setMultiple( $values, $ttl = null ): bool { |
| 132 | + $values_array = $this->iterable_to_array( $values ); |
| 133 | + $expire = $this->ttl_to_seconds( $ttl ); |
| 134 | + $results = wp_cache_set_multiple( $values_array, self::CACHE_GROUP, $expire ); |
| 135 | + |
| 136 | + // Return true only if all operations succeeded. |
| 137 | + return ! in_array( false, $results, true ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Deletes multiple cache items in a single operation. |
| 142 | + * |
| 143 | + * @since 7.0.0 |
| 144 | + * |
| 145 | + * @param iterable<string> $keys A list of string-based keys to be deleted. |
| 146 | + * @return bool True if the items were successfully removed. False if there was an error. |
| 147 | + */ |
| 148 | + public function deleteMultiple( $keys ): bool { |
| 149 | + $keys_array = $this->iterable_to_array( $keys ); |
| 150 | + $results = wp_cache_delete_multiple( $keys_array, self::CACHE_GROUP ); |
| 151 | + |
| 152 | + // Return true only if all operations succeeded. |
| 153 | + return ! in_array( false, $results, true ); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Determines whether an item is present in the cache. |
| 158 | + * |
| 159 | + * @since 7.0.0 |
| 160 | + * |
| 161 | + * @param string $key The cache item key. |
| 162 | + * @return bool True if the item exists in the cache, false otherwise. |
| 163 | + */ |
| 164 | + public function has( $key ): bool { |
| 165 | + $found = false; |
| 166 | + wp_cache_get( $key, self::CACHE_GROUP, false, $found ); |
| 167 | + |
| 168 | + return (bool) $found; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Converts a PSR-16 TTL value to seconds for WordPress cache functions. |
| 173 | + * |
| 174 | + * @since 7.0.0 |
| 175 | + * |
| 176 | + * @param null|int|DateInterval $ttl The TTL value. |
| 177 | + * @return int The TTL in seconds, or 0 for no expiration. |
| 178 | + */ |
| 179 | + private function ttl_to_seconds( $ttl ): int { |
| 180 | + if ( null === $ttl ) { |
| 181 | + return 0; |
| 182 | + } |
| 183 | + |
| 184 | + if ( $ttl instanceof DateInterval ) { |
| 185 | + $now = new DateTime(); |
| 186 | + $end = ( clone $now )->add( $ttl ); |
| 187 | + |
| 188 | + return $end->getTimestamp() - $now->getTimestamp(); |
| 189 | + } |
| 190 | + |
| 191 | + return max( 0, (int) $ttl ); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Converts an iterable to an array. |
| 196 | + * |
| 197 | + * @since 7.0.0 |
| 198 | + * |
| 199 | + * @param iterable<mixed> $items The iterable to convert. |
| 200 | + * @return array<mixed> The array. |
| 201 | + */ |
| 202 | + private function iterable_to_array( $items ): array { |
| 203 | + if ( is_array( $items ) ) { |
| 204 | + return $items; |
| 205 | + } |
| 206 | + |
| 207 | + return iterator_to_array( $items ); |
| 208 | + } |
| 209 | +} |
0 commit comments