Where Do Wordpress Cache Functions Store Data?
Wp_Cache Functions Wp_Cache_Add( $Key, $Data, $Group, $Expire ) Wp_Cache_Set( $Key, $Data, $Group, $Expire ) I Want to Take Advantage of These Nice Functions...
wp_cache functions
wp_cache_add( $key, $data, $group, $expire )
wp_cache_set( $key, $data, $group, $expire )
I want to take advantage of these nice functions. I more or less understood the logic of working.
But where does WP store the answer to a previously cached query? Is it also keeping the database? Or is it saving to root as a temporary JSON file?
Here is a simple example.
$result = wp_cache_get( 'my_result' );
if ( false === $result ) {
$result = $wpdb->get_results( $query );
wp_cache_set( 'my_result', $result );
}
// Do something with $result;
1 Answer
From the WP_Object_Cache documentation you linked to in your question.
By default, the object cache is non-persistent. This means that data stored in the cache resides in memory only and only for the duration of the request. Cached data will not be stored persistently across page loads unless you install a persistent caching plugin.
This means by default it isn't persistently stored anywhere, any data cached using the wp_cache_* functions is stored in the $wp_object_cache global variable, so is only used for the duration on one request.
This can be useful if you have complicated queries used multiple times in a single page/request. Otherwise, as per the documentation, look into persistent caching or the Transients API.