Jarves\Cache\Cacher::setDistributedCache PHP Метод

setDistributedCache() публичный Метод

This stores a ms timestamp on the distributed cache (Jarves::setCache()) and the actual data on the high-speed cache driver (Jarves::setFastCache()). This mechanism makes sure, you gain the maximum performance by using the fast cache driver to store the actual data and using the distributed cache driver to store a ms timestamp where we can check (over several jarves.cms installations) whether the cache is still valid or not. Use Jarves::invalidateCache($key) to invalidate this cache. You don't have to define the full key, instead you can pass only a part of the key.
См. также: invalidateCache for more information. Don't mix the usage of getDistributedCache() and getCache() since this method stores extra values at the value, which makes getCache() returning something invalid.
public setDistributedCache ( string $key, mixed $value, integer $lifeTime = null ) : boolean
$key string
$value mixed Only simple data types. Serialize your value if you have objects/arrays.
$lifeTime integer
Результат boolean
    public function setDistributedCache($key, $value, $lifeTime = null)
    {
        $timestamp = microtime(true);
        $cache['data'] = $value;
        $cache['timestamp'] = $timestamp;
        $this->distributedCache->deleteInvalidate($key);
        return $this->fastCache->set($key, $cache, $lifeTime);
    }

Usage Example

Пример #1
0
 /**
  * Returns a rendered view. If we find html behind the given cache
  * it returns this directly. This is a couple os ms faster than `renderCached`
  * since the template engine is never used when there's a valid cache.
  *
  * 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.
  *
  * @param string $cacheKey
  * @param string $view
  * @param array|callable $data Pass the data as array or a data provider function.
  * @param integer $lifeTime In seconds. Default is one hour/3600 seconds.
  * @param bool $force Force to bypass the cache and always call $data. For debuggin purposes.
  *
  * @see method `render` to get more information.
  *
  * @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']);
 }
All Usage Examples Of Jarves\Cache\Cacher::setDistributedCache