Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active October 14, 2020 23:31
Show Gist options
  • Save jrobinsonc/832f287b69fa3bfdd6f475fa33122edf to your computer and use it in GitHub Desktop.
Save jrobinsonc/832f287b69fa3bfdd6f475fa33122edf to your computer and use it in GitHub Desktop.
Wordpress helper: cacheman
<?php
/**
* cacheman
*
* @author JoseRobinson.com
* @link https://gist.github.com/jrobinsonc/832f287b69fa3bfdd6f475fa33122edf
* @version 1.2.0
* @param string $key
* @param callable $callback
* @param int $expire - Optional
* @return mixed
*/
function cacheman($key, $callback = null, $expire = YEAR_IN_SECONDS)
{
$group = 'general';
if (false !== strpos($key, '.')) {
list($group, $key) = explode('.', $key);
}
$data = wp_cache_get( $key, $group );
if (false === $data && is_callable($callback)) {
$data = $callback();
wp_cache_set( $key, $data, $group, $expire );
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment