wppaste
WordPress

WP_AI_Client_Cache

Since
7.0.0
Source
wp-includes/ai-client/adapters/class-wp-ai-client-cache.php:22
WordPress-specific PSR-16 cache adapter for the AI Client.

Description

Bridges PSR-16 cache operations to WordPress object cache functions, enabling the AI client to leverage WordPress caching infrastructure.

Compatibility

WordPress
since 7.0.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0.

Hooks and filters fired · 1

Every hook that fires from inside WP_AI_Client_Cache, in the order it appears in the class, grouped by the method that fires it.

Methods · 11

  • get_cache_group()Retrieves the cache group used for cache operations, applying a filter for customization.
  • get()Fetches a value from the cache.
  • set()Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
  • delete()Delete an item from the cache by its unique key.
  • clear()Wipes clean the entire cache's keys.
  • getMultiple()Obtains multiple cache items by their unique keys.
  • setMultiple()Persists a set of key => value pairs in the cache, with an optional TTL.
  • deleteMultiple()Deletes multiple cache items in a single operation.
  • has()Determines whether an item is present in the cache.
  • ttl_to_seconds()Converts a PSR-16 TTL value to seconds for WordPress cache functions.
  • iterable_to_array()Converts an iterable to an array.

Source code

class WP_AI_Client_Cache implements CacheInterface { 	/**	 * Cache group used for all cache operations.	 *	 * @since 7.0.0	 * @var string	 */	private const CACHE_GROUP = 'wp_ai_client'; 	/**	 * Retrieves the cache group used for cache operations, applying a filter for customization.	 *	 * @since 7.1.0	 *	 * @return string Cache group name.	 */	private function get_cache_group(): string {		/**		 * Filters the cache group used by the WP AI Client cache adapter.		 *		 * Allows integrators to change the object cache group under which AI client		 * items are stored. This is useful for avoiding key collisions, creating		 * environment-specific caches, or adapting to backend constraints.		 *		 * @since 7.1.0		 *		 * @param string $group The cache group.		 */		return (string) apply_filters( 'wp_ai_client_cache_group', self::CACHE_GROUP );	} 	/**	 * Fetches a value from the cache.	 *	 * @since 7.0.0	 *	 * @param string $key           The unique key of this item in the cache.	 * @param mixed  $default_value Default value to return if the key does not exist.	 * @return mixed The value of the item from the cache, or $default_value in case of cache miss.	 */	public function get( $key, $default_value = null ) {		$found = false;		$value = wp_cache_get( $key, $this->get_cache_group(), false, $found ); 		if ( ! $found ) {			return $default_value;		} 		return $value;	} 	/**	 * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.	 *	 * @since 7.0.0	 *	 * @param string                $key   The key of the item to store.	 * @param mixed                 $value The value of the item to store, must be serializable.	 * @param null|int|DateInterval $ttl   Optional. The TTL value of this item.	 * @return bool True on success and false on failure.	 */	public function set( $key, $value, $ttl = null ): bool {		$expire = $this->ttl_to_seconds( $ttl ); 		return wp_cache_set( $key, $value, $this->get_cache_group(), $expire );	} 	/**	 * Delete an item from the cache by its unique key.	 *	 * @since 7.0.0	 *	 * @param string $key The unique cache key of the item to delete.	 * @return bool True if the item was successfully removed. False if there was an error.	 */	public function delete( $key ): bool {		return wp_cache_delete( $key, $this->get_cache_group() );	}

Changelog

Introduced in 7.0.0. One change between 7.0.4 and 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

7.1.0
Method get_cache_group() added.verified against source
7.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.