Drupalのキャッシュについての資料
ここの図でキャッシュのイメージがなんとなく。。。
キャッシュのプロセスについて
Drupal8で追加されたに「Dynamic Page Cache」ついての説明
Render API
Contextキャッシュとは
urlやuserなど、文脈(直訳だと)に応じたキャッシュ(キー?)の事
Contexts are automatically replaced with the value for the current request (e.g. the current language) and combined with the keys to form a cache ID. The cache contexts, tags, and max-age will be propagated up the render array hierarchy to determine cacheability for containing render array sections.
参照:Core\Cache\Context\CacheContextInterface
参照:Core\Cache\Context\UserCacheContext
PHPStormを利用している場合、CacheContextInterfaceを開いて、Subtypes Hierarchyでどんなコンテキストがあるのか確認出来る
(Type Hierarchy → Subtypes Hierarchy.)
たとえば、user別にキャッシュするためのキーはUserCacheContextを確認すると
Cache context ID: 'user'.
となっている
その他、coreに定義されているキーの例
- site : サイト全体でのキャッシュ
- url.query_args : パラメータも含めたurl(ページャーモジュールはこれを使用している)のキャッシュ
- user.permission : ユーザの権限ごとのキャッシュ など
独自にコンテキストキャッシュのキーを作ることも可能?
TIPS
TIPS. 特定のブロックのキャッシュ設定を変更する(おそらくバッドノウハウ)
例:ラベルで判定して、キャッシュのコンテキストに'url'を追加する
function mymodule_block_build_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
{
/**
* 例ではブロックの"ラベル"で判定しているが、管理画面上から変更可能な部分なので危険
* core/modules/block/block.api.php
* https://api.drupal.org/comment/60564
*/
if ($block->getConfiguration()['label'] === 'xxxxxx') {
$build['#cache']['contexts'][] = 'url';
}
}