Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SandiyosDev/466aeb09250809e6346349f0b84cc579 to your computer and use it in GitHub Desktop.
Save SandiyosDev/466aeb09250809e6346349f0b84cc579 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' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment