-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Cache] Added PhpFilesAdapter #18832
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9279ea
[Cache] Added PhpFilesAdapter
ttrakos 16913f8
Added clearing opcache and workaround for hhvm, fixed some code style…
ttrakos cf07f56
Fixed code style issues and hhvm return value when deleting non-exist…
ttrakos 2e5e966
Added docblock for constructors.
ttrakos d9886bd
Fixed namespace support.
ttrakos 4554368
Made PhpFilesAdapter append-only.
trakos 846eb80
Code style fixes.
trakos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
137 changes: 137 additions & 0 deletions
137
src/Symfony/Component/Cache/Adapter/Helper/FilesCacheHelper.php
This file contains hidden or 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,137 @@ | ||
<?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\Helper; | ||
|
||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
class FilesCacheHelper | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $fileSuffix; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $directory; | ||
|
||
/** | ||
* @param string $directory Path where cache items should be stored, defaults to sys_get_temp_dir().'/symfony-cache' | ||
* @param string $namespace Cache namespace | ||
* @param string $version Version (works the same way as namespace) | ||
* @param string $fileSuffix Suffix that will be appended to all file names | ||
*/ | ||
public function __construct($directory = null, $namespace = null, $version = null, $fileSuffix = '') | ||
{ | ||
if (!isset($directory[0])) { | ||
$directory = sys_get_temp_dir().'/symfony-cache'; | ||
} | ||
if (isset($namespace[0])) { | ||
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) { | ||
throw new InvalidArgumentException(sprintf('Cache namespace for filesystem cache contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0])); | ||
} | ||
$directory .= '/'.$namespace; | ||
} | ||
if (isset($version[0])) { | ||
if (preg_match('#[^-+_.A-Za-z0-9]#', $version, $match)) { | ||
throw new InvalidArgumentException(sprintf('Cache version contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0])); | ||
} | ||
$directory .= '/'.$version; | ||
} | ||
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)) { | ||
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) + strlen($fileSuffix) > 234) { | ||
throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory)); | ||
} | ||
|
||
$this->fileSuffix = $fileSuffix; | ||
$this->directory = $dir; | ||
} | ||
|
||
/** | ||
* Returns root cache directory. | ||
* | ||
* @return string | ||
*/ | ||
public function getDirectory() | ||
{ | ||
return $this->directory; | ||
} | ||
|
||
/** | ||
* Saves entry in cache. | ||
* | ||
* @param string $id Id of the cache entry (used for obtaining file path to write to). | ||
* @param string $fileContent Content to write to cache file | ||
* @param int|null $modificationTime If this is not-null it will be passed to touch() | ||
* | ||
* @return bool | ||
*/ | ||
public function saveFileForId($id, $fileContent, $modificationTime = null) | ||
{ | ||
$file = $this->getFilePath($id, true); | ||
|
||
return $this->saveFile($file, $fileContent, $modificationTime); | ||
} | ||
|
||
/** | ||
* Saves entry in cache. | ||
* | ||
* @param string $file File path to cache entry. | ||
* @param string $fileContent Content to write to cache file | ||
* @param int|null $modificationTime If this is not-null it will be passed to touch() | ||
* | ||
* @return bool | ||
*/ | ||
public function saveFile($file, $fileContent, $modificationTime = null) | ||
{ | ||
$temporaryFile = $this->directory.uniqid('', true); | ||
if (false === @file_put_contents($temporaryFile, $fileContent)) { | ||
return false; | ||
} | ||
|
||
if (null !== $modificationTime) { | ||
@touch($temporaryFile, $modificationTime); | ||
} | ||
|
||
return @rename($temporaryFile, $file); | ||
} | ||
|
||
/** | ||
* Returns file path to cache entry. | ||
* | ||
* @param string $id Cache entry id. | ||
* @param bool $mkdir Whether to create necessary directories before returning file path. | ||
* | ||
* @return string | ||
*/ | ||
public function getFilePath($id, $mkdir = false) | ||
{ | ||
$hash = str_replace('/', '-', base64_encode(md5($id, true))); | ||
$dir = $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR; | ||
|
||
if ($mkdir && !file_exists($dir)) { | ||
@mkdir($dir, 0777, true); | ||
} | ||
|
||
return $dir.substr($hash, 2, -2).$this->fileSuffix; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why not an abstract like you did previously?
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 had to move some logic away (I couldn't just delete files for hhvm), and I just thought it's going to be more readable this way. Plus you could add tests to it this way if it will grow larger.
I can move it back to abstract if you prefer, I don't mind.