You are here

abstract class FileCache in Plug 7

Same name in this branch
  1. 7 lib/Drupal/Component/FileCache/FileCache.php \Drupal\Component\FileCache\FileCache
  2. 7 lib/doctrine/cache/lib/Doctrine/Common/Cache/FileCache.php \Doctrine\Common\Cache\FileCache

Base file cache driver.

@since 2.3 @author Fabio B. Silva <fabio.bat.silva@gmail.com>

Hierarchy

Expanded class hierarchy of FileCache

File

lib/doctrine/cache/lib/Doctrine/Common/Cache/FileCache.php, line 28

Namespace

Doctrine\Common\Cache
View source
abstract class FileCache extends CacheProvider {

  /**
   * The cache directory.
   *
   * @var string
   */
  protected $directory;

  /**
   * The cache file extension.
   *
   * @var string
   */
  private $extension;

  /**
   * @var string[] regular expressions for replacing disallowed characters in file name
   */
  private $disallowedCharacterPatterns = array(
    '/\\-/',
    // replaced to disambiguate original `-` and `-` derived from replacements
    '/[^a-zA-Z0-9\\-_\\[\\]]/',
  );

  /**
   * @var string[] replacements for disallowed file characters
   */
  private $replacementCharacters = array(
    '__',
    '-',
  );

  /**
   * Constructor.
   *
   * @param string $directory The cache directory.
   * @param string $extension The cache file extension.
   *
   * @throws \InvalidArgumentException
   */
  public function __construct($directory, $extension = '') {
    if (!is_dir($directory) && !@mkdir($directory, 0777, true)) {
      throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $directory));
    }
    if (!is_writable($directory)) {
      throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $directory));
    }
    $this->directory = realpath($directory);
    $this->extension = (string) $extension;
  }

  /**
   * Gets the cache directory.
   *
   * @return string
   */
  public function getDirectory() {
    return $this->directory;
  }

  /**
   * Gets the cache file extension.
   *
   * @return string|null
   */
  public function getExtension() {
    return $this->extension;
  }

  /**
   * @param string $id
   *
   * @return string
   */
  protected function getFilename($id) {
    return $this->directory . DIRECTORY_SEPARATOR . implode(str_split(hash('sha256', $id), 2), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . preg_replace($this->disallowedCharacterPatterns, $this->replacementCharacters, $id) . $this->extension;
  }

  /**
   * {@inheritdoc}
   */
  protected function doDelete($id) {
    return @unlink($this
      ->getFilename($id));
  }

  /**
   * {@inheritdoc}
   */
  protected function doFlush() {
    foreach ($this
      ->getIterator() as $name => $file) {
      @unlink($name);
    }
    return true;
  }

  /**
   * {@inheritdoc}
   */
  protected function doGetStats() {
    $usage = 0;
    foreach ($this
      ->getIterator() as $file) {
      $usage += $file
        ->getSize();
    }
    $free = disk_free_space($this->directory);
    return array(
      Cache::STATS_HITS => null,
      Cache::STATS_MISSES => null,
      Cache::STATS_UPTIME => null,
      Cache::STATS_MEMORY_USAGE => $usage,
      Cache::STATS_MEMORY_AVAILABLE => $free,
    );
  }

  /**
   * Create path if needed.
   *
   * @param string $path
   * @return bool TRUE on success or if path already exists, FALSE if path cannot be created.
   */
  private function createPathIfNeeded($path) {
    if (!is_dir($path)) {
      if (false === @mkdir($path, 0777, true) && !is_dir($path)) {
        return false;
      }
    }
    return true;
  }

  /**
   * Writes a string content to file in an atomic way.
   *
   * @param string $filename Path to the file where to write the data.
   * @param string $content  The content to write
   *
   * @return bool TRUE on success, FALSE if path cannot be created, if path is not writable or an any other error.
   */
  protected function writeFile($filename, $content) {
    $filepath = pathinfo($filename, PATHINFO_DIRNAME);
    if (!$this
      ->createPathIfNeeded($filepath)) {
      return false;
    }
    if (!is_writable($filepath)) {
      return false;
    }
    $tmpFile = tempnam($filepath, 'swap');
    if (file_put_contents($tmpFile, $content) !== false) {
      if (@rename($tmpFile, $filename)) {
        @chmod($filename, 0666 & ~umask());
        return true;
      }
      @unlink($tmpFile);
    }
    return false;
  }

  /**
   * @return \Iterator
   */
  private function getIterator() {
    return new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory)), '/^.+' . preg_quote($this->extension, '/') . '$/i');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Cache::STATS_HITS constant
Cache::STATS_MEMORY_AVAILABLE constant
Cache::STATS_MEMORY_AVAILIABLE constant Only for backward compatibility (may be removed in next major release)
Cache::STATS_MEMORY_USAGE constant
Cache::STATS_MISSES constant
Cache::STATS_UPTIME constant
CacheProvider::$namespace private property The namespace to prefix all cache ids with.
CacheProvider::$namespaceVersion private property The namespace version.
CacheProvider::contains public function Tests if an entry exists in the cache. Overrides Cache::contains
CacheProvider::delete public function Deletes a cache entry. Overrides Cache::delete
CacheProvider::deleteAll public function Deletes all cache entries. Overrides ClearableCache::deleteAll
CacheProvider::doContains abstract protected function Tests if an entry exists in the cache. 17
CacheProvider::DOCTRINE_NAMESPACE_CACHEKEY constant
CacheProvider::doFetch abstract protected function Fetches an entry from the cache. 17
CacheProvider::doFetchMultiple protected function Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. 4
CacheProvider::doSave abstract protected function Puts data into the cache. 17
CacheProvider::fetch public function Fetches an entry from the cache. Overrides Cache::fetch
CacheProvider::fetchMultiple public function Returns an associative array of values for keys is found in cache. Overrides MultiGetCache::fetchMultiple
CacheProvider::flushAll public function Flushes all cache entries. Overrides FlushableCache::flushAll
CacheProvider::getNamespace public function Retrieves the namespace that prefixes all cache ids.
CacheProvider::getNamespaceCacheKey private function Returns the namespace cache key.
CacheProvider::getNamespacedId private function Prefixes the passed id with the configured namespace value.
CacheProvider::getNamespaceVersion private function Returns the namespace version.
CacheProvider::getStats public function Retrieves cached information from the data store. Overrides Cache::getStats
CacheProvider::save public function Puts data into the cache. Overrides Cache::save
CacheProvider::setNamespace public function Sets the namespace to prefix all cache ids with. 1
FileCache::$directory protected property The cache directory.
FileCache::$disallowedCharacterPatterns private property
FileCache::$extension private property The cache file extension.
FileCache::$replacementCharacters private property
FileCache::createPathIfNeeded private function Create path if needed.
FileCache::doDelete protected function Deletes a cache entry. Overrides CacheProvider::doDelete
FileCache::doFlush protected function Flushes all cache entries. Overrides CacheProvider::doFlush
FileCache::doGetStats protected function Retrieves cached information from the data store. Overrides CacheProvider::doGetStats
FileCache::getDirectory public function Gets the cache directory.
FileCache::getExtension public function Gets the cache file extension.
FileCache::getFilename protected function
FileCache::getIterator private function
FileCache::writeFile protected function Writes a string content to file in an atomic way.
FileCache::__construct public function Constructor. 2