generoi / sage-cachetags
Requires
- php: >=7.4
- roots/acorn: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-25 19:45:39 UTC
README
Sage 包,用于跟踪页面依赖的数据使用缓存标签(灵感来自Drupal 的缓存标签)。
示例
首页显示页面内容以及 3 个菜谱预览。缓存标签可能为
post:1
(首页)post:232
、post:233
、post:234
(3 个菜谱预览)term:123
、term:124
(菜谱预览中显示的菜谱分类)post:10
(3 个菜谱中突出显示的产品名称)。
这些标签将在渲染页面时收集,然后存储在数据库中,并可选择添加为 HTTP 标头。
当任何帖子或术语更新时,页面缓存和反向代理知道应清除首页缓存。
安装
composer require generoi/sage-cachetags
首先使用 Acorn 发布 config/cachetags.php 配置文件
wp acorn vendor:publish --provider="Genero\Sage\CacheTags\CacheTagsServiceProvider"
编辑它以符合您的需求,如果您使用数据库存储,则构建所需的数据库表
wp acorn cachetags:database
无效化器
目前它支持 Kinsta 页面缓存、WP Super Cache、SiteGround Optimizer 和 Fastly。如果您使用 Fastly 作为 Kinsta 的前端并希望同时无效化,则可以使用多个无效化器。
SiteGround Optimizer
如果将 SiteGroundCacheInvalidator
无效化器添加到 config/cachetags.php
文件中,则存在集成。
Super Cache
如果将 SuperCacheInvalidator
无效化器添加到 config/cachetags.php
文件中,则存在集成。
Kinsta
如果将 KinstaCacheInvalidator
添加到 config/cachetags.php
文件中,则存在集成。
Cloudflare
Cloudflare Pro 计划支持 HTTP 标头清除,但目前尚无无效化器。如果您愿意,可以查看 Fastly 的一个示例。
Fastly
Fastly(Varnish)代理缓存有 FastlySoftCacheInvalidator
和 FastlyCacheInvalidator
(硬)缓存无效化器。使用此设置,您不需要持久的 store
,因为 Fastly 与 HTTP 标头一起工作。示例 config/cachetags.php
$isProduction = in_array(parse_url(WP_HOME, PHP_URL_HOST), [ 'www.example.com', ]); return [ 'http-header' => 'Surrogate-Key', 'store' => CacheTagStore::class, 'invalidator' => array_filter([ $isProduction ? FastlySoftCacheInvalidator::class : null, ]), 'action' => [ Core::class, HttpHeader::class, ], ];
与 roots/sage 一起使用的特性
Composers
namespace App\View\Composers; use Genero\Sage\CacheTags\Concerns\ComposerCacheTags; use Genero\Sage\CacheTags\Tags\CoreTags; use Roots\Acorn\View\Composer; use Illuminate\View\View; class ContentSingle extends Composer { use ComposerCacheTags; protected static $views = [ 'partials.content-single', ]; /** * @return array */ public function with() { $post = get_post(); return [ 'post' => $post, 'date' => $this->date($post), 'authors' => $this->authors($post), 'excerpt' => $this->excerpt($post), 'related' => $this->related($post), 'categories' => $this->categories($post), ]; } public function cacheTags(View $view): array { return [ ...CoreTags::posts($view->post), ...CoreTags::terms($view->categories), ...CoreTags::query($this->related()) ]; } }
ACF Blocks
namespace App\Blocks; use Genero\Sage\CacheTags\Tags\CoreTags; use Genero\Sage\CacheTags\Concerns\BlockCacheTags; class ArticleList extends Block { use BlockCacheTags; public $name = 'Article List'; public $slug = 'article-list'; public function cacheTags(): array { $query = $this->buildQuery(); return [ ...CoreTags::archive('post'), ...CoreTags::query($query), ]; } }
CLI
# Flush the entire cache
wp acorn cachetags:flush
API
创建自定义标签
最简单的方法是查看此存储库的代码并创建一个自定义 Action
和可能使用的 CustomTag
类,但逻辑实际上并没有什么不同。
// Tag content app(CacheTags::class)->add(['custom-tag']); // Clear it whenever you want \add_action('custom/update', fn() => app(CacheTags::class)->clear(['custom-tag']));