Skip to content

Improve check on loading cached file if the file not found #19773

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/caching/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function getValue($key)
{
$cacheFile = $this->getCacheFile($key);

if (@filemtime($cacheFile) > time()) {
if (file_exists($cacheFile) && @filemtime($cacheFile) > time()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It may result in additional stat call.
  2. It becomes non-atomic operation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, why do we want to avoid it at all costs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filemtime assumes that file already exists - which is already wrong assumption. Then it solves situation with non-existing file by suppressing error - which is also wrong approach. PHP has dedicated function(s) to check whether the file exists - so why not to use it properly? It is not adding extra stat call since stat data are memory cached on first attempt. The currently implemented mechanism is also not 100% atomic anyway since there is microtime between @filemtime and @fopen and @flock in the block (BTW would not file_get_contents() be better?). Should you wish I can add extra suppress operator @file_exists($path) even though I don't think it'd be necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lubosdz because there's a time gap between file_exists and filemtime. file_get_contents() with locking flags might be, indeed, better in terms of atomicity.

$fp = @fopen($cacheFile, 'r');
if ($fp !== false) {
@flock($fp, LOCK_SH);
Expand Down