Jarves\Cache\ResponseCacher::renderFullCached PHP Method

renderFullCached() public method

Example: return $this->renderFullCached('myCache', 'plugin1/default.tpl', function(){ return array('items' => heavyDbQuery()); }); Note: The $data callable is only called if the cache needs to regenerate (when it has been invalidated or empty, or the view file changed). If the callable $data returns NULL, then this will return NULL, too, without entering the actual rendering process. You should use this method in your plugins instead of writing your own cache mechanism, because this method handles PageResponse merging. Means: If templates used in this $view are changing somehow the PageResponse ({{loadAsset('style.css')}} calls) then this information (diff to current PageResponse) is stored and restored when we found a html cache. The diff is beside the actual rendered HTML also stored in the cache to keep this possible.
See also: method `render` to get more information.
public renderFullCached ( string $cacheKey, string $view, array | callable $data = null, integer $lifeTime = null, boolean $force = false ) : string
$cacheKey string
$view string
$data array | callable Pass the data as array or a data provider function.
$lifeTime integer In seconds. Default is one hour/3600 seconds.
$force boolean Force to bypass the cache and always call $data. For debuggin purposes.
return string
    public function renderFullCached($cacheKey, $view, $data = null, $lifeTime = null, $force = false)
    {
        $cache = $this->cacher->getDistributedCache($cacheKey);
        $mTime = $this->getViewMTime($view);
        if (!is_string($cache)) {
            $cache = null;
        } else {
            $cache = @unserialize($cache);
        }
        if ($force || !$cache || !$cache['content'] || !is_array($cache) || $mTime != $cache['fileMTime']) {
            $oldResponse = clone $this->pageStack->getPageResponse();
            $data2 = $data;
            if (is_callable($data)) {
                $data2 = call_user_func($data, $view);
                if (null === $data2) {
                    //the data callback returned NULL so this means
                    //we aren't the correct controller for this request
                    //or the request contains invalid input
                    return null;
                }
            }
            $content = $this->templating->render($view, $data2);
            $response = $this->pageStack->getPageResponse();
            $diff = $oldResponse->diff($response);
            $cache = array('content' => $content, 'fileMTime' => $mTime, 'responseDiff' => $diff);
            $this->cacher->setDistributedCache($cacheKey, serialize($cache), $lifeTime ?: 3600);
        } else {
            if ($cache['responseDiff']) {
                $this->pageStack->getPageResponse()->patch($cache['responseDiff']);
            }
        }
        return $this->pageResponseFactory->createPluginResponse($cache['content']);
    }