jrobinsonc
8/26/2016 - 4:14 AM

Wordpress helper: cacheman

Wordpress helper: cacheman

<?php

/**
 * cacheman
 *
 * @author JoseRobinson.com
 * @link https://gist.github.com/jrobinsonc/832f287b69fa3bfdd6f475fa33122edf
 * @version 1.1.0
 * @param string $key
 * @param callable $callback
 * @param int $expire - Optional
 * @param bool $rewrite - Optional
 * @return mixed
 */
function cacheman($key, $callback = null, $expire = 0, $rewrite = false)
{
    if (false !== strpos($key, '|')) {
        list($group, $key) = explode('|', $key);
    } else {
        $group = 'general';
    }
    
    $data = wp_cache_get( $key, $group );

    if ((false === $data || true === $rewrite) && is_callable($callback)) {
        $data = $callback();

        wp_cache_set( $key, $data, $group, $expire );
    }

    return $data;
}