labor-digital / typo3-distributed-cache-clearing
用于清除多容器设置中缓存的处理程序
Requires
- labor-digital/typo3-better-api: ^10.35.0
This package is auto-updated.
Last update: 2024-09-25 15:21:55 UTC
README
这是一个用于清除多容器设置中本地文件缓存的实用扩展。它设计为易于扩展,支持各种消息队列后端。默认情况下,包括 DB/TYPO3 注册表和 Redis 后端。
要求
- TYPO3 v10
- TYPO3 - 更好的 API
- 使用 Composer 安装
安装
使用 Composer 安装此包
composer require labor-digital/typo3-distributed-cache-clearing
使用方法
要利用分布式缓存清除,您需要使用 ext 配置逻辑注册一个后端类。这将激活消息处理,当 TYPO3 缓存在后端或 CLI 中清除时。
<?php namespace LaborDigital\T3baExample\Configuration\ExtConfig; use LaborDigital\T3ba\ExtConfig\ExtConfigContext; use LaborDigital\T3dcc\Core\Message\Backend\Registry\RegistryMessageBackend; use LaborDigital\T3dcc\ExtConfigHandler\ConfigureDistributedCacheClearingInterface; use LaborDigital\T3dcc\ExtConfigHandler\DccConfigurator; class Dcc implements ConfigureDistributedCacheClearingInterface { public static function configureDistributedCacheClearing(DccConfigurator $configurator, ExtConfigContext $context): void { // You can use any of the built-in backends or a class that implements the MessageBackendInterface here. // Additional options for the backend can be provided as second parameter $configurator->setMessageBackend(RegistryMessageBackend::class); } }
为了监听在其他容器中发出的缓存清除事件,您可以使用多种方法。最简单的方法是将“在每个请求中检查”选项设置为 true。这也在 ext 配置类中完成。这种选项的主要缺点是可能会降低您的性能,尤其是如果使用了外部事件队列(SNS、ServiceBus、...)。
<?php namespace LaborDigital\T3baExample\Configuration\ExtConfig; use LaborDigital\T3ba\ExtConfig\ExtConfigContext; use LaborDigital\T3dcc\Core\Message\Backend\Registry\RegistryMessageBackend; use LaborDigital\T3dcc\ExtConfigHandler\ConfigureDistributedCacheClearingInterface; use LaborDigital\T3dcc\ExtConfigHandler\DccConfigurator; class Dcc implements ConfigureDistributedCacheClearingInterface { public static function configureDistributedCacheClearing(DccConfigurator $configurator, ExtConfigContext $context): void { $configurator->setCheckInEveryRequest(true) } }
如果您的基础设施允许您独立地对每个容器执行 HTTP 请求,并且您使用 T3FA 扩展,您可以在您的站点路由配置中使用 DccBundle
。路由将在 /api/dcc/clearIfRequired
可用。
<?php namespace LaborDigital\T3baExample\Configuration\ExtConfig\Site\Common; use LaborDigital\T3dcc\Api\Bundle\DccBundle; use LaborDigital\T3fa\ExtConfigHandler\Api\BundleCollector; use LaborDigital\T3fa\ExtConfigHandler\Api\ConfigureApiInterface; class Api implements ConfigureApiInterface { public static function registerBundles(BundleCollector $collector): void { $collector->register(DccBundle::class); } /* ... */ }
或者,您可以使用内置的 CLI 命令 t3dcc:handleMessages
,它执行相同的操作。
对于高度专业的设置,您还可以使用扩展提供的 /ext/t3dcc/handleMessages.php
文件。它可以在任何其他 PHP 脚本中执行,甚至在没有 TYPO3 上下文的情况下。它包含封装的引导程序和小型应用程序,用于启动 TYPO3 核心和清除缓存(如有必要)。在包含文件之前,您需要将 T3DCC_AUTOLOAD_PATH
设置为环境变量或常量,其值为您的 composer autoload.php 的绝对路径。其余的应该会自动完成。
此脚本将位于您的安装的 public/typo3conf
目录中。
<?php define('T3DCC_AUTOLOAD_PATH', dirname(__DIR__, 2) . '/vendor/autoload.php'); require __DIR__ . '/ext/t3dcc/handleMessages.php';
后端
此扩展提供了两个简单的后端
RegistryMessageBackend
此后端利用 TYPO3 sys_registry
表在容器之间发送消息。这是最简单的实现,不需要额外的选项或配置。
RedisMessageBackend
此后端利用 Redis 数据库作为容器之间的消息队列。配置结构与 redis 缓存后端相同。
<?php namespace LaborDigital\T3baExample\Configuration\ExtConfig; use LaborDigital\T3ba\ExtConfig\ExtConfigContext; use LaborDigital\T3dcc\Core\Message\Backend\Redis\RedisMessageBackend; use LaborDigital\T3dcc\ExtConfigHandler\ConfigureDistributedCacheClearingInterface; use LaborDigital\T3dcc\ExtConfigHandler\DccConfigurator; class Dcc implements ConfigureDistributedCacheClearingInterface { public static function configureDistributedCacheClearing(DccConfigurator $configurator, ExtConfigContext $context): void { $configurator->setMessageBackend( RedisMessageBackend::class, [ // Required 'hostname' => 'host', // The following options are optional 'password' => '', // Only needed if the host requires a password 'database' => 1, // It is not required, but recommended to set a database id here (Default: 1), 'port' => 6379, 'ttl' => 60 * 15 ] ); } }
Postcardware
您可以自由使用此包,但如果它进入您的生产环境,我们非常感谢您从您的家乡给我们寄一张明信片,提及您正在使用我们哪些包。
我们的地址是:LABOR.digital - Fischtorplatz 21 - 55116 Mainz, Germany。
我们将发布我们收到的所有明信片在我们的 公司网站 上。