定制 acpl/flarum-lscache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

acpl/flarum-lscache

Composer 安装命令:

composer require acpl/flarum-lscache

包简介

LSCache implementation for Flarum.

README 文档

README

Latest Stable Version Total Downloads GitHub Sponsors

A Flarum extension. Integrates LSCache with your forum.

Requires a LiteSpeed Web Server or OpenLiteSpeed.

Installation

Install with composer:

composer require acpl/flarum-lscache

Upon initial activation, the extension will add its configurations to the .htaccess file. It's recommended to back up your .htaccess file before installing the extension.

Updating

composer update acpl/flarum-lscache
php flarum migrate
php flarum cache:clear

Cache Management

This extension smartly manages the cache by purging it only where needed. For instance, when a new post is added in a discussion, the cache for that specific discussion, its tags, and the homepage are purged.

When you clear the Flarum cache, the LSCache is also cleared automatically unless you disable this feature in the settings.

You can clear the LSCache without clearing the Flarum cache via the admin panel. This option is available under the standard Flarum cache clearing option. There is also the php flarum lscache:clear command. The command supports the --path argument. For example, php flarum lscache:clear --path=/tags --path=/d/1-test. You can use this if you only want to purge specific paths instead of the entire cache.

For Developers

Important

These instructions are for Flarum 2.0. For Flarum 1.x documentation, please refer to: Flarum 1.x Guide

How the Extension Tags Paths

First, it's useful to understand how the extension adds LSCache tags to forum paths. The extension uses route names. For example, if you registered routes for a resource in your extension named examples:

(new Extend\Routes('api'))
    ->get('/examples', 'examples.index', ExamplesListController::class)
    ->post('/examples', 'examples.create', ExamplesCreateController::class)
    // and so on

The extension will automatically add the following tags to the response:

  • For paths with the .index suffix (e.g., examples.index):
    • A tag corresponding to the main resource name is added (e.g., examples)
  • For other paths:
    • A tag with the full path name is added (e.g., examples.overview)
  • If the request contains parameters:
    • For the id parameter: a tag {resource}_{id} is added (e.g., example_1)
    • For the slug parameter: a tag {resource}_{slug} is added (e.g., example_example-slug)

Tip

To see the cache tags added for a given request, check the value of the X-LiteSpeed-Tag header in the Network tab of DevTools.

Purging Cache

By default, the extension purges the cache for a resource with a given ID if it detects successful requests to paths with the suffixes .create, .update, .delete.

To disable this behavior and add your own event handling, add your resource to the $resourcesSupportedByEvent array:

// 💡 resource name should be in singular form
\Acpl\FlarumLSCache\Utility\LSCachePurger::$resourcesSupportedByEvent[] = 'example'

return [
    // ... your current extenders
];

Then you can create an event listener:

// 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;

/** @extends AbstractCachePurgeListener<ExampleUpdated> */
class ExampleUpdatedListener extends AbstractCachePurgeListener
{
    protected function addPurgeData(object $event): void  
    {
        $this->purger
          // Purge cache tag
          ->addPurgeTag('examples');
          // or purge multiple cache tags
          ->addPurgeTags([
              'examples',
              "examples_{$event->example->id}"
          ]);
          // Purge a single path
          ->addPurgePath('/examples');
          // or purge multiple paths
          ->addPurgePaths([
              '/examples',
              "/examples_{$event->example->id}",
          ]);
    }
}

Tip

It is recommended to purge cache tags instead of paths, as they also apply to different versions of the address, e.g., with query strings.

It's also possible to create an event subscriber if you want to group multiple listeners in one class:

// 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
}

Links

acpl/flarum-lscache 适用场景与选型建议

acpl/flarum-lscache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.63k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2021 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cache」 「flarum」 「litespeed」 「LSCache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 acpl/flarum-lscache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 acpl/flarum-lscache 我们能提供哪些服务?
定制开发 / 二次开发

基于 acpl/flarum-lscache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2021-11-16