@@ -29,6 +29,27 @@ 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+ * 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+
3253 /**
3354 * Fetches a value from the cache.
3455 *
@@ -40,7 +61,7 @@ class WP_AI_Client_Cache implements CacheInterface {
4061 */
4162 public function get ( $ key , $ default_value = null ) {
4263 $ 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 );
4465
4566 if ( ! $ found ) {
4667 return $ default_value ;
@@ -62,7 +83,7 @@ public function get( $key, $default_value = null ) {
6283 public function set ( $ key , $ value , $ ttl = null ): bool {
6384 $ expire = $ this ->ttl_to_seconds ( $ ttl );
6485
65- return wp_cache_set ( $ key , $ value , self :: CACHE_GROUP , $ expire );
86+ return wp_cache_set ( $ key , $ value , $ this -> get_cache_group () , $ expire );
6687 }
6788
6889 /**
@@ -74,7 +95,7 @@ public function set( $key, $value, $ttl = null ): bool {
7495 * @return bool True if the item was successfully removed. False if there was an error.
7596 */
7697 public function delete ( $ key ): bool {
77- return wp_cache_delete ( $ key , self :: CACHE_GROUP );
98+ return wp_cache_delete ( $ key , $ this -> get_cache_group () );
7899 }
79100
80101 /**
@@ -92,7 +113,7 @@ public function clear(): bool {
92113 return false ;
93114 }
94115
95- return wp_cache_flush_group ( self :: CACHE_GROUP );
116+ return wp_cache_flush_group ( $ this -> get_cache_group () );
96117 }
97118
98119 /**
@@ -111,7 +132,7 @@ public function getMultiple( $keys, $default_value = null ): array {
111132 * @var array<string> $keys_array
112133 */
113134 $ 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 () );
115136 $ result = array ();
116137
117138 foreach ( $ keys_array as $ key ) {
@@ -138,7 +159,7 @@ public function getMultiple( $keys, $default_value = null ): array {
138159 public function setMultiple ( $ values , $ ttl = null ): bool {
139160 $ values_array = $ this ->iterable_to_array ( $ values );
140161 $ 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 );
142163
143164 // Return true only if all operations succeeded.
144165 return ! in_array ( false , $ results , true );
@@ -154,7 +175,7 @@ public function setMultiple( $values, $ttl = null ): bool {
154175 */
155176 public function deleteMultiple ( $ keys ): bool {
156177 $ 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 () );
158179
159180 // Return true only if all operations succeeded.
160181 return ! in_array ( false , $ results , true );
@@ -170,7 +191,7 @@ public function deleteMultiple( $keys ): bool {
170191 */
171192 public function has ( $ key ): bool {
172193 $ found = false ;
173- wp_cache_get ( $ key , self :: CACHE_GROUP , false , $ found );
194+ wp_cache_get ( $ key , $ this -> get_cache_group () , false , $ found );
174195
175196 return (bool ) $ found ;
176197 }
0 commit comments