Skip to content

Instantly share code, notes, and snippets.

@villesiltala
Last active June 12, 2017 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save villesiltala/f64b63070ed1dd082cb5ae0382974b8b to your computer and use it in GitHub Desktop.
Save villesiltala/f64b63070ed1dd082cb5ae0382974b8b to your computer and use it in GitHub Desktop.
WP Redis Group Cache usage example - see the comment section for description
---- page.php ---
<?php
/**
* This is a demo for using the WP Redis Group Cache.
*/
/**
* A demo DustPress model class.
*/
class Page extends \DustPress\Model {
/**
* The current page id.
*
* @var int
*/
protected $post_id;
/**
* The group key for our template type.
*
* @var string
*/
protected $group_key;
/**
* Get the main content of the page.
*
* @return array|bool|mixed|null|object
*/
public function Content() {
// Class functions are run in top-down order thus we can initialize the id here.
$this->post_id = get_the_ID();
// Lets define the cache keys.
// The group key is defined by the template class name which is 'Page'.
$this->group_key = get_called_class();
// Cache key for this function consists of the post id and the function name.
$cache_key = $this->post_id . '/' . __FUNCTION__;
// Maybe get from cache...
$content = wp_cache_get( $cache_key, $this->group_key );
if ( $content ) {
// Return the cached data for our view.
return $content;
}
$content = \DustPress\Query::get_acf_post( $this->post_id );
// Store the data for a week.
wp_cache_set( $cache_key, $content, $this->group_key, WEEK_IN_SECONDS );
// Return the fetched data for our view.
return $content;
}
/**
* Get the sidebar content for the page.
*/
public function Sidebar() {
// Same as above...
$cache_key = $this->post_id . '/' . __FUNCTION__;
$data = wp_cache_get( $cache_key, $this->group_key );
if ( $data ) {
return $data;
}
// Lets fetch 10 newest pages for a sidebar link list.
$pages = get_pages( [
'number' => 10,
'exclude' => $this->post_id,
'sort_column' => 'post_modified',
'sort_order' => 'desc',
] );
// Store the data for a week.
wp_cache_set( $cache_key, $pages, $this->group_key, WEEK_IN_SECONDS );
// Return the fetched data for our view.
return $pages;
}
}
?>
---- cache.php ---
<?php
/**
* Cache functions
* ---------------
* Currently we flush all function caches for all 'Page' models
* on 'save_post' hook when the saved post is a page.
*/
/**
* Clear cache by cache groups.
*
* @param array $groups Cache groups.
*/
function flush_cache( $groups ) {
if ( ! class_exists( '\Geniem\GroupCache' ) ) {
return;
}
foreach ( $groups as $group ) {
\Geniem\GroupCache::delete_group( $group );
}
}
/**
* Flushes group caches save_post hook.
*
* @param int $post_id Post ID.
*/
function flush_page_caches( $post_id = 0 ) {
// If the post is not a saved page, return.
if ( 0 === $post_id ||
get_post_status( $post_id ) === 'auto-draft' ||
get_post_type( $post_id ) !== 'page' ) {
return;
}
// Here we could define multiple groups.
$groups = [
'Page'
];
// Flush all defined groups.
flush_cache( $groups );
}
add_action( 'save_post', 'flush_page_caches' );
?>
@villesiltala
Copy link
Author

villesiltala commented Jun 2, 2017

This example contains two separate files in one file.

The first one is an example DustPress model. In this template model we fetch the main content of the current page and ten newest pages for the sidebar in two separate methods. Each method has its own caching both having the same cache group key.

The second file is a WordPress functions file which controls cache invalidating. In a function hooked on the save_post hook we flush all method caches for all Page model instances when a post of the page type is saved. Thus we can be sure the main content and the sidebar link list on each page contains the latest data if any of the pages is modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment