Ticket #65127: 65127.patch
| File 65127.patch, 5.5 KB (added by , 2 months ago) |
|---|
-
src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php
diff --git a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php index 4550489748..f8c35bf4d7 100644
a b class WP_AI_Client_Cache implements CacheInterface { 29 29 */ 30 30 private const CACHE_GROUP = 'wp_ai_client'; 31 31 32 /** 33 * Retrieves the cache group used for cache operations, applying a filter for customization. 34 * 35 * @since 7.1.0 36 * 37 * @return string Cache group name. 38 */ 39 private function get_cache_group(): string { 40 /** 41 * Filter the cache group used by the WP AI Client cache adapter. 42 * 43 * Allows integrators to change the object cache group under which AI client 44 * items are stored. 45 * 46 * @since 7.1.0 47 * 48 * @param string $group The cache group. 49 */ 50 return (string) apply_filters( 'wp_ai_client_cache_group', self::CACHE_GROUP ); 51 } 52 32 53 /** 33 54 * Fetches a value from the cache. 34 55 * … … class WP_AI_Client_Cache implements CacheInterface { 40 61 */ 41 62 public function get( $key, $default_value = null ) { 42 63 $found = false; 43 $value = wp_cache_get( $key, self::CACHE_GROUP, false, $found );64 $value = wp_cache_get( $key, $this->get_cache_group(), false, $found ); 44 65 45 66 if ( ! $found ) { 46 67 return $default_value; … … class WP_AI_Client_Cache implements CacheInterface { 62 83 public function set( $key, $value, $ttl = null ): bool { 63 84 $expire = $this->ttl_to_seconds( $ttl ); 64 85 65 return wp_cache_set( $key, $value, self::CACHE_GROUP, $expire );86 return wp_cache_set( $key, $value, $this->get_cache_group(), $expire ); 66 87 } 67 88 68 89 /** … … class WP_AI_Client_Cache implements CacheInterface { 74 95 * @return bool True if the item was successfully removed. False if there was an error. 75 96 */ 76 97 public function delete( $key ): bool { 77 return wp_cache_delete( $key, self::CACHE_GROUP);98 return wp_cache_delete( $key, $this->get_cache_group() ); 78 99 } 79 100 80 101 /** … … class WP_AI_Client_Cache implements CacheInterface { 92 113 return false; 93 114 } 94 115 95 return wp_cache_flush_group( self::CACHE_GROUP);116 return wp_cache_flush_group( $this->get_cache_group() ); 96 117 } 97 118 98 119 /** … … class WP_AI_Client_Cache implements CacheInterface { 111 132 * @var array<string> $keys_array 112 133 */ 113 134 $keys_array = $this->iterable_to_array( $keys ); 114 $values = wp_cache_get_multiple( $keys_array, self::CACHE_GROUP);135 $values = wp_cache_get_multiple( $keys_array, $this->get_cache_group() ); 115 136 $result = array(); 116 137 117 138 foreach ( $keys_array as $key ) { … … class WP_AI_Client_Cache implements CacheInterface { 138 159 public function setMultiple( $values, $ttl = null ): bool { 139 160 $values_array = $this->iterable_to_array( $values ); 140 161 $expire = $this->ttl_to_seconds( $ttl ); 141 $results = wp_cache_set_multiple( $values_array, self::CACHE_GROUP, $expire );162 $results = wp_cache_set_multiple( $values_array, $this->get_cache_group(), $expire ); 142 163 143 164 // Return true only if all operations succeeded. 144 165 return ! in_array( false, $results, true ); … … class WP_AI_Client_Cache implements CacheInterface { 154 175 */ 155 176 public function deleteMultiple( $keys ): bool { 156 177 $keys_array = $this->iterable_to_array( $keys ); 157 $results = wp_cache_delete_multiple( $keys_array, self::CACHE_GROUP);178 $results = wp_cache_delete_multiple( $keys_array, $this->get_cache_group() ); 158 179 159 180 // Return true only if all operations succeeded. 160 181 return ! in_array( false, $results, true ); … … class WP_AI_Client_Cache implements CacheInterface { 170 191 */ 171 192 public function has( $key ): bool { 172 193 $found = false; 173 wp_cache_get( $key, self::CACHE_GROUP, false, $found );194 wp_cache_get( $key, $this->get_cache_group(), false, $found ); 174 195 175 196 return (bool) $found; 176 197 } -
tests/phpunit/tests/ai-client/wpAiClientCache.php
diff --git a/tests/phpunit/tests/ai-client/wpAiClientCache.php b/tests/phpunit/tests/ai-client/wpAiClientCache.php index 690b8b9668..e214e73b75 100644
a b class Tests_AI_Client_Cache extends WP_UnitTestCase { 218 218 $this->assertTrue( $this->cache->set( 'key1', 'value1', $ttl ) ); 219 219 $this->assertSame( 'value1', $this->cache->get( 'key1' ) ); 220 220 } 221 222 /** 223 * Test that the cache group filter is respected. 224 * 225 * @ticket 64591 226 */ 227 public function test_cache_group_filter_is_respected() { 228 add_filter( 'wp_ai_client_cache_group', function( $group ) { 229 return 'wp_ai_client_tests_group'; 230 } ); 231 232 $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); 233 $this->assertTrue( $set ); 234 235 // Directly read from the underlying object cache using the expected group. 236 $value = wp_cache_get( 'ai_test_key', 'wp_ai_client_tests_group' ); 237 $this->assertSame( 'ai_value', $value ); 238 } 239 240 /** 241 * Test that a non-string cache group filter value is cast to string. 242 * 243 * @ticket 64591 244 */ 245 public function test_cache_group_filter_returns_non_string() { 246 add_filter( 'wp_ai_client_cache_group', function( $group ) { 247 return 12345; // Non-string value. 248 } ); 249 250 $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); 251 $this->assertTrue( $set ); 252 253 // Directly read from the underlying object cache using the expected group (cast to string). 254 $value = wp_cache_get( 'ai_test_key', '12345' ); 255 $this->assertSame( 'ai_value', $value ); 256 } 221 257 }