PHP code example of stevegrunwell / wp-cache-remember
1. Go to this page and download the library: Download stevegrunwell/wp-cache-remember library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stevegrunwell / wp-cache-remember example snippets
function do_something() {
$cache_key = 'some-cache-key';
$cached = wp_cache_get( $cache_key );
// Return the cached value.
if ( $cached ) {
return $cached;
}
// Do all the work to calculate the value.
$value = a_whole_lotta_processing();
// Cache the value.
wp_cache_set( $cache_key, $value );
return $value;
}
function do_something() {
return wp_cache_remember( 'some-cache-key', function () {
return a_whole_lotta_processing();
} );
}
function get_latest_posts() {
return wp_cache_remember( 'latest_posts', function () {
return new WP_Query( array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'desc',
) );
}, 'my-cache-group', HOUR_IN_SECONDS );
}