Skip to content

Commit 87a5738

Browse files
committed
Allow customization of the AI Client object cache group
Introduces the `wp_ai_client_cache_group` filter to allow developers to customize the object cache group used by `WP_AI_Client_Cache`. Includes tests for using a custom cache group and casting non-string filter values to strings. Props baikaresandeep007-1, apermo. Fixes #65127. git-svn-id: https://develop.svn.wordpress.org/trunk@62557 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 50ad424 commit 87a5738

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ class WP_AI_Client_Cache implements CacheInterface {
2929
*/
3030
private const CACHE_GROUP = 'wp_ai_client';
3131

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+
* Filters 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. This is useful for avoiding key collisions, creating
45+
* environment-specific caches, or adapting to backend constraints.
46+
*
47+
* @since 7.1.0
48+
*
49+
* @param string $group The cache group.
50+
*/
51+
return (string) apply_filters( 'wp_ai_client_cache_group', self::CACHE_GROUP );
52+
}
53+
3254
/**
3355
* Fetches a value from the cache.
3456
*
@@ -40,7 +62,7 @@ class WP_AI_Client_Cache implements CacheInterface {
4062
*/
4163
public function get( $key, $default_value = null ) {
4264
$found = false;
43-
$value = wp_cache_get( $key, self::CACHE_GROUP, false, $found );
65+
$value = wp_cache_get( $key, $this->get_cache_group(), false, $found );
4466

4567
if ( ! $found ) {
4668
return $default_value;
@@ -62,7 +84,7 @@ public function get( $key, $default_value = null ) {
6284
public function set( $key, $value, $ttl = null ): bool {
6385
$expire = $this->ttl_to_seconds( $ttl );
6486

65-
return wp_cache_set( $key, $value, self::CACHE_GROUP, $expire );
87+
return wp_cache_set( $key, $value, $this->get_cache_group(), $expire );
6688
}
6789

6890
/**
@@ -74,7 +96,7 @@ public function set( $key, $value, $ttl = null ): bool {
7496
* @return bool True if the item was successfully removed. False if there was an error.
7597
*/
7698
public function delete( $key ): bool {
77-
return wp_cache_delete( $key, self::CACHE_GROUP );
99+
return wp_cache_delete( $key, $this->get_cache_group() );
78100
}
79101

80102
/**
@@ -92,7 +114,7 @@ public function clear(): bool {
92114
return false;
93115
}
94116

95-
return wp_cache_flush_group( self::CACHE_GROUP );
117+
return wp_cache_flush_group( $this->get_cache_group() );
96118
}
97119

98120
/**
@@ -111,7 +133,7 @@ public function getMultiple( $keys, $default_value = null ): array {
111133
* @var array<string> $keys_array
112134
*/
113135
$keys_array = $this->iterable_to_array( $keys );
114-
$values = wp_cache_get_multiple( $keys_array, self::CACHE_GROUP );
136+
$values = wp_cache_get_multiple( $keys_array, $this->get_cache_group() );
115137
$result = array();
116138

117139
foreach ( $keys_array as $key ) {
@@ -138,7 +160,7 @@ public function getMultiple( $keys, $default_value = null ): array {
138160
public function setMultiple( $values, $ttl = null ): bool {
139161
$values_array = $this->iterable_to_array( $values );
140162
$expire = $this->ttl_to_seconds( $ttl );
141-
$results = wp_cache_set_multiple( $values_array, self::CACHE_GROUP, $expire );
163+
$results = wp_cache_set_multiple( $values_array, $this->get_cache_group(), $expire );
142164

143165
// Return true only if all operations succeeded.
144166
return ! in_array( false, $results, true );
@@ -154,7 +176,7 @@ public function setMultiple( $values, $ttl = null ): bool {
154176
*/
155177
public function deleteMultiple( $keys ): bool {
156178
$keys_array = $this->iterable_to_array( $keys );
157-
$results = wp_cache_delete_multiple( $keys_array, self::CACHE_GROUP );
179+
$results = wp_cache_delete_multiple( $keys_array, $this->get_cache_group() );
158180

159181
// Return true only if all operations succeeded.
160182
return ! in_array( false, $results, true );
@@ -170,7 +192,7 @@ public function deleteMultiple( $keys ): bool {
170192
*/
171193
public function has( $key ): bool {
172194
$found = false;
173-
wp_cache_get( $key, self::CACHE_GROUP, false, $found );
195+
wp_cache_get( $key, $this->get_cache_group(), false, $found );
174196

175197
return (bool) $found;
176198
}

tests/phpunit/tests/ai-client/wpAiClientCache.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,44 @@ public function test_ttl_with_date_interval() {
218218
$this->assertTrue( $this->cache->set( 'key1', 'value1', $ttl ) );
219219
$this->assertSame( 'value1', $this->cache->get( 'key1' ) );
220220
}
221+
222+
/**
223+
* Test that the cache group filter is respected.
224+
*
225+
* @ticket 65127
226+
*/
227+
public function test_cache_group_filter_is_respected() {
228+
add_filter(
229+
'wp_ai_client_cache_group',
230+
function ( $group ) {
231+
return 'wp_ai_client_tests_group';
232+
}
233+
);
234+
235+
$set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 );
236+
$this->assertTrue( $set );
237+
238+
$value = wp_cache_get( 'ai_test_key', 'wp_ai_client_tests_group' );
239+
$this->assertSame( 'ai_value', $value );
240+
}
241+
242+
/**
243+
* Test that a non-string cache group filter value is cast to string.
244+
*
245+
* @ticket 65127
246+
*/
247+
public function test_cache_group_filter_returns_non_string() {
248+
add_filter(
249+
'wp_ai_client_cache_group',
250+
function ( $group ) {
251+
return 12345;
252+
}
253+
);
254+
255+
$set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 );
256+
$this->assertTrue( $set );
257+
258+
$value = wp_cache_get( 'ai_test_key', '12345' );
259+
$this->assertSame( 'ai_value', $value );
260+
}
221261
}

0 commit comments

Comments
 (0)