/**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     if ($item instanceof CacheItemInterface) {
         // get item's tags
         $tags = $item->getTags();
         // record to a $tagItem (one tag one tagItem)
         if (count($tags)) {
             $key = $item->getKey();
             foreach ($tags as $tag) {
                 $tagKey = $this->getTagKey($tag);
                 $tagItem = $cache->getItem($tagKey);
                 if ($tagItem->isHit()) {
                     $keyArray = $tagItem->get();
                     $keyArray[$key] = true;
                 } else {
                     $keyArray = [$key => true];
                 }
                 $tagItem->set($keyArray);
                 $tagItem->expiresAfter($this->tag_life);
                 // one year
                 $cache->save($tagItem);
             }
         }
     }
     // always true
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     if ($item instanceof CacheItemInterface && $item->isHit()) {
         // time left
         $left = $item->getExpiration()->getTimestamp() - time();
         if ($left < $this->time_left && rand(1, $this->divisor) <= $this->probability) {
             // log message
             $cache->log('notice', Message::get(Message::CACHE_STAMPEDE_EXT, $item->getKey()));
             // revert to miss
             return $item->setHit(false);
         }
     }
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     // distribution
     $dis = $this->distribution;
     if ($item instanceof CacheItemInterface) {
         // expire ttl
         $ttl = $item->getExpiration()->getTimestamp() - time();
         // percentage
         $percent = (rand(0, $dis * 2) - $dis) * 0.001;
         // new expire ttl
         $new_ttl = (int) round($ttl + $ttl * $percent);
         $item->expiresAfter($new_ttl);
     }
     return true;
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     if ($item instanceof CacheItemInterface) {
         if ($stage === ExtensionStage::STAGE_POST_GET) {
             if ($item->isHit()) {
                 $fnc = $this->decrypt;
                 $res = $fnc($item->get());
             }
         } else {
             $fnc = $this->encrypt;
             $res = $fnc($item->get());
         }
         if (isset($res)) {
             // encrypt/decrypt failed
             if ($res === false) {
                 return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_ENCRYPT, $item->getKey()), Message::CACHE_FAIL_ENCRYPT);
             }
             // set to new string value
             $item->set($res);
         }
     }
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     if ($item instanceof CacheItemInterface) {
         if ($stage === ExtensionStage::STAGE_POST_GET) {
             if ($item->isHit()) {
                 $res = @unserialize($item->get());
             }
         } else {
             $res = @serialize($item->get());
         }
         if (isset($res)) {
             if ($res === false) {
                 return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_SERIALIZE, $item->getKey()), Message::CACHE_FAIL_SERIALIZE);
             }
             $item->set($res);
         }
     }
     return true;
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function save(CacheItemInterface $item)
 {
     $key = $item->getKey();
     $file = $this->getPath($key);
     // make sure directory exits
     $dir = dirname($file);
     if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
         return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_MKDIR, $dir), Message::CACHE_FAIL_MKDIR);
     }
     // write to file
     if (($tmp = tempnam($dir, 'temp_')) === false || file_put_contents($tmp, $item->get()) === false) {
         return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_WRITEFILE, $key), Message::CACHE_FAIL_WRITEFILE);
     }
     chmod($tmp, 0640);
     // rename to $file
     if (rename($tmp, $file) === false) {
         return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_WRITEFILE, $file), Message::CACHE_FAIL_WRITEFILE);
     }
     // set expire time
     touch($file, $item->getExpiration()->getTimestamp());
     return true;
 }