Drupal 8块缓存
时间:2020-03-05 15:25:33 来源:igfitidea点击:
在Drupal8和Drupal7中,定制块缓存的方法是不同的。
在Drupal8中,所有可渲染数组都是可缓存的,甚至那些由自定义块返回的数组也是可缓存的。
示例:
- 一些昂贵的计算数据取决于活动主题:不同主题的不同结果。然后你会因主题缓存上下文的不同而有所不同。
- 创建显示个性化消息的渲染数组时,渲染数组会因用户而异。然后根据用户缓存上下文改变(渲染数组)。
- 一般来说,当一些昂贵的计算信息因某些环境背景而变化时。
然后根据缓存上下文而变化。
drupal8中新的和改进了很多的cacheapi提供了一种复杂的方法来缓存所有可呈现项,无论是页面、实体还是块。
Drupal8允许开发人员直接在块对象的"build()"方法返回的渲染数组中管理块的缓存行为。
function mymodule_block_info() { $blocks = array(); $blocks['mymodule_example_block'] = array( 'info' => t('Block title'), //Block caching options (per role, per user, etc.) //DRUPAL_NO_CACHE is the default. 'cache' => DRUPAL_NO_CACHE, ); return $blocks; }
在Drupal 7中,按角色缓存块如下所示:
class MyCustomBlock extends BlockBase { public function build() { return array( '#markup' => $markup, '#cache' => array( 'contexts' => array( 'url.path', ), ), ); } }
在Drupal 8中,缓存设置直接在块的build()方法返回的可渲染数组中操作:
用于操纵缓存设置的可用参数包括"keys"、"contexts"、"tags"、"max age"和"bin"。
Drupal 8核心的缓存上下文:
cookies :name headers :name ip languages :type request_format route .book_navigation .menu_active_trails :menu_name .name session .exists theme timezone url .host .query_args :key .pagers :pager_id .path .site user .is_super_user .node_grants :operation .permissions .roles :role