Skip to content

Commit 91504d9

Browse files
Add setCacheDuration() method, with this we can modify the cache duration value at runtime (#7)
1 parent 0c7a6e1 commit 91504d9

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/Concerns/ManipulateHttpResponse.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ protected function acknowledgeEsiSupport(Response $response)
3636
* the response as a cacheable content.
3737
*
3838
* @param \Symfony\Component\HttpFoundation\Response $response
39-
* @param int $cacheDuration
4039
*/
41-
protected function addCacheableHeader(Response $response, $cacheDuration)
40+
protected function addCacheableHeader(Response $response)
4241
{
43-
$duration = $this->getCacheDuration((int) $cacheDuration);
42+
$duration = $this->getCacheDuration();
4443

4544
$response->headers->set($this->getConfig('cacheable_header'), '1');
4645
$response->headers->set('Cache-Control', 'public, max-age='.$duration);
@@ -60,43 +59,49 @@ public function addUncacheableHeader(Response $response)
6059
}
6160

6261
/**
63-
* Normalize the given cache duration and convert
62+
* Normalize the cache duration value and convert
6463
* it to seconds.
6564
*
66-
* @param int $duration
67-
*
6865
* @return int|float
6966
*/
70-
protected function getCacheDuration($duration)
67+
protected function getCacheDuration()
7168
{
72-
$cacheInMinutes = ($duration > 0) ? $duration : $this->getConfig('cache_duration');
73-
74-
return $cacheInMinutes * 60;
69+
return $this->getConfig('cache_duration') * 60;
7570
}
7671

7772
/**
7873
* Manipulate the current Http response.
7974
*
8075
* @param \Symfony\Component\HttpFoundation\Response $response
81-
* @param int $cacheDuration
8276
*
8377
* @return \Symfony\Component\HttpFoundation\Response
8478
*/
85-
public function manipulate(Response $response, $cacheDuration)
79+
public function manipulate(Response $response)
8680
{
8781
$this->acknowledgeEsiSupport($response);
8882

8983
if ($this->shouldNotCache($response)) {
9084
return $response;
9185
}
9286

93-
$this->addCacheableHeader($response, $cacheDuration);
87+
$this->addCacheableHeader($response);
9488
$this->addLastModifiedHeader($response);
9589
$this->addEtagHeader($response);
9690

9791
return $response;
9892
}
9993

94+
/**
95+
* Set cache duration value in minutes. This value will
96+
* be added to the HTTP response's Cache-Control header.
97+
*
98+
* @param int $duration [Cache duration value in minutes]
99+
*/
100+
public function setCacheDuration($duration)
101+
{
102+
$this->setConfig('cache_duration', (int) $duration);
103+
}
104+
100105
/**
101106
* Set the current Http request headers.
102107
*
@@ -145,4 +150,14 @@ abstract protected function addLastModifiedHeader(Response $response);
145150
* @return mixed
146151
*/
147152
abstract public function getConfig($key);
153+
154+
/**
155+
* Set configuration value for a specific key.
156+
*
157+
* @param string $key
158+
* @param mixed $value
159+
*
160+
* @return void
161+
*/
162+
abstract public function setConfig($key, $value);
148163
}

src/Middleware/CacheableByVarnish.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public function handle(Request $request, Closure $next, $cacheDuration = null)
1919
{
2020
\Varnishable::setRequestHeaders($request->headers);
2121

22+
if ((int) $cacheDuration > 0) {
23+
\Varnishable::setCacheDuration($cacheDuration);
24+
}
25+
2226
$response = $next($request);
2327

24-
return \Varnishable::manipulate($response, $cacheDuration);
28+
return \Varnishable::manipulate($response);
2529
}
2630
}

tests/Concerns/ManipulateHttpResponseTests.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public function it_wont_acknowledge_esi_supports_when_there_was_no_esi_header_sp
7070
/** @test */
7171
public function it_can_add_cacheable_header_to_the_current_response_object()
7272
{
73-
$this->invokeMethod($this->service, 'addCacheableHeader', [$this->response, 60]);
73+
\Varnishable::setCacheDuration(60);
74+
$this->invokeMethod($this->service, 'addCacheableHeader', [$this->response]);
7475

7576
$cacheable = $this->response->headers->get(\Varnishable::getConfig('cacheable_header'));
7677
$cacheControl = $this->response->headers->get('Cache-Control');
@@ -96,7 +97,8 @@ public function it_can_calculate_total_cache_duration_in_seconds()
9697
$expected = [300, 900, 1800, 3600];
9798

9899
for ($i=0; $i<count($data); $i++) {
99-
$actual = $this->invokeMethod($this->service, 'getCacheDuration', [$data[$i]]);
100+
\Varnishable::setCacheDuration($data[$i]);
101+
$actual = $this->invokeMethod($this->service, 'getCacheDuration');
100102

101103
$this->assertEquals($expected[$i], $actual);
102104
}
@@ -105,12 +107,13 @@ public function it_can_calculate_total_cache_duration_in_seconds()
105107
/** @test */
106108
public function it_can_fully_manipulate_http_response_as_expected()
107109
{
110+
\Varnishable::setCacheDuration(120);
108111
$this->headers->set(\Varnishable::getConfig('esi_capability_header'), 'v1.0');
109112
$this->response->header(\Varnishable::getConfig('cacheable_header'), '1');
110113

111114
$this->service->setRequestHeaders($this->headers);
112115

113-
$this->service->manipulate($this->response, 120);
116+
$this->service->manipulate($this->response);
114117

115118
$actual = $this->response->headers->get(\Varnishable::getConfig('esi_reply_header'));
116119
$this->assertEquals('v1.0', $actual);

0 commit comments

Comments
 (0)