abstract class FileCache in Plug 7
Same name in this branch
- 7 lib/Drupal/Component/FileCache/FileCache.php \Drupal\Component\FileCache\FileCache
- 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
- class \Doctrine\Common\Cache\CacheProvider implements Cache, ClearableCache, FlushableCache, MultiGetCache
- class \Doctrine\Common\Cache\FileCache
Expanded class hierarchy of FileCache
File
- lib/
doctrine/ cache/ lib/ Doctrine/ Common/ Cache/ FileCache.php, line 28
Namespace
Doctrine\Common\CacheView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Cache:: |
constant | |||
Cache:: |
constant | |||
Cache:: |
constant | Only for backward compatibility (may be removed in next major release) | ||
Cache:: |
constant | |||
Cache:: |
constant | |||
Cache:: |
constant | |||
CacheProvider:: |
private | property | The namespace to prefix all cache ids with. | |
CacheProvider:: |
private | property | The namespace version. | |
CacheProvider:: |
public | function |
Tests if an entry exists in the cache. Overrides Cache:: |
|
CacheProvider:: |
public | function |
Deletes a cache entry. Overrides Cache:: |
|
CacheProvider:: |
public | function |
Deletes all cache entries. Overrides ClearableCache:: |
|
CacheProvider:: |
abstract protected | function | Tests if an entry exists in the cache. | 17 |
CacheProvider:: |
constant | |||
CacheProvider:: |
abstract protected | function | Fetches an entry from the cache. | 17 |
CacheProvider:: |
protected | function | Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. | 4 |
CacheProvider:: |
abstract protected | function | Puts data into the cache. | 17 |
CacheProvider:: |
public | function |
Fetches an entry from the cache. Overrides Cache:: |
|
CacheProvider:: |
public | function |
Returns an associative array of values for keys is found in cache. Overrides MultiGetCache:: |
|
CacheProvider:: |
public | function |
Flushes all cache entries. Overrides FlushableCache:: |
|
CacheProvider:: |
public | function | Retrieves the namespace that prefixes all cache ids. | |
CacheProvider:: |
private | function | Returns the namespace cache key. | |
CacheProvider:: |
private | function | Prefixes the passed id with the configured namespace value. | |
CacheProvider:: |
private | function | Returns the namespace version. | |
CacheProvider:: |
public | function |
Retrieves cached information from the data store. Overrides Cache:: |
|
CacheProvider:: |
public | function |
Puts data into the cache. Overrides Cache:: |
|
CacheProvider:: |
public | function | Sets the namespace to prefix all cache ids with. | 1 |
FileCache:: |
protected | property | The cache directory. | |
FileCache:: |
private | property | ||
FileCache:: |
private | property | The cache file extension. | |
FileCache:: |
private | property | ||
FileCache:: |
private | function | Create path if needed. | |
FileCache:: |
protected | function |
Deletes a cache entry. Overrides CacheProvider:: |
|
FileCache:: |
protected | function |
Flushes all cache entries. Overrides CacheProvider:: |
|
FileCache:: |
protected | function |
Retrieves cached information from the data store. Overrides CacheProvider:: |
|
FileCache:: |
public | function | Gets the cache directory. | |
FileCache:: |
public | function | Gets the cache file extension. | |
FileCache:: |
protected | function | ||
FileCache:: |
private | function | ||
FileCache:: |
protected | function | Writes a string content to file in an atomic way. | |
FileCache:: |
public | function | Constructor. | 2 |