每当在Drupal 8中的生产环境(通过编码)中编辑内容类型时清除“渲染缓存”

时间:2018-04-05 07:33:41

标签: caching drupal-8

在Drupal 8中,当我编辑在另一个节点(页面)中使用的分类术语中使用的内容类型时,除非我手动清除“渲染缓存”,否则更改不会反映在页面上。 / p>

每当我通过编码修改节点时,我想实现清除缓存(全部/特定)。但问题是twig不接受PHP代码。 如果有人对此问题有任何建议(对某些代码更好),我会全力以赴!

2 个答案:

答案 0 :(得分:0)

Drupal默认缓存每一页。为了避免在开发过程中需要设置开发环境。您可以按照以下步骤操作:

  • 编辑您的settings.php文件。

      

    您的sites / default / settings.php文件底部的代码应如下所示:

    if (file_exists(__DIR__ . '/settings.local.php')) {
      include __DIR__ . '/settings.local.php';
    }
    
  • 在sites / default文件夹中创建settings.local.php文件

      

    将copy sites / example.settings.local.php复制到sites / default / settings.local.php

  • 修改调试配置

      

    查看sites / default / settings.local.php中的变量,具体来说:取消注释这些行。

    # disable the CSS and JavaScript aggregation features
    $config['system.performance']['css']['preprocess'] = FALSE;
    $config['system.performance']['js']['preprocess'] = FALSE;
    
    # disable the render cache
    $settings['cache']['bins']['render'] = 'cache.backend.null';
    
    # Disable Dynamic Page Cache.
    $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
    
      

    添加此行以在未登录时禁用缓存。

    $settings['cache']['bins']['page'] = 'cache.backend.null';
    
  • 启用Twig调试选项

      

    在sites文件夹中创建文件development.services.yml。   启用Twig调试将以下代码复制并粘贴到其中并保存。

    parameters:
      http.response.debug_cacheability_headers: true
        twig.config:
          debug: true
          auto_reload: true
          cache: false  
      services:
        cache.backend.null:
          class: Drupal\Core\Cache\NullBackendFactory
    

答案 1 :(得分:0)

要在编辑节点/分类法/块实体后清除特定类型的缓存,我们可以实现hook_form_node_form_alter()或hook_form_taxonomy_term_form_alter()或一般的hook_form_alter()。我们可以使用特定的form_id或其他方式清除内部缓存。 这是具有特定节点类型编辑形式的节点的示例:

function myModule_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit' && ($form_id == 'myNode_edit_form') {
      $form['actions'][$action]['#submit'][] = 'cache_form_submit_node';
    }
  }
}

function cache_form_submit_node($form, FormStateInterface $form_state) {
  drupal_flush_all_caches();  //To clear all cache or

  $renderCache = \Drupal::service('cache.render');
  $renderCache->invalidateAll(); 
}