From 3fbcff86e8caf912c4f614e24555deef7d846384 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Thu, 14 Mar 2024 06:15:22 -0700 Subject: [PATCH] Take expiry dates into account when setting the cache duration Resolves #187 --- CHANGELOG.md | 5 +++++ README.md | 2 +- composer.json | 2 +- composer.lock | 22 +++++++++++----------- src/controllers/DefaultController.php | 12 ++++++++++-- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d93fa..a9393a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index 5381ef4..d34bce9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 5b240b6..27fa231 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index 5eab996..2f5c36e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ae356faea7dac544d8d151d9ce97044b", + "content-hash": "e4f4d832d9f6c9825f5231a79efab836", "packages": [ { "name": "cebe/markdown", @@ -766,16 +766,16 @@ }, { "name": "craftcms/cms", - "version": "4.8.1", + "version": "4.8.2", "source": { "type": "git", "url": "https://github.com/craftcms/cms.git", - "reference": "95a93e9f081f3d1a02c2fe0f967ed0c70c96e01d" + "reference": "3d3854612ea7419aabed4dfa45017b7b02201ddf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/craftcms/cms/zipball/95a93e9f081f3d1a02c2fe0f967ed0c70c96e01d", - "reference": "95a93e9f081f3d1a02c2fe0f967ed0c70c96e01d", + "url": "https://api.github.com/repos/craftcms/cms/zipball/3d3854612ea7419aabed4dfa45017b7b02201ddf", + "reference": "3d3854612ea7419aabed4dfa45017b7b02201ddf", "shasum": "" }, "require": { @@ -878,7 +878,7 @@ "rss": "https://github.com/craftcms/cms/releases.atom", "source": "https://github.com/craftcms/cms" }, - "time": "2024-03-05T20:11:03+00:00" + "time": "2024-03-12T23:00:16+00:00" }, { "name": "craftcms/plugin-installer", @@ -6904,16 +6904,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.60", + "version": "1.10.62", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe" + "reference": "cd5c8a1660ed3540b211407c77abf4af193a6af9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/95dcea7d6c628a3f2f56d091d8a0219485a86bbe", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/cd5c8a1660ed3540b211407c77abf4af193a6af9", + "reference": "cd5c8a1660ed3540b211407c77abf4af193a6af9", "shasum": "" }, "require": { @@ -6962,7 +6962,7 @@ "type": "tidelift" } ], - "time": "2024-03-07T13:30:19+00:00" + "time": "2024-03-13T12:27:20+00:00" }, { "name": "rector/rector", diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index 7dd6d23..ea8c937 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -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() @@ -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";