wp_cache_set

Saves the data to the cache.

Description

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

Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.

Parameters (4)

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

Usage

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

Defined (1)

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

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