/** * {@inheritdoc} */ protected function filterInvalidatedKeys(array &$keys) { $tags = $invalids = array(); foreach ($keys as $i => $key) { CacheItem::validateKey($key); foreach ($this->redis->hGetAll($this->namespace . $key . ':tags') as $tag => $version) { $tags[$this->namespace . 'tag:' . $tag][$version][$i] = $key; } } if ($tags) { $j = 0; $versions = $this->redis->mGet(array_keys($tags)); foreach ($tags as $tag => $version) { $version = $versions[$j++]; unset($tags[$tag][(int) $version]); foreach ($tags[$tag] as $version) { foreach ($version as $i => $key) { $invalids[] = $key; unset($keys[$i]); } } } } return $invalids; }
public function __construct($namespace = '', $defaultLifetime = 0, $version = null) { if (!static::isSupported()) { throw new CacheException('APCu is not enabled'); } if ('cli' === PHP_SAPI) { ini_set('apc.use_request_time', 0); } parent::__construct($namespace, $defaultLifetime); if (null !== $version) { CacheItem::validateKey($version); if (!apcu_exists($version . ':' . $namespace)) { $this->clear($namespace); apcu_add($version . ':' . $namespace, null); } } }
/** * {@inheritdoc} */ public function deleteItem($key) { CacheItem::validateKey($key); unset($this->values[$key], $this->expiries[$key]); return true; }
/** * Store an array of cached values. * * @param array $values The cached values */ public function warmUp(array $values) { if (file_exists($this->file)) { if (!is_file($this->file)) { throw new InvalidArgumentException(sprintf('Cache path exists and is not a file: %s.', $this->file)); } if (!is_writable($this->file)) { throw new InvalidArgumentException(sprintf('Cache file is not writable: %s.', $this->file)); } } else { $directory = dirname($this->file); if (!is_dir($directory) && !@mkdir($directory, 0777, true)) { throw new InvalidArgumentException(sprintf('Cache directory does not exist and cannot be created: %s.', $directory)); } if (!is_writable($directory)) { throw new InvalidArgumentException(sprintf('Cache directory is not writable: %s.', $directory)); } } $dump = <<<'EOF' <?php // This file has been auto-generated by the Symfony Cache Component. return array( EOF; foreach ($values as $key => $value) { CacheItem::validateKey(is_int($key) ? (string) $key : $key); if (null === $value || is_object($value)) { try { $value = serialize($value); } catch (\Exception $e) { throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, get_class($value)), 0, $e); } } elseif (is_array($value)) { try { $serialized = serialize($value); $unserialized = unserialize($serialized); } catch (\Exception $e) { throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable array value.', $key), 0, $e); } // Store arrays serialized if they contain any objects or references if ($unserialized !== $value || false !== strpos($serialized, ';R:') && preg_match('/;R:[1-9]/', $serialized)) { $value = $serialized; } } elseif (is_string($value)) { // Serialize strings if they could be confused with serialized objects or arrays if ('N;' === $value || isset($value[2]) && ':' === $value[1]) { $value = serialize($value); } } elseif (!is_scalar($value)) { throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value))); } $dump .= var_export($key, true) . ' => ' . var_export($value, true) . ",\n"; } $dump .= "\n);\n"; $dump = str_replace("' . \"\\0\" . '", "", $dump); $tmpFile = uniqid($this->file); file_put_contents($tmpFile, $dump); @chmod($tmpFile, 0666); unset($serialized, $unserialized, $value, $dump); @rename($tmpFile, $this->file); $this->values = (include $this->file) ?: array(); }
private function getId($key) { CacheItem::validateKey($key); return $this->namespace . $key; }
private function getId($key) { CacheItem::validateKey($key); if (null === $this->maxIdLength) { return $this->namespace . $key; } if (strlen($id = $this->namespace . $key) > $this->maxIdLength) { $id = $this->namespace . substr_replace(base64_encode(md5($key, true)), ':', -2); } return $id; }
/** * @dataProvider provideInvalidKey * @expectedException Symfony\Component\Cache\Exception\InvalidArgumentException * @expectedExceptionMessage Cache key */ public function testInvalidKey($key) { CacheItem::validateKey($key); }