Example #1
0
 /**
  *
  * @param CacheKey $k
  * @param integer $lifetime if null uses the class default
  * @param boolean $print
  * @param boolean $fail if true throws a NotCachedException if not cached.
  * @throws Cachearium\Exceptions\NotCachedException
  * @throws Cachearium\Exceptions\CacheKeyClashException
  * @return string The cached item as a string or false if not cached.
  */
 public function recursiveStart(CacheKey $k, $lifetime = null, $print = true, $fail = false)
 {
     // @codeCoverageIgnoreStart
     if (!$this->enabled) {
         return false;
     }
     // @codeCoverageIgnoreEnd
     foreach ($this->loopdata as $l) {
         /* @var $l CacheData */
         if ($l->checkClash($k)) {
             throw new Exceptions\CacheKeyClashException();
         }
     }
     // check if we are inside another cache for automatic dependencies.
     /* @var $cachedata CacheData */
     $cachedata = null;
     try {
         $cachedata = $this->getData($k);
         if (!$cachedata->checkUpdateToDate($this)) {
             // stale
             $cachedata = null;
         }
         // TODO $this->prefetch($cachedata->getDependencies());
     } catch (Exceptions\NotCachedException $e) {
     }
     // found. just return it.
     if ($cachedata) {
         try {
             $this->log(CacheLogEnum::ACCESSED, $cachedata->key, $cachedata->lifetime);
             $key = "cache-" . rand();
             $retval = $cachedata->stringify($this);
             if ($print) {
                 // @codeCoverageIgnoreStart
                 if (static::$debugOnPage) {
                     $this->printProbeStart($key, $cachedata, 'hit');
                 }
                 // @codeCoverageIgnoreEnd
                 echo $retval;
                 // @codeCoverageIgnoreStart
                 if (static::$debugOnPage) {
                     $this->printProbeEnd($key, $cachedata);
                 }
                 // @codeCoverageIgnoreEnd
             }
             return $retval;
         } catch (Exceptions\NotCachedException $e) {
             $this->delete($k);
             // clear recursively
             if ($this->inloop) {
                 throw $e;
             }
         }
     }
     if ($fail) {
         throw new Exceptions\NotCachedException();
     }
     $this->inloop++;
     $cd = new CacheData($k);
     $cd->setLifetime($lifetime ? $lifetime : $this->lifetime);
     $this->loopdata[$this->inloop] = $cd;
     if ($this->inloop > 1) {
         // we are recursive. push whatever we have so far in the previous cache
         $data = ob_get_contents();
         ob_clean();
         foreach ($this->loopdata as $l) {
             if ($l == $cd) {
                 // don't depend on itself
                 continue;
             }
             /* @var $l CacheData */
             $l->addDependency($k);
         }
         $this->loopdata[$this->inloop - 1]->appendData($data);
         $this->loopdata[$this->inloop - 1]->appendRecursionData($cd);
     } else {
         // something was not cached below. We invalidated all cache
         // dependencies
     }
     ob_start();
     ob_implicit_flush(false);
     return false;
 }