cpsit/typo3-cache-bags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cpsit/typo3-cache-bags

Composer 安装命令:

composer require cpsit/typo3-cache-bags

包简介

TYPO3 CMS extension to build and register cache bags for enhanced cache control

README 文档

README

Extension icon

TYPO3 extension cache_bags

Coverage CGL Release License
Version Downloads Supported TYPO3 versions Extension stability

📦 Packagist | 🐥 TYPO3 extension repository | 💾 Repository | 🐛 Issue tracker

An extension for TYPO3 CMS to build and register cache bags for enhanced cache control. Cache bags are built during runtime on uncached contents and can be used to define cache metadata like cache tags. In addition, they are used to calculate expiration dates for specific cache entries. This allows are fine-grained cache control, depending on the contents and their explicit dependencies like specific database contents or short-living API requests.

🚀 Features

  • Interface for cache bags with different cache scopes (e.g. pages)
  • Cache bag registry to handle generated cache bags
  • Cache expiration calculator for various use cases (query builder, query result etc.)
  • Event listener to override page cache expiration, based on registered page cache bags
  • Compatible with TYPO3 13.4 LTS

🔥 Installation

Composer

composer require cpsit/typo3-cache-bags

TER

Alternatively, you can download the extension via the TYPO3 extension repository (TER).

⚡ Usage

Cache bags

A cache bag can be seen as some type of metadata collection for a specific cache scope, e.g. for the current page (Frontend-related cache). It is used to control the cache behavior within a given scope, for example by defining custom cache tags or an explicit expiration date.

At the moment the following cache bags are supported:

Cache bag Scope
Cache\Bag\PageCacheBag Enum\CacheScope::Pages

Tip

You can also add your own cache bags by implementing Cache\Bag\CacheBag.

Here is an example about how to generate a new PageCacheBag for a given page:

use CPSIT\Typo3CacheBags;

$pageId = 72;
$expirationDate = new DateTimeImmutable('tomorrow midnight');

$cacheBag = Typo3CacheBags\Cache\Bag\PageCacheBag::forPage($pageId, $expirationDate);

Cache bag registry

In order to actually use a generated cache bag, each bag must be registered in the global Cache\Bag\CacheBagRegistry. This registry is defined as singleton instance and stores all registered cache bags during runtime.

The CacheBagRegistry should be injected using dependency injection or, if DI is not possible, by using GeneralUtility::makeInstance():

use CPSIT\Typo3CacheBags;
use TYPO3\CMS\Core;

$cacheBag = Typo3CacheBags\Cache\Bag\PageCacheBag::forPage(72);
$cacheBagRegistry = Core\Utility\GeneralUtility::makeInstance(Typo3CacheBags\Cache\Bag\CacheBagRegistry::class);
$cacheBagRegistry->add($cacheBag);

Dispatched events

When adding a new CacheBag to the registry, a CacheBagRegisteredEvent is dispatched. It is used, for example, to apply cache tags to the current Frontend request (using the shipped PageCacheBagRegisteredEventListener).

Get closest expiration date

Based on all registered cache bags, the registry is able to calculate the closest expiration date (if any cache bag provides an expiration date) for a given cache scope:

use CPSIT\Typo3CacheBags;
use TYPO3\CMS\Core;

$cacheBagRegistry = Core\Utility\GeneralUtility::makeInstance(Typo3CacheBags\Cache\Bag\CacheBagRegistry::class);
$expirationDate = $cacheBagRegistry->getExpirationDate(Typo3CacheBags\Enum\CacheScope::Pages);

This is used, for example, to apply the expiration date to the current Frontend page cache (using the shipped PageCacheLifetimeEventListener for TYPO3 ≥ v12 and PageCacheTimeoutHook for TYPO3 v11).

Cache expiration calculator

As already mentioned, cache bags may also store the expiration date of a targeted cache entry. The extension ships with a Cache\CacheExpirationCalculator that can be used to calculate an expiration date. The calculation is based on various input methods. At the moment, the following methods are available:

  • Calculation based on an Extbase query or query result
  • Calculation based on a Query Builder instance
  • Calculation based on an initialized Relation Handler
use CPSIT\Typo3CacheBags;
use TYPO3\CMS\Core;

// Use DI instead, calculator is *NOT* publicly available in the service container!
$cacheExpirationCalculator = Core\Utility\GeneralUtility::makeInstance(Typo3CacheBags\Cache\Expiration\CacheExpirationCalculator::class);
$connectionPool = Core\Utility\GeneralUtility::makeInstance(Core\Database\ConnectionPool::class);

$queryBuilder = $connectionPool->getQueryBuilderForTable('pages');
$queryBuilder->select('*')
    ->from('pages')
    ->where(
        $queryBuilder->expr()->or(
            $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(72, Core\Database\Connection::PARAM_INT)),
            $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(72, Core\Database\Connection::PARAM_INT)),
        ),
    )
;

$expirationDate = $cacheExpirationCalculator->forQueryBuilder('pages', $queryBuilder);
$cacheBag = Typo3CacheBags\Cache\Bag\PageCacheBag::forPage(72, $expirationDate);

Full example

Typical use cases of cache bags are list and detail views of custom records in Frontend scope. Here is a full example about how to use the PageCacheBag in list and detail views of a custom table:

use CPSIT\Typo3CacheBags;
use Psr\Http\Message;
use TYPO3\CMS\Core;
use TYPO3\CMS\Extbase;

final class BlogController extends Extbase\Mvc\Controller\ActionController
{
    public function __construct(
        private readonly BlogRepository $blogRepository,
        private readonly Typo3CacheBags\Cache\Bag\CacheBagRegistry $cacheBagRegistry,
        private readonly Typo3CacheBags\Cache\Expiration\CacheExpirationCalculator $cacheExpirationCalculator,
    ) {}

    public function listAction(): Message\ResponseInterface
    {
        /** @var Extbase\Persistence\QueryResultInterface<Blog> $blogArticles */
        $blogArticles = $this->blogRepository->findAll();

        // Create cache bag with reference to the queried table
        // and apply the calculated expiration date of all queried blog articles
        $cacheBag = Typo3CacheBags\Cache\Bag\PageCacheBag::forTable(
            Blog::TABLE_NAME,
            $this->cacheExpirationCalculator->forQueryResult(Blog::TABLE_NAME, $blogArticles),
        );

        // Add cache bag to registry
        $this->cacheBagRegistry->add($cacheBag);

        $this->view->assign('articles', $blogArticles);

        return $this->htmlResponse();
    }

    public function detailAction(Blog $article): Message\ResponseInterface
    {
        // Create cache bag with reference to the current article
        // and apply the article's endtime as cache expiration date
        $cacheBag = Typo3CacheBags\Cache\Bag\PageCacheBag::forRecord(
            Blog::TABLE_NAME,
            $article->getUid(),
            $article->getEndtime(),
        );

        // Add cache bag to registry
        $this->cacheBagRegistry->add($cacheBag);

        $this->view->assign('article', $article);

        return $this->htmlResponse();
    }
}

🧑‍💻 Contributing

Please have a look at CONTRIBUTING.md.

💎 Credits

The extension icon ("container") is a modified version of the original actions-container icon from TYPO3 core which is originally licensed under MIT License.

⭐ License

This project is licensed under GNU General Public License 2.0 (or later).

cpsit/typo3-cache-bags 适用场景与选型建议

cpsit/typo3-cache-bags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.26k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 cpsit/typo3-cache-bags 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2024-06-26