acpl/ flarum-lscache
Flarum的LSCache实现。
Requires
- php: >=8.1
- flarum/core: ^1.8
Requires (Dev)
- clarkwinkelmann/flarum-ext-author-change: ^1.0
- flarum/approval: ^1.8
- flarum/likes: ^1.8
- flarum/phpstan: ^1.8
- flarum/tags: ^1.8
- fof/masquerade: ^2.1
- sycho/flarum-move-posts: ^0.1.7
- v17development/flarum-blog: ^0.7.7
Suggests
- blomstra/flarum-redis: This library allows using Redis as cache, session and for the queue. https://github.com/blomstra/flarum-redis#set-up
README
Flarum扩展。将LSCache集成到您的论坛。
需要LiteSpeed Web Server或OpenLiteSpeed。
安装
使用composer安装
composer require acpl/flarum-lscache
在初次激活后,扩展会将配置添加到.htaccess
文件。建议在安装扩展之前备份您的.htaccess
文件。
更新
composer update acpl/flarum-lscache php flarum migrate php flarum cache:clear
缓存管理
此扩展通过仅清除所需的缓存来智能管理缓存。例如,在讨论中添加新帖子时,将清除该特定讨论、其标签和主页的缓存。
当您清除Flarum缓存时,LSCache也会自动清除,除非您在设置中禁用此功能。
您可以通过管理面板清除LSCache而不清除Flarum缓存。此选项在标准Flarum缓存清除选项下可用。还有一个php flarum lscache:clear
命令。该命令支持--path
参数。例如,php flarum lscache:clear --path=/tags --path=/d/1-test
。如果您只想清除特定路径而不是整个缓存,可以使用此命令。
针对开发者
扩展如何添加标签路径
首先,了解扩展如何将LSCache标签添加到论坛路径是有用的。扩展使用路由名称。例如,如果您在扩展中注册了名为examples
的资源路由
(new Extend\Routes('api')) ->get('/examples', 'examples.index', ExamplesListController::class) ->post('/examples', 'examples.create', ExamplesCreateController::class) // and so on
扩展将自动添加以下标签到响应中
- 对于带有
.index
后缀的路径(例如,examples.index
)- 添加对应于主要资源名称的标签(例如,
examples
)
- 添加对应于主要资源名称的标签(例如,
- 对于其他路径
- 添加带有完整路径名称的标签(例如,
examples.overview
)
- 添加带有完整路径名称的标签(例如,
- 如果请求包含参数
- 对于
id
参数:添加一个{resource}_{id}
标签(例如,example_1
) - 对于
slug
参数:添加一个{resource}_{slug}
标签(例如,example_example-slug
)
- 对于
提示
要查看为给定请求添加的缓存标签,请检查DevTools网络选项卡中的X-LiteSpeed-Tag
头值。
清除缓存
默认情况下,如果检测到对带有后缀.create
、.update
、.delete
的路径的成功请求,则扩展会清除具有给定ID的资源缓存。
要禁用此行为并添加自己的事件处理,请将您的资源添加到$resourcesSupportedByEvent
数组中
// 💡 resource name should be in singular form \ACPL\FlarumLSCache\Utility\LSCachePurger::$resourcesSupportedByEvent[] = 'example' return [ // ... your current extenders ];
然后您可以创建一个事件监听器
// extend.php use Flarum\Extend; return [ // ... your current extenders (new Extend\Conditional) ->whenExtensionEnabled('acpl-lscache', [ (new Extend\Event)->listen(ExampleUpdated::class, ExampleUpdatedListener::class) ]), ];
// ExampleUpdatedListener.php use ACPL\FlarumLSCache\Listener\AbstractCachePurgeListener; class PurgingCacheListener extends AbstractCachePurgeListener { /** @param ExampleUpdated $event */ protected function addPurgeData($event): void { // Purge cache tag $this->purger->addPurgeTag('examples'); // or purge multiple cache tags $this->purger->addPurgeTags([ 'examples', "examples_{$event->example->id}" ]); // Purge a single path $this->purger->addPurgePath('/examples'); // or purge multiple paths $this->purger->addPurgePaths([ '/examples', "/examples_{$event->example->id}", ]); } }
提示
建议清除缓存标签而不是路径,因为它们也适用于不同版本的地址,例如带有查询字符串的地址。
如果您想在一个类中分组多个监听器,也可以创建事件订阅者
// extend.php use Flarum\Extend; return [ // ... your current extenders (new Extend\Conditional) ->whenExtensionEnabled('acpl-lscache', [ (new Extend\Event)->subscribe(ExampleEventSubscriber::class), ]), ];
// ExampleEventSubscriber.php use ACPL\FlarumLSCache\Listener\AbstractCachePurgeSubscriber; use Illuminate\Contracts\Events\Dispatcher; class ExampleEventSubscriber extends AbstractCachePurgeSubscriber { public function subscribe(Dispatcher $events): void { $this->addPurgeListener($events, ExampleUpdated::class, [$this, 'handleExampleUpdated']); // ... rest of listeners } public function handleExampleUpdated(ExampleUpdated $event): void { $this->purger->addPurgeTags([ 'examples', "example_{$event->example->id}", ]); } // ... rest of methods }