Skip to content

Commit

Permalink
Take expiry dates into account when setting the cache duration
Browse files Browse the repository at this point in the history
Resolves #187
  • Loading branch information
brandonkelly committed Mar 14, 2024
1 parent 86798bc commit 3fbcff8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Element API

## 4.1.0 - 2024-03-14

- Element API now requires Craft 4.3.0+ or 5.0.0+.
- Endpoints are no longer cached beyond the lowest expiry date in the results. ([#187](https://github.com/craftcms/element-api/issues/187))

## 4.0.0 - 2024-03-11

- Added Craft 5 compatibility. ([#186](https://github.com/craftcms/element-api/pull/186))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It’s powered by Phil Sturgeon’s excellent [Fractal](http://fractal.thephplea

## Requirements

This plugin requires Craft CMS 4.0.0+ or 5.0.0+.
This plugin requires Craft CMS 4.3.0+ or 5.0.0+.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"craftcms/cms": "^4.0.0-RC3|^5.0.0-beta.1",
"craftcms/cms": "^4.3.0|^5.0.0-beta.1",
"league/fractal": "^0.20.1"
},
"require-dev": {
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function actionIndex(string $pattern): Response
}

$elementsService = Craft::$app->getElements();
$elementsService->startCollectingCacheTags();
$elementsService->startCollectingCacheInfo();
}

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

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

if ($maxDuration) {
if ($expire !== null) {
$expire = min($expire, $maxDuration);
} else {
$expire = $maxDuration;
}
}

$cachedContent = $this->response->content;
if (isset($contentType)) {
$cachedContent = "data:$contentType,$cachedContent";
Expand Down

0 comments on commit 3fbcff8

Please sign in to comment.