/** * {@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; }
/** * {@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; }