【问题标题】:Change cache settings programmatically in Drupal 7在 Drupal 7 中以编程方式更改缓存设置
【发布时间】:2012-08-10 09:50:08
【问题描述】:

我有一个显示来自外部站点的 RSS 提要列表的块。我想继续缓存除提到的块之外的其他块。怎么做?

例如,我有 blockAblockBblockC。我只想将 blockB 的缓存设置永久更改为 DRUPAL_NO_CACHE 并保留其他块,我想以编程方式进行。

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    您可以在创建您的块的特定模块中更改缓存角色。 在下面的块信息中:

    function pref_block_info() {
      return array(
        'pref_main' => array(
          'info' => t('Display flash game for auth. users'),
          'cache' => DRUPAL_NO_CACHE,
        ),
        'pref_winner' => array(
          'info' => t('Show the winner of the last week.'),
          'cache' => DRUPAL_NO_CACHE,
        ),
        'pref_leader' => array(
          'info' => t('Show the leader of the current week.'),
          'cache' => DRUPAL_NO_CACHE,
        ),
        'pref_top' => array(
          'info' => t('Show the top 10 of the current week.'),
          'cache' => DRUPAL_NO_CACHE,
        ),
      );
    }
    

    【讨论】:

      【解决方案2】:

      如果您在自己的模块中定义块,Jurgo 给出的答案是完全正确的。

      如果你想改变其他模块写入的块的缓存行为,那么你可以使用函数mymodule_block_list_alter

      function mymodule_block_list_alter(&$blocks, $theme, $code_blocks) {
        // Remove the caching on rss feeds block. 
        // Here rss-feeds is the unique key for the block
        $blocks['rss-feeds']['cache'] = DRUPAL_NO_CACHE;
      }
      

      【讨论】:

        【解决方案3】:

        积木从何而来?这很重要。正如 Jurgo 所说,如果它是自定义模块,您可以在 hook_block_info 中指定它。如果它们是视图块,则在处理此问题的视图中每个显示都有一个缓存设置。如果它们是其他模块提供的块,则需要直接查询数据库以更改块的缓存设置。

        作为一般说明,要显示 RSS 提要,只需使用提要和视图。然后,您根本不需要为此编写任何自定义代码。

        【讨论】:

          【解决方案4】:

          这将通过转到性能设置页面 (admin/settings/performance) 并通过向下滚动单击“清除缓存数据”来减少工作量。

          但是要确保这个页面只有管理员才能访问。

          对于 Drupal 7Drupal 6 相同:

          <?php
            drupal_flush_all_caches();
            drupal_set_message('cache flushed.');
          ?>
          

          【讨论】:

          • 我很抱歉让我无法理解
          猜你喜欢
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-10
          • 2015-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多