Skip to content

Commit

Permalink
feat: Add cache enabling/disabling setting
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed May 20, 2023
1 parent e7eb136 commit aea0e7f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
3 changes: 2 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@

// Settings
(new Extend\Settings())
->default('acpl-lscache.cache_enabled', true)
->default('acpl-lscache.public_cache_ttl', 604_800)
->default('acpl-lscache.clearing_cache_listener', true)
->default('acpl-lscache.drop_qs', implode("\n", LSCache::DEFAULT_DROP_QS)),
(new Extend\Event())->listen(Saved::class, Listener\UpdateHtaccess::class),
(new Extend\Event())->listen(Saved::class, Listener\UpdateSettings::class),

// Vary cookie
(new Extend\Middleware('forum'))->insertAfter(StartSession::class, VaryCookieMiddleware::class),
Expand Down
14 changes: 14 additions & 0 deletions js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import addPurgeLSCacheButton from './addPurgeLSCacheButton';
app.initializers.add('acpl-lscache', () => {
app.extensionData
.for('acpl-lscache')
.registerSetting({
setting: 'acpl-lscache.cache_enabled',
label: app.translator.trans('acpl-lscache.admin.cache_enabled_label'),
help: app.translator.trans('acpl-lscache.admin.cache_enabled_help', {
a: (
<Link
href="https://docs.litespeedtech.com/lscache/noplugin/installation/#verify-your-site-is-being-cached"
external={true}
target="_blank"
/>
),
}),
type: 'boolean',
})
.registerSetting({
setting: 'acpl-lscache.public_cache_ttl',
label: app.translator.trans('acpl-lscache.admin.public_cache_ttl_label'),
Expand Down
3 changes: 3 additions & 0 deletions locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
acpl-lscache:
admin:
cache_enabled_label: "Enable LSCache"
cache_enabled_help: "Check out the <a>Information page</a> on how to test the cache. NOTE: When disabling the cache, all cached entries for this site will be purged."

public_cache_ttl_label: "Default Public Cache TTL"
public_cache_ttl_help: "Define how long, in seconds, public pages should be cached. The default value is 604800 seconds (one week)."

Expand Down
27 changes: 0 additions & 27 deletions src/Listener/UpdateHtaccess.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Listener/UpdateSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ACPL\FlarumCache\Listener;

use ACPL\FlarumCache\Command\LSCacheClearCommand;
use ACPL\FlarumCache\Utility\HtaccessManager;
use Flarum\Settings\Event\Saved;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class UpdateSettings
{
protected HtaccessManager $htaccessManager;
protected LSCacheClearCommand $cacheClearCommand;

public function __construct(HtaccessManager $htaccessManager, LSCacheClearCommand $command)
{
$this->htaccessManager = $htaccessManager;
$this->cacheClearCommand = $command;
}

/**
* @throws FileNotFoundException|ExceptionInterface
*/
public function handle(Saved $event): void
{
if (isset($event->settings['acpl-lscache.drop_qs'])) {
$this->htaccessManager->updateHtaccess();
}

// If the LSCache is being disabled, initiate a cache clear operation.
if (isset($event->settings['acpl-lscache.cache_enabled']) && $event->settings['acpl-lscache.cache_enabled'] === false) {
$this->cacheClearCommand->run(new ArrayInput([]), new NullOutput());
}
}
}
14 changes: 11 additions & 3 deletions src/Middleware/LSCacheControlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$response = $handler->handle($request);
$method = $request->getMethod();

if ($this->settings->get('acpl-lscache.cache_enabled', true) === false) {
return $this->withCacheControlHeader($response, 'no-cache');
}

if (! in_array($method, ['GET', 'HEAD']) || $response->hasHeader(LSCacheHeadersEnum::CACHE_CONTROL)) {
return $response;
}
Expand Down Expand Up @@ -58,9 +62,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

//Cache CSRF privately
if ($routeName === 'lscache.csrf') {
$sessionTTL = $this->session['lifetime'] * 60;

return $this->withCacheControlHeader($response, "private,max-age=$sessionTTL");
// Subtract 2 minutes (120 seconds)
// from the session lifetime to set the cache to expire before the actual session does.
// This is to prevent a potential issue where an expired CSRF token might be served from the cache.
return $this->withCacheControlHeader(
$response,
'private,max-age='.(($this->session['lifetime'] * 60) - 120)
);
}

$lscacheParams = [];
Expand Down

0 comments on commit aea0e7f

Please sign in to comment.