1. Go to this page and download the library: Download duckdev/wp-cache-helper 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/ */
duckdev / wp-cache-helper 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;
}
// Use this as a global variable or something.
$cache = new \DuckDev\Cache\Cache();
function do_something() {
return $cache->remember( 'some-cache-key', function () {
return a_whole_lotta_processing();
} );
}
function get_latest_posts() {
return $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 );
}