-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNginxCacheBackend.php
More file actions
145 lines (122 loc) · 4.35 KB
/
NginxCacheBackend.php
File metadata and controls
145 lines (122 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Bnf\NginxCache\Cache\Backend;
/**
* nginx_cache – NGINX Cache Manager for TYPO3
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
use TYPO3\CMS\Core\Cache\Backend\TransientBackendInterface;
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;
final readonly class NginxCacheBackend implements TransientBackendInterface
{
private Typo3DatabaseBackend $backend;
public function __construct()
{
$this->backend = new Typo3DatabaseBackend();
}
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void
{
$this->backend->set($entryIdentifier, $data, $tags, $lifetime);
}
public function remove(string $entryIdentifier): bool
{
$url = $this->backend->get($entryIdentifier);
if ($url === false) {
/* The key is not available. Do nothing. */
return false;
}
$this->purge($url);
return $this->backend->remove($entryIdentifier);
}
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?).
* Or introduce a configure option to set the flushAll URL. */
if (Environment::isCli()) {
return;
}
$url = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . '*';
$this->purge($url);
$this->backend->flush();
}
public function flushByTag(string $tag): void
{
$identifiers = $this->findIdentifiersByTag($tag);
foreach ($identifiers as $identifier) {
$this->remove($identifier);
}
}
/**
* @param list<string> $tasgs
*/
public function flushByTags(array $tags): void
{
array_walk($tags, [$this, 'flushByTag']);
}
protected function purge(string $url): string
{
$content = '';
try {
$requestOptions = [
'timeout' => 5,
'connect_timeout' => 5,
];
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
$response = $requestFactory->request($url, 'PURGE', $requestOptions);
if ($response->getStatusCode() === 200) {
if ($response->getHeader('Content-Type') === 'text/plain') {
$content = $response->getBody()->getContents();
}
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
error_log("request for url '" . $url . "' failed with 40x.");
error_log($e->getMessage());
throw $e;
} catch (\GuzzleHttp\Exception\RequestException $e) {
error_log("request for url '" . $url . "' failed with 50x.");
error_log($e->getMessage());
throw $e;
} catch (\GuzzleHttp\Exception\ConnectException $e) {
error_log("request for url '" . $url . "' timed out after 5 seconds");
error_log($e->getMessage());
throw $e;
}
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();
}
}