Пример #1
0
 /**
  * {@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;
 }
Пример #2
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;
 }
Пример #3
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;
 }