Skip to content

Commit 3fbcff8

Browse files
committed
Take expiry dates into account when setting the cache duration
Resolves #187
1 parent 86798bc commit 3fbcff8

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes for Element API
22

3+
## 4.1.0 - 2024-03-14
4+
5+
- Element API now requires Craft 4.3.0+ or 5.0.0+.
6+
- Endpoints are no longer cached beyond the lowest expiry date in the results. ([#187](https://github.com/craftcms/element-api/issues/187))
7+
38
## 4.0.0 - 2024-03-11
49

510
- Added Craft 5 compatibility. ([#186](https://github.com/craftcms/element-api/pull/186))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It’s powered by Phil Sturgeon’s excellent [Fractal](http://fractal.thephplea
66

77
## Requirements
88

9-
This plugin requires Craft CMS 4.0.0+ or 5.0.0+.
9+
This plugin requires Craft CMS 4.3.0+ or 5.0.0+.
1010

1111
## Installation
1212

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"minimum-stability": "dev",
2727
"prefer-stable": true,
2828
"require": {
29-
"craftcms/cms": "^4.0.0-RC3|^5.0.0-beta.1",
29+
"craftcms/cms": "^4.3.0|^5.0.0-beta.1",
3030
"league/fractal": "^0.20.1"
3131
},
3232
"require-dev": {

composer.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controllers/DefaultController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function actionIndex(string $pattern): Response
123123
}
124124

125125
$elementsService = Craft::$app->getElements();
126-
$elementsService->startCollectingCacheTags();
126+
$elementsService->startCollectingCacheInfo();
127127
}
128128

129129
// Extract config settings that aren't meant for createResource()
@@ -233,9 +233,17 @@ public function actionIndex(string $pattern): Response
233233
}
234234

235235
/** @phpstan-ignore-next-line */
236-
$dep = $elementsService->stopCollectingCacheTags();
236+
[$dep, $maxDuration] = $elementsService->stopCollectingCacheInfo();
237237
$dep->tags[] = 'element-api';
238238

239+
if ($maxDuration) {
240+
if ($expire !== null) {
241+
$expire = min($expire, $maxDuration);
242+
} else {
243+
$expire = $maxDuration;
244+
}
245+
}
246+
239247
$cachedContent = $this->response->content;
240248
if (isset($contentType)) {
241249
$cachedContent = "data:$contentType,$cachedContent";

0 commit comments

Comments
 (0)