From abf2490aaeebe790e461205cdcb034fe89cbf887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Thu, 25 Nov 2021 15:56:01 +0100 Subject: [PATCH] Avoid unnecessary home page cache cleaning --- src/Middleware/LSCachePurgeMiddleware.php | 44 ++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Middleware/LSCachePurgeMiddleware.php b/src/Middleware/LSCachePurgeMiddleware.php index 1eca49d..2aba039 100644 --- a/src/Middleware/LSCachePurgeMiddleware.php +++ b/src/Middleware/LSCachePurgeMiddleware.php @@ -1,8 +1,8 @@ getAttribute('routeName'); - $params = $request->getAttribute('routeParameters'); + + $isDiscussion = Str::startsWith($routeName, 'discussions'); + $body = $request->getParsedBody(); + + // If this is just an update of the last read post, there is no point in clearing the public cache + if ($isDiscussion && Arr::get($body, 'data.attributes.lastReadPostNumber')) { + return $response; + } $shouldReturnHeader = false; $purgeParams = []; @@ -44,34 +51,27 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface array_push($purgeParams, 'stale'); } - if (Str::endsWith($routeName, ['.create', '.update', '.delete'])) { - $rootRouteName = LSCache::extractRootRouteName($routeName); - array_push($purgeParams, "tag=$rootRouteName.index"); - $shouldReturnHeader = true; - - if (!empty($params) && !empty($params['id'])) { - array_push($purgeParams, "tag=$rootRouteName{$params['id']}"); - } - } + $params = $request->getAttribute('routeParameters'); - $isDiscussion = Str::startsWith($routeName, 'discussions'); $isPost = Str::startsWith($routeName, 'posts'); if ($isDiscussion || $isPost) { - array_push($purgeParams, 'tag=default', 'tag=index'); - $shouldReturnHeader = true; - $purgeList = $this->settings->get('acpl-lscache.purge_on_discussion_update'); if (!empty($purgeList)) { $purgeList = explode("\n", $purgeList); $purgeList = array_filter($purgeList, fn($item) => Str::startsWith($item, ['/', 'tag='])); $purgeParams = array_merge($purgeParams, $purgeList); + $shouldReturnHeader = true; + } + + // If this is a post update, we don't need to clear the home page cache + if ($routeName !== 'posts.update') { + array_push($purgeParams, 'tag=default', 'tag=index'); + $shouldReturnHeader = true; } } if ($isPost) { - $body = $request->getParsedBody(); - // When a new post is added $discussionId = Arr::get($body, 'data.relationships.discussion.data.id'); @@ -90,6 +90,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface } } + if (!$isDiscussion && Str::endsWith($routeName, ['.create', '.update', '.delete'])) { + $rootRouteName = LSCache::extractRootRouteName($routeName); + array_push($purgeParams, "tag=$rootRouteName.index"); + $shouldReturnHeader = true; + + if (!empty($params) && !empty($params['id'])) { + array_push($purgeParams, "tag=$rootRouteName{$params['id']}"); + } + } + if (!$shouldReturnHeader) { return $response; }