wp_cache_add

Adds data to the cache, if the cache key doesn't already exist.

Description

wp_cache_add( (int|string) $key, (mixed) $data, (string) $group = '', (int) $expire = 0 ); 

Parameters (4)

0. $key (int|string)
The cache key to use for retrieval later.
1. $data (mixed)
The data to add to the cache.
2. $group — Optional. (string) => ''
The group to add the cache to. Enables the same key to be used across groups. Default empty.
3. $expire — Optional. (int)
When the cache data should expire, in seconds. Default 0 (no expiration).

Usage

  1. if ( !function_exists( 'wp_cache_add' ) ) { 
  2. require_once ABSPATH . WPINC . '/cache.php'; 
  3.  
  4. // The cache key to use for retrieval later. 
  5. $key = null; 
  6.  
  7. // The data to add to the cache. 
  8. $data = null; 
  9.  
  10. // Optional. The group to add the cache to. Enables the same key 
  11. // to be used across groups. Default empty. 
  12. $group = ''; 
  13.  
  14. // Optional. When the cache data should expire, in seconds. 
  15. // Default 0 (no expiration). 
  16. $expire = -1; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = wp_cache_add($key, $data, $group, $expire); 
  20.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/cache.php  
  1. function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { 
  2. global $wp_object_cache; 
  3.  
  4. return $wp_object_cache->add( $key, $data, $group, (int) $expire );