Example #1
0
 /**
  * Begins FragmentCache widget.
  * Usage:
  * if (Cache::getInstance()->beginCache('key', $this)) {
  *      $this->endCache();
  * }
  * @param string $key the key identifying the content to be cached.
  * @param \yii\base\View $view view object
  * @param integer $duration number of seconds that the data can remain valid in cache.
  * Use 0 to indicate that the cached data will never expire.
  * @return boolean
  */
 public function beginCache($key, $view, $duration = 60)
 {
     $properties['id'] = $this->_cachePrefix . $key;
     $properties['view'] = $view;
     $properties['duration'] = $duration;
     $cache = FragmentCache::begin($properties);
     if ($cache->getCachedContent() !== false) {
         $this->endCache();
         return false;
     }
     return true;
 }
Example #2
0
 /**
  * Begins fragment caching.
  * This method will display cached content if it is available.
  * If not, it will start caching and would expect an [[endCache()]]
  * call to end the cache and save the content into cache.
  * A typical usage of fragment caching is as follows,
  *
  * ~~~
  * if ($this->beginCache($id)) {
  *     // ...generate content here
  *     $this->endCache();
  * }
  * ~~~
  *
  * @param string $id a unique ID identifying the fragment to be cached.
  * @param array $properties initial property values for [[FragmentCache]]
  * @return boolean whether you should generate the content for caching.
  * False if the cached version is available.
  */
 public function beginCache($id, $properties = [])
 {
     $properties['id'] = $id;
     $properties['view'] = $this;
     /* @var $cache FragmentCache */
     $cache = FragmentCache::begin($properties);
     if ($cache->getCachedContent() !== false) {
         $this->endCache();
         return false;
     } else {
         return true;
     }
 }