/**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     if (rand(1, $this->divisor) <= $this->probability) {
         // log message
         $cache->log('info', Message::get(Message::CACHE_GARBAGE_COLLECT, date("Y-m-d H:i:s")));
         // purge those staled
         $cache->getDriver()->purge($this->max_lifetime);
     }
     // always true
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     // 100/1000 (10%) chances to commit
     if (rand(1, $this->divisor) <= $this->probability) {
         // log message
         $cache->log('notice', Message::get(Message::CACHE_COMMIT_DEFERRED));
         // commit deferred
         $cache->getDriver()->commit();
     }
     // always return true
     return true;
 }
예제 #3
0
 /**
  * {@inheritDoc}
  */
 public function __invoke(CachePoolInterface $cache, $stage, CacheItemInterface $item = null)
 {
     /*
      * 1. $this->trigger = '', always bypass the cache
      * 2. if sees $this->trigger in $_REQUEST, bypass the cache
      * 3. not to setError if $this->message == ''
      */
     if ($this->trigger === '' || isset($_REQUEST[$this->trigger]) && $_REQUEST[$this->trigger]) {
         return $this->message ? $this->falseAndSetError(Message::get(Message::CACHE_BYPASS_EXT), Message::CACHE_BYPASS_EXT) : false;
         // always return true if no trigger found
     } else {
         return true;
     }
 }
예제 #4
0
 /**
  * {@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;
 }
예제 #5
0
 /**
  * {@inheritDoc}
  */
 public function setDriver(DriverInterface $driver, $fallback = true)
 {
     // ping first
     if ($driver->ping()) {
         $this->driver = $driver;
         return;
     }
     // fallback
     if ($fallback) {
         // set to fallback driver
         $this->driver = $driver->getFallback();
         // issue warning
         trigger_error(Message::get(Message::CACHE_FALLBACK_DRIVER, get_class($driver), get_class($this->driver)), E_USER_WARNING);
     } else {
         throw new InvalidArgumentException(Message::get(Message::CACHE_FAIL_DRIVER, get_class($driver)), Message::CACHE_FAIL_DRIVER);
     }
 }
예제 #6
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;
 }
예제 #7
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;
 }
예제 #8
0
 /**
  * {@inheritDoc}
  */
 public function addExtension(ExtensionInterface $extension)
 {
     // extension not loaded yet
     if (!isset($this->loaded[get_class($extension)])) {
         // stages handling
         $handles = $extension->stagesHandling();
         foreach ($handles as $stage => $priority) {
             $this->extensions[$stage][$priority][] = $extension;
         }
         // register extension methods if any
         $methods = $extension->registerMethods();
         foreach ($methods as $func) {
             if (method_exists($extension, $func) && !isset($this->methods[$func])) {
                 $this->methods[$func] = [$extension, $func];
             } else {
                 throw new Exception\InvalidArgumentException(Message::get(Message::CACHE_INVALID_METHOD, get_class($extension), $func), Message::CACHE_INVALID_METHOD);
             }
         }
         // mark this extension loaded
         $this->loaded[get_class($extension)] = true;
         // loaded twice
     } else {
         throw new Exception\DuplicationFoundException(Message::get(Message::CACHE_INVALID_EXT, get_class($extension)), Message::CACHE_INVALID_EXT);
     }
 }
예제 #9
0
 /**
  * Validate key string
  *
  * @param  string &$key key to check
  * @return void
  * @throws Exception\InvalidArgumentException
  * @access protected
  */
 protected function validateKey(&$key)
 {
     // validate key
     if (is_string($key)) {
         $key = trim($key);
         return;
     }
     // throw exception
     throw new Exception\InvalidArgumentException(Message::get(Message::CACHE_INVALID_KEY, $key), Message::CACHE_INVALID_KEY);
 }
예제 #10
0
 /**
  * Remove all contents under one directory
  *
  * @param  string $dir directory
  * @param  int $maxlife delete those older than $maxlife seconds
  * @param  bool $removeDir remove the directory also
  * @return bool
  * @access protected
  */
 protected function deleteFromDir($dir, $maxlife = 0, $removeDir = false)
 {
     $now = time();
     if (is_dir($dir)) {
         $files = scandir($dir);
         foreach ($files as $file) {
             if ($file == "." || $file == "..") {
                 continue;
             }
             $sub = $dir . DIRECTORY_SEPARATOR . $file;
             $res = true;
             if (is_dir($sub)) {
                 $res = $this->deleteFromDir($sub, $maxlife, true);
             } else {
                 if (!$maxlife || $now - filemtime($sub) > $maxlife) {
                     $res = unlink($sub);
                 }
             }
             if ($res === false) {
                 return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_DELETE, $sub), Message::CACHE_FAIL_DELETE);
             }
         }
         if ($removeDir && !@rmdir($dir)) {
             return $this->falseAndSetError(Message::get(Message::CACHE_FAIL_DELETE, $dir), Message::CACHE_FAIL_DELETE);
         }
     }
     return true;
 }