Skip to content

Commit 68b0099

Browse files
committed
Respect the Cache Lifetime from the Resource Type configuration and use "lifetime" in names
1 parent 9a2bc3c commit 68b0099

File tree

13 files changed

+227
-132
lines changed

13 files changed

+227
-132
lines changed

Classes/Cache/Cache.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class Cache implements CacheInterface
3030
*
3131
* @var integer
3232
*/
33-
private $cacheLifeTime = 0;
33+
private $cacheLifetime;
3434

3535
/**
3636
* Life time defined in the expires header
3737
*
3838
* @var integer
3939
*/
40-
private $expiresHeaderLifeTime = 0;
40+
private $expiresHeaderLifetime;
4141

4242
/**
4343
* @var ResponseFactory
@@ -56,13 +56,13 @@ public function __construct(ResponseFactoryInterface $responseFactory)
5656

5757
public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface
5858
{
59-
$cacheLifeTime = $this->getCacheLifeTime();
59+
$cacheLifetime = $this->getCacheLifetime();
6060

6161
/*
6262
* Use caching if the cache life time configuration is not -1, an API
6363
* path is given and the request is a read request
6464
*/
65-
$useCaching = ($cacheLifeTime !== -1) && $request->getPath();
65+
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
6666
if (!$useCaching) {
6767
return null;
6868
}
@@ -85,29 +85,26 @@ public function getCachedValueForRequest(RestRequestInterface $request): ?Respon
8585
return $response
8686
->withHeader(Header::CONTENT_TYPE, $responseData[Header::CONTENT_TYPE])
8787
->withHeader(Header::LAST_MODIFIED, $responseData[Header::LAST_MODIFIED])
88-
->withHeader(Header::EXPIRES, $this->getHttpDate(time() + $this->getExpiresHeaderLifeTime()))
88+
->withHeader(Header::EXPIRES, $this->getHttpDate(time() + $this->getExpiresHeaderLifetime()))
8989
->withHeader(Header::CUNDD_REST_CACHED, 'true');
9090
}
9191

9292
public function setCachedValueForRequest(
9393
RestRequestInterface $request,
9494
ResponseInterface $response,
9595
ResourceConfiguration $resourceConfiguration
96-
) {
96+
): void {
9797
if (false === $this->canBeCached($request, $response)) {
9898
return;
9999
}
100100

101-
$cacheLifeTime = $resourceConfiguration->getCacheLifetime();
102-
if ($cacheLifeTime < 0) {
103-
$cacheLifeTime = $this->getCacheLifeTime();
104-
}
101+
$cacheLifetime = $this->getCacheLifetime();
105102

106103
/*
107104
* Use caching if the cache life time configuration is not -1, an API
108105
* path is given and the request is a read request
109106
*/
110-
$useCaching = ($cacheLifeTime !== -1) && $request->getPath();
107+
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
111108
if (!$useCaching) {
112109
return;
113110
}
@@ -123,7 +120,7 @@ public function setCachedValueForRequest(
123120
Header::LAST_MODIFIED => $this->getHttpDate(time()),
124121
],
125122
$this->getTags($request),
126-
$cacheLifeTime
123+
$cacheLifetime
127124
);
128125
}
129126

@@ -147,12 +144,12 @@ public function getCacheKeyForRequest(RestRequestInterface $request): string
147144
/**
148145
* Sets the cache life time
149146
*
150-
* @param int $cacheLifeTime
147+
* @param int $cacheLifetime
151148
* @return $this
152149
*/
153-
public function setCacheLifeTime(int $cacheLifeTime): CacheInterface
150+
public function setCacheLifetime(int $cacheLifetime): CacheInterface
154151
{
155-
$this->cacheLifeTime = $cacheLifeTime;
152+
$this->cacheLifetime = $cacheLifetime;
156153

157154
return $this;
158155
}
@@ -162,20 +159,20 @@ public function setCacheLifeTime(int $cacheLifeTime): CacheInterface
162159
*
163160
* @return int
164161
*/
165-
public function getCacheLifeTime(): int
162+
public function getCacheLifetime(): int
166163
{
167-
return $this->cacheLifeTime;
164+
return $this->cacheLifetime;
168165
}
169166

170167
/**
171168
* Sets the life time defined in the expires header
172169
*
173-
* @param int $expiresHeaderLifeTime
170+
* @param int $expiresHeaderLifetime
174171
* @return $this
175172
*/
176-
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): CacheInterface
173+
public function setExpiresHeaderLifetime(int $expiresHeaderLifetime): CacheInterface
177174
{
178-
$this->expiresHeaderLifeTime = $expiresHeaderLifeTime;
175+
$this->expiresHeaderLifetime = $expiresHeaderLifetime;
179176

180177
return $this;
181178
}
@@ -185,9 +182,20 @@ public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): CacheInter
185182
*
186183
* @return int
187184
*/
188-
public function getExpiresHeaderLifeTime(): int
185+
public function getExpiresHeaderLifetime(): int
186+
{
187+
return $this->expiresHeaderLifetime;
188+
}
189+
190+
/**
191+
* Sets the concrete Cache instance
192+
*
193+
* @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $cacheInstance
194+
* @internal
195+
*/
196+
public function setCacheInstance($cacheInstance)
189197
{
190-
return $this->expiresHeaderLifeTime;
198+
$this->cacheInstance = $cacheInstance;
191199
}
192200

193201
/**
@@ -217,17 +225,6 @@ private function getCacheInstance()
217225
return $this->cacheInstance;
218226
}
219227

220-
/**
221-
* Sets the concrete Cache instance
222-
*
223-
* @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $cacheInstance
224-
* @internal
225-
*/
226-
public function setCacheInstance($cacheInstance)
227-
{
228-
$this->cacheInstance = $cacheInstance;
229-
}
230-
231228
/**
232229
* Clears the cache for the current request
233230
*

Classes/Cache/CacheFactory.php

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,45 @@
44
namespace Cundd\Rest\Cache;
55

66
use Cundd\Rest\Configuration\ConfigurationProviderInterface;
7+
use Cundd\Rest\Domain\Model\ResourceType;
78
use Cundd\Rest\ObjectManager;
89

910
class CacheFactory
1011
{
1112
/**
13+
* Return a Cache instance for the Resource Type
14+
*
15+
* @param ResourceType $resourceType
1216
* @param ConfigurationProviderInterface $configurationProvider
1317
* @param ObjectManager $objectManager
1418
* @return CacheInterface
1519
*/
1620
public function buildCache(
21+
ResourceType $resourceType,
1722
ConfigurationProviderInterface $configurationProvider,
1823
ObjectManager $objectManager
1924
): CacheInterface {
2025
$cacheInstance = $this->getCacheInstance($configurationProvider, $objectManager);
2126

22-
if ($cacheInstance instanceof CacheInterface) {
23-
$cacheInstance->setCacheLifeTime($this->getCacheLifeTime($configurationProvider));
24-
$cacheInstance->setExpiresHeaderLifeTime($this->getExpiresHeaderLifeTime($configurationProvider));
25-
}
27+
$cacheInstance->setCacheLifetime(
28+
$this->getCacheLifetime($configurationProvider, $resourceType)
29+
);
30+
$cacheInstance->setExpiresHeaderLifetime(
31+
$this->getExpiresHeaderLifetime($configurationProvider, $resourceType)
32+
);
2633

2734
return $cacheInstance;
2835
}
2936

3037
/**
3138
* @param ConfigurationProviderInterface $configurationProvider
3239
* @param ObjectManager $objectManager
33-
* @return mixed
40+
* @return CacheInterface
3441
*/
3542
private function getCacheInstance(
3643
ConfigurationProviderInterface $configurationProvider,
3744
ObjectManager $objectManager
38-
) {
45+
): CacheInterface {
3946
$cacheImplementation = $configurationProvider->getSetting('cacheClass');
4047
if ($cacheImplementation && $objectManager->isRegistered($cacheImplementation)) {
4148
return $objectManager->get($cacheImplementation);
@@ -46,28 +53,49 @@ private function getCacheInstance(
4653

4754
/**
4855
* @param ConfigurationProviderInterface $configurationProvider
49-
* @return int|mixed
56+
* @param ResourceType $resourceType
57+
* @return int
5058
*/
51-
private function getCacheLifeTime(ConfigurationProviderInterface $configurationProvider): int
59+
private function getCacheLifetime(ConfigurationProviderInterface $configurationProvider, ResourceType $resourceType)
5260
{
53-
$readCacheLifeTime = $configurationProvider->getSetting('cacheLifeTime');
54-
if ($readCacheLifeTime === null) {
55-
$readCacheLifeTime = -1;
61+
$resourceConfiguration = $configurationProvider->getResourceConfiguration($resourceType);
62+
$cacheLifetime = $resourceConfiguration->getCacheLifetime();
63+
if ($cacheLifetime > -1) {
64+
return $cacheLifetime;
65+
}
66+
67+
$cacheLifetime = $configurationProvider->getSetting('cacheLifeTime');
68+
if ($cacheLifetime !== null) {
69+
return (int)$cacheLifetime;
70+
}
71+
72+
$cacheLifetime = $configurationProvider->getSetting('cacheLifetime');
73+
if ($cacheLifetime !== null) {
74+
return (int)$cacheLifetime;
5675
}
5776

58-
return (int)$readCacheLifeTime;
77+
return -1;
5978
}
6079

6180
/**
6281
* @param ConfigurationProviderInterface $configurationProvider
63-
* @return int|mixed
82+
* @param ResourceType $resourceType
83+
* @return int
6484
*/
65-
private function getExpiresHeaderLifeTime(ConfigurationProviderInterface $configurationProvider): int
66-
{
67-
$expiresHeaderLifeTime = $configurationProvider->getSetting('expiresHeaderLifeTime');
85+
private function getExpiresHeaderLifetime(
86+
ConfigurationProviderInterface $configurationProvider,
87+
ResourceType $resourceType
88+
) {
89+
$expiresHeaderLifetime = $configurationProvider->getSetting('expiresHeaderLifetime');
90+
if ($expiresHeaderLifetime !== null) {
91+
return (int)$expiresHeaderLifetime;
92+
}
93+
94+
$expiresHeaderLifetime = $configurationProvider->getSetting('expiresHeaderLifeTime');
95+
if ($expiresHeaderLifetime !== null) {
96+
return (int)$expiresHeaderLifetime;
97+
}
6898

69-
return ($expiresHeaderLifeTime !== null)
70-
? intval($expiresHeaderLifeTime)
71-
: $this->getCacheLifeTime($configurationProvider);
99+
return $this->getCacheLifetime($configurationProvider, $resourceType);
72100
}
73101
}

Classes/Cache/CacheInterface.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
interface CacheInterface
1515
{
1616
/**
17-
* Returns the cached value for the given request or NULL if it is not defined
17+
* Return the cached value for the given request or NULL if it is not defined
1818
*
1919
* @param RestRequestInterface $request
2020
* @return ResponseInterface|null
2121
*/
2222
public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface;
2323

2424
/**
25-
* Sets the cache value for the given request
25+
* Set the cache value for the given request
2626
*
2727
* @param RestRequestInterface $request
2828
* @param ResponseInterface $response
@@ -33,43 +33,43 @@ public function setCachedValueForRequest(
3333
RestRequestInterface $request,
3434
ResponseInterface $response,
3535
ResourceConfiguration $resourceConfiguration
36-
);
36+
): void;
3737

3838
/**
39-
* Returns the cache key for the given request
39+
* Return the cache key for the given request
4040
*
4141
* @param RestRequestInterface $request
4242
* @return string
4343
*/
4444
public function getCacheKeyForRequest(RestRequestInterface $request): string;
4545

4646
/**
47-
* Sets the cache life time
47+
* Set the cache lifetime
4848
*
49-
* @param int $cacheLifeTime
49+
* @param int $cacheLifetime
5050
* @return $this
5151
*/
52-
public function setCacheLifeTime(int $cacheLifeTime): self;
52+
public function setCacheLifetime(int $cacheLifetime): self;
5353

5454
/**
55-
* Returns the cache life time
55+
* Return the cache lifetime
5656
*
5757
* @return int
5858
*/
59-
public function getCacheLifeTime(): int;
59+
public function getCacheLifetime();
6060

6161
/**
62-
* Sets the life time defined in the expires header
62+
* Set the lifetime defined in the expires header
6363
*
64-
* @param int $expiresHeaderLifeTime
64+
* @param int $expiresHeaderLifetime
6565
* @return $this
6666
*/
67-
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): self;
67+
public function setExpiresHeaderLifetime(int $expiresHeaderLifetime): self;
6868

6969
/**
70-
* Returns the life time defined in the expires header
70+
* Return the lifetime defined in the expires header
7171
*
7272
* @return int
7373
*/
74-
public function getExpiresHeaderLifeTime(): int;
74+
public function getExpiresHeaderLifetime();
7575
}

Classes/Configuration/AbstractConfigurationProvider.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ public function getConfiguredResources(): array
140140
}
141141

142142
$resourceType = new ResourceType($normalizeResourceType);
143+
$cacheLifetime = $this->detectCacheLifetimeConfiguration($configuration);
144+
143145
$configurationCollection[$normalizeResourceType] = new ResourceConfiguration(
144146
$resourceType,
145147
$readAccess,
146148
$writeAccess,
147-
isset($configuration['cacheLifeTime']) ? intval($configuration['cacheLifeTime']) : -1,
149+
$cacheLifetime,
148150
isset($configuration['handlerClass']) ? $configuration['handlerClass'] : '',
149151
$this->getAliasesForResourceType($resourceType)
150152
);
@@ -221,4 +223,20 @@ private function preparePath(array $configuration, $path)
221223

222224
return [$configuration, $normalizeResourceType];
223225
}
226+
227+
/**
228+
* @param array $configuration
229+
* @return int
230+
*/
231+
private function detectCacheLifetimeConfiguration(array $configuration): int
232+
{
233+
if (isset($configuration['cacheLifeTime']) && is_numeric($configuration['cacheLifeTime'])) {
234+
return (int)$configuration['cacheLifeTime'];
235+
}
236+
if (isset($configuration['cacheLifetime']) && is_numeric($configuration['cacheLifetime'])) {
237+
return (int)$configuration['cacheLifetime'];
238+
}
239+
240+
return -1;
241+
}
224242
}

Classes/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private function getCachedResponseOrCallHandler(
116116
/** @noinspection PhpUnusedParameterInspection */
117117
ResponseInterface $response
118118
) {
119-
$cache = $this->objectManager->getCache();
119+
$cache = $this->objectManager->getCache($request->getResourceType());
120120
$cachedResponse = $cache->getCachedValueForRequest($request);
121121

122122
// If a cached response exists return it

0 commit comments

Comments
 (0)