Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented filesystem cache, one and only (see #1415)
- Loading branch information
Showing
4 changed files
with
269 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) 2014 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Interface Twig_Cache_CacheInterface needs to be implemented by cache classes | ||
* | ||
* @author Andrew Tch <andrew@noop.lv> | ||
*/ | ||
interface Twig_Cache_CacheInterface | ||
{ | ||
/** | ||
* Returns cache key depending on current implementation. | ||
* | ||
* @param string $className Template class name | ||
* @param string $prefix Global template class prefix | ||
* @return string | ||
*/ | ||
public function getCacheKey($className, $prefix); | ||
|
||
/** | ||
* Clears all cached templates | ||
*/ | ||
public function clear(); | ||
|
||
/** | ||
* Checks if given cache entity exists. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function has($key); | ||
|
||
/** | ||
* Writes compiled template to cache. | ||
* | ||
* @param $key | ||
* @param $content | ||
*/ | ||
public function write($key, $content); | ||
|
||
/** | ||
* Loads cache file. This function returns nothing, it should require or eval() the compiled template. | ||
* | ||
* @param $key | ||
*/ | ||
public function load($key); | ||
|
||
/** | ||
* Returns timestamp of cached template.. | ||
* | ||
* @param $key | ||
* | ||
* @return int | ||
*/ | ||
public function getTimestamp($key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) 2014 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Class Twig_Cache_FilesystemCache - the one and only officially supported cache for Twig | ||
* | ||
* @author Andrew Tch <andrew@noop.lv> | ||
*/ | ||
class Twig_Cache_FilesystemCache implements Twig_Cache_CacheInterface | ||
{ | ||
/** | ||
* Root cache directory | ||
* | ||
* @var string | ||
*/ | ||
protected $directory; | ||
|
||
public function __construct($directory) | ||
{ | ||
$this->directory = $directory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheKey($className, $prefix) | ||
{ | ||
$class = substr($className, strlen($prefix)); | ||
|
||
return $this->directory.'/'.substr($class, 0, 2).'/'.substr($class, 2, 2).'/'.substr($class, 4).'.php'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::LEAVES_ONLY) as $file) { | ||
if ($file->isFile()) { | ||
@unlink($file->getPathname()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($key) | ||
{ | ||
return is_readable($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($key) | ||
{ | ||
require_once $key; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($key, $content) | ||
{ | ||
$dir = dirname($key); | ||
if (!is_dir($dir)) { | ||
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) { | ||
throw new RuntimeException(sprintf("Unable to create the cache directory (%s).", $dir)); | ||
} | ||
} elseif (!is_writable($dir)) { | ||
throw new RuntimeException(sprintf("Unable to write in the cache directory (%s).", $dir)); | ||
} | ||
|
||
$tmpFile = tempnam($dir, basename($key)); | ||
if (false !== @file_put_contents($tmpFile, $content)) { | ||
// rename does not work on Win32 before 5.2.6 | ||
if (@rename($tmpFile, $key) || (@copy($tmpFile, $key) && unlink($tmpFile))) { | ||
@chmod($key, 0666 & ~umask()); | ||
|
||
return; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTimestamp($key) | ||
{ | ||
return filemtime($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) 2014 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Class Twig_Cache_MemoryCache - use this to disable caching | ||
* | ||
* @author Andrew Tch <andrew@noop.lv> | ||
*/ | ||
class Twig_Cache_MemoryCache implements Twig_Cache_CacheInterface | ||
{ | ||
/** | ||
* Current cached files | ||
* @var array | ||
*/ | ||
protected $data = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheKey($className, $prefix) | ||
{ | ||
return $className; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
$this->data = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($key) | ||
{ | ||
return isset($this->data[$key]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($key, $content) | ||
{ | ||
$this->data[$key] = array( | ||
'data' => $content, | ||
'timestamp' => time() | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($key) | ||
{ | ||
eval('?>'.$this->data[$key]['data']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTimestamp($key) | ||
{ | ||
return $this->data[$key]['timestamp']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters