Last active
October 14, 2020 23:31
-
-
Save jrobinsonc/832f287b69fa3bfdd6f475fa33122edf to your computer and use it in GitHub Desktop.
Wordpress helper: cacheman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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