-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Add FilesystemAdapter #17721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Adapter; | ||
|
||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class FilesystemAdapter extends AbstractAdapter | ||
{ | ||
private $directory; | ||
|
||
public function __construct($directory, $defaultLifetime = null) | ||
{ | ||
parent::__construct('', $defaultLifetime); | ||
|
||
if (!file_exists($dir = $directory.'/.')) { | ||
@mkdir($directory, 0777, true); | ||
} | ||
if (false === $dir = realpath($dir)) { | ||
throw new InvalidArgumentException(sprintf('Cache directory does not exist (%s)', $directory)); | ||
} | ||
if (!is_writable($dir .= DIRECTORY_SEPARATOR)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. realpath can remove it (depends on OS) |
||
throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory)); | ||
} | ||
// On Windows the whole path is limited to 258 chars | ||
if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 190) { | ||
throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory)); | ||
} | ||
|
||
$this->directory = $dir; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doFetch(array $ids) | ||
{ | ||
$values = array(); | ||
$now = time(); | ||
|
||
foreach ($ids as $id) { | ||
$file = $this->getFile($id); | ||
if (!$h = @fopen($file, 'rb')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
continue; | ||
} | ||
flock($h, LOCK_SH); | ||
if ($now >= (int) $expiresAt = fgets($h)) { | ||
flock($h, LOCK_UN); | ||
fclose($h); | ||
if (isset($expiresAt[0])) { | ||
@unlink($file); | ||
} | ||
} else { | ||
$value = stream_get_contents($h); | ||
flock($h, LOCK_UN); | ||
fclose($h); | ||
$values[$id] = unserialize($value); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doHave($id) | ||
{ | ||
$file = $this->getFile($id); | ||
|
||
return file_exists($file) && (@filemtime($file) > time() || $this->doFetch(array($id))); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doClear($namespace) | ||
{ | ||
$ok = true; | ||
|
||
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) { | ||
$ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok; | ||
} | ||
|
||
return $ok; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doDelete(array $ids) | ||
{ | ||
$ok = true; | ||
|
||
foreach ($ids as $id) { | ||
$file = $this->getFile($id); | ||
$ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok; | ||
} | ||
|
||
return $ok; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doSave(array $values, $lifetime) | ||
{ | ||
$ok = true; | ||
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX; | ||
|
||
foreach ($values as $id => $value) { | ||
$file = $this->getFile($id); | ||
$dir = dirname($file); | ||
if (!file_exists($dir)) { | ||
@mkdir($dir, 0777, true); | ||
} | ||
$value = $expiresAt."\n".serialize($value); | ||
if (false !== @file_put_contents($file, $value, LOCK_EX)) { | ||
@touch($file, $expiresAt); | ||
} else { | ||
$ok = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of the |
||
} | ||
} | ||
|
||
return $ok; | ||
} | ||
|
||
private function getFile($id) | ||
{ | ||
$hash = hash('sha256', $id); | ||
|
||
return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.$hash; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests\Adapter; | ||
|
||
use Cache\IntegrationTests\CachePoolTest; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
|
||
/** | ||
* @group time-sensitive | ||
*/ | ||
class FilesystemAdapterTest extends CachePoolTest | ||
{ | ||
public function createCachePool() | ||
{ | ||
if (defined('HHVM_VERSION')) { | ||
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM'; | ||
} | ||
|
||
return new FilesystemAdapter(sys_get_temp_dir().DIRECTORY_SEPARATOR.'sf-cache'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move
(%s)
directly afterCache directory
(same with the other messages below).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer having the message in front to make it more readable. The path can be quite long and could push the interesting info out of the viewport or on the next line.