Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 4 additions & 12 deletions .build/packages/cache_status/Classes/CacheHit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class CacheHit implements MiddlewareInterface
final readonly class CacheHit implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Expand All @@ -21,21 +20,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $response;
}

$tsfe = $request->getAttribute('frontend.controller', $GLOBALS['TSFE'] ?? null);
if (!$tsfe instanceof TypoScriptFrontendController) {
return $response;
}

return $response->withHeader(
'X-TYPO3-Cache',
$this->hitCache($tsfe) ? 'HIT' : 'MISS'
$this->hitCache($request) ? 'HIT' : 'MISS'
);
}

private function hitCache(TypoScriptFrontendController $tsfe): bool
private function hitCache(ServerRequestInterface $request): bool
{
return call_user_func(\Closure::bind(function() use ($tsfe) {
return $tsfe->pageContentWasLoadedFromCache ?? $tsfe->cacheContentFlag ?? false;
}, null, $tsfe));
return $request->getAttribute('frontend.page.parts')?->hasPageContentBeenLoadedFromCache() ?? false;
}
}
2 changes: 1 addition & 1 deletion .build/packages/cache_status/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"license": "GPL-2.0+",
"require": {
"typo3/cms-core": "^12.4.0 || ^13.0.0 || dev-main"
"typo3/cms-core": "^12.4 || ^13.0 || ^14.2 || dev-main"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 0 additions & 29 deletions .build/packages/cache_status/ext_emconf.php

This file was deleted.

26 changes: 0 additions & 26 deletions .build/v12/composer.json

This file was deleted.

1 change: 0 additions & 1 deletion .build/v13/config/system/additional.php

This file was deleted.

16 changes: 8 additions & 8 deletions .build/v13-dev/composer.json → .build/v14-dev/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"bnf/cache-status": "@dev",
"bnf/nginx-cache": "@dev",
"cweagans/composer-patches": "^1.7",
"typo3/cms-adminpanel": "13.4.x-dev as 13.4.0",
"typo3/cms-backend": "13.4.x-dev as 13.4.0",
"typo3/cms-core": "13.4.x-dev as 13.4.0",
"typo3/cms-extbase": "13.4.x-dev as 13.4.0",
"typo3/cms-filelist": "13.4.x-dev as 13.4.0",
"typo3/cms-fluid": "13.4.x-dev as 13.4.0",
"typo3/cms-frontend": "13.4.x-dev as 13.4.0",
"typo3/cms-install": "13.4.x-dev as 13.4.0"
"typo3/cms-adminpanel": "14.3.x-dev as 14.2.0",
"typo3/cms-backend": "14.3.x-dev as 14.2.0",
"typo3/cms-core": "14.3.x-dev as 14.2.0",
"typo3/cms-extbase": "14.3.x-dev as 14.2.0",
"typo3/cms-filelist": "14.3.x-dev as 14.2.0",
"typo3/cms-fluid": "14.3.x-dev as 14.2.0",
"typo3/cms-frontend": "14.3.x-dev as 14.2.0",
"typo3/cms-install": "14.3.x-dev as 14.2.0"
},
"config": {
"allow-plugins": {
Expand Down
6 changes: 3 additions & 3 deletions .build/v13/composer.json → .build/v14/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"require": {
"bnf/cache-status": "@dev",
"bnf/nginx-cache": "@dev",
"typo3/cms-adminpanel": "^13.1",
"typo3/cms-install": "^13.1",
"typo3/minimal": "^13.1"
"typo3/cms-adminpanel": "^14.2",
"typo3/cms-install": "^14.2",
"typo3/minimal": "^14.0"
},
"config": {
"allow-plugins": {
Expand Down
94 changes: 43 additions & 51 deletions Classes/Cache/Backend/NginxCacheBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,39 @@
use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
use TYPO3\CMS\Core\Cache\Exception;
use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class NginxCacheBackend extends Typo3DatabaseBackend implements TransientBackendInterface
final readonly class NginxCacheBackend implements TransientBackendInterface
{
/**
* Saves data in a cache file.
*
* @param string $entryIdentifier An identifier for this specific cache entry
* @param string $data The data to be stored
* @param array $tags Tags to associate with this cache entry
* @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
* @return void
* @throws Exception if no cache frontend has been set.
* @throws InvalidDataException if the data to be stored is not a string.
*/
public function set($entryIdentifier, $data, array $tags = array(), $lifetime = null)
{
parent::set($entryIdentifier, $data, $tags, $lifetime);
private Typo3DatabaseBackend $backend;

if ($lifetime === 0) {
// unlimited is not supported by nginx
$lifetime = 24 * 60 * 60;
}
public function __construct()
{
$this->backend = new Typo3DatabaseBackend();
}

/* Note: We use an explicit opt-in strategy to define requests as cachable.
* That means this functionality relies on the "fastcgi_cache_valid 0"
* in nginx.conf as documented in README.rst */
header('X-Accel-Expires: ' . $lifetime);
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void
{
$this->backend->set($entryIdentifier, $data, $tags, $lifetime);
}

/**
* Removes all cache entries matching the specified identifier.
*
* @param string $entryIdentifier Specifies the cache entry to remove
* @return bool TRUE if (at least) an entry could be removed or FALSE if no entry was found
*/
public function remove($entryIdentifier)
public function remove(string $entryIdentifier): bool
{
$url = parent::get($entryIdentifier);
$url = $this->backend->get($entryIdentifier);
if ($url === false) {
/* The key is not available. Do nothing. */
return false;
}

$this->purge($url);

return parent::remove($entryIdentifier);
return $this->backend->remove($entryIdentifier);
}

/**
* Removes all cache entries of this cache.
*
* @return void
*/
public function flush()
public function flush(): void
{
/* FIXME: this won't work for cli requests. We could try do derive the site_url from
* existing cache entries (using findIdentifierByTag?).
Expand All @@ -89,16 +65,10 @@ public function flush()
$url = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . '*';
$this->purge($url);

parent::flush();
$this->backend->flush();
}

/**
* Removes all cache entries of this cache which are tagged by the specified tag.
*
* @param string $tag The tag the entries must have
* @return void
*/
public function flushByTag($tag)
public function flushByTag(string $tag): void
{
$identifiers = $this->findIdentifiersByTag($tag);
foreach ($identifiers as $identifier) {
Expand All @@ -107,12 +77,9 @@ public function flushByTag($tag)
}

/**
* Removes all cache entries of this cache which are tagged by any of the specified tags.
*
* @param string[] $tags List of tags
* @return void
* @param list<string> $tasgs
*/
public function flushByTags(array $tags)
public function flushByTags(array $tags): void
{
array_walk($tags, [$this, 'flushByTag']);
}
Expand Down Expand Up @@ -150,4 +117,29 @@ protected function purge(string $url): string

return $content;
}

public function get(string $entryIdentifier): mixed
{
return $this->backend->get($entryIdentifier);
}

public function has(string $entryIdentifier): bool
{
return $this->backend->has($entryIdentifier);
}

public function collectGarbage(): void
{
$this->backend->collectGarbage();
}

public function setCache(FrontendInterface $cache): void
{
$this->backend->setCache($cache);
}

public function getTableDefinitions(): string
{
return $this->backend->getTableDefinitions();
}
}
Loading