Skip to content

[BUGFIX] Treat a cache entry without generated file as missing - #479

Merged
lochmueller merged 2 commits into
lochmueller:mainfrom
contemas-tschmidt:fix/valid-cache-entry-requires-file
Nov 16, 2026
Merged

lochmueller merged 2 commits into
lochmueller:mainfrom
contemas-tschmidt:fix/valid-cache-entry-requires-file

Conversation

@contemas-tschmidt

@contemas-tschmidt contemas-tschmidt commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Short description

GenerateMiddleware answers "already in cache" as soon as the database entry is valid, without checking that the generated file exists. If the file is gone and the entry is still there, nobody writes the page again until the entry expires, and every request is handled by PHP in the meantime.

How it happens

Anything that empties the cache directory without the database table, for example a deployment step such as find <cache dir> -mindepth 1 -delete after a cache flush. A page that is requested between the flush and the delete gets a new entry plus file, then loses the file and keeps the entry. The entry is valid for the full lifetime, so hasValidCacheEntry() keeps returning true and set() is never called again.

From the outside it looks like SFC is installed and active, but the page is rendered by PHP on each request.

What changed

hasValidCacheEntry() additionally requires that a generated file exists for the URI. Otherwise the page is stored again, which rewrites the entry and the files, so the situation heals itself on the next request.

The check lives in a new StaticFileBackend::hasStaticFile() (plain, .gz, .br or .php, depending on the enabled generators). It reuses getFilepath(), so hashed identifiers are handled. has() cannot be used for this: it is is_file() || parent::has() and therefore also true for an entry without file. The middleware asks the backend of its cache; any other backend keeps the old behaviour (the entry decides). The extra is_file() only runs on requests that reach PHP anyway.

Tests

  • backend: entry without file -> has() true, hasStaticFile() false; every generated file variant -> true; unknown / invalid identifier -> false
  • middleware: valid entry with file is not stored again, valid entry without file is stored again
  • the existing expired / explanation / cookie cases are unchanged

phpunit (401 tests), php-cs-fixer and phpstan are clean.

Related Issue

None found.

More Details

  • Work in Progress? No
  • Targets main. The same method exists in the TYPO3 13 line, I can open a second PR for v13 if you want it there.

🤖 Generated with Claude Code

GenerateMiddleware reported a page as "already in cache" as soon as the
database entry was valid, without looking at the file system. If the
generated files are gone while the entry is still there, the page is not
written again until the entry expires, and every request is handled by
PHP in the meantime.

That happens whenever the cache directory is emptied without the database
table, e.g. a deployment step like "find <cache dir> -delete" right after
a cache flush: a page requested between the flush and the delete keeps a
valid entry but loses its file.

The entry now only counts as valid if at least one generated file exists
(plain, .gz, .br or .php, depending on the enabled generators). Otherwise
the page is stored again, which rewrites entry and file.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@lochmueller

Copy link
Copy Markdown
Owner

Hey @contemas-tschmidt

thanks. do you check the StaticFileCache Backend? The "has" method in there, check the existence of the file and also the existence of the DB entry. https://github.com/lochmueller/staticfilecache/blob/main/Classes/Cache/StaticFileBackend.php#L140

So a more robust way would be, just call the "has" in front of the "get" for the cache, to also check the file. This will reuce the addional dependency of the IdentifierBuilder in the middleware.

What do you think?

Regards,
Tim

@lochmueller lochmueller left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please change the resolving of the files to the "has" method (comment above)

Review feedback: the middleware should not depend on the IdentifierBuilder.

StaticFileBackend::has() cannot be used for this, it is true when either
the file or the database entry exists. A new hasStaticFile() checks the
files only (plain, .gz, .br, .php) and reuses getFilepath(), so hashed
identifiers are handled as well. The middleware asks the backend of its
cache; other backends keep the old behaviour (the entry decides).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@contemas-tschmidt

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I checked has() first, but it is is_file(...) || parent::has(...), so it returns true when only the DB entry exists. With an entry and no file, has() and get() both still succeed (verified with a unit test on current main), and get() already calls has() internally, so calling it first doesn't change the outcome. Changing has() to AND would break remove(), which relies on it to clean up either half.

Your point about the extra dependency in the middleware is fair though, so I pushed a change: the file check now lives in StaticFileBackend::hasStaticFile() (reusing getFilepath(), so hashed identifiers are handled) and the middleware asks the backend of its cache. No IdentifierBuilder in the middleware anymore. Tests, php-cs-fixer and phpstan are green locally. Does that work for you?

@lochmueller

Copy link
Copy Markdown
Owner

Thanks... Merged.

@lochmueller
lochmueller merged commit 06fca80 into lochmueller:main Nov 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants