Skip to content

Commit

Permalink
Avoid unnecessary home page cache cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Nov 25, 2021
1 parent 16c7a64 commit abf2490
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/Middleware/LSCachePurgeMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace ACPL\FlarumCache\Middleware;

use ACPL\FlarumCache\LSCacheHeadersEnum;
use ACPL\FlarumCache\LSCache;
use ACPL\FlarumCache\LSCacheHeadersEnum;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -34,7 +34,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

$routeName = $request->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 = [];
Expand All @@ -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');

Expand All @@ -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;
}
Expand Down

0 comments on commit abf2490

Please sign in to comment.