如何强制drupal函数不使用DB缓存? [英] how to force drupal function to not use DB cache?

查看:101
本文介绍了如何强制drupal函数不使用DB缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块,我使用 node_load(array('nid'=> arg(1)));
现在的问题是这个函数继续获取其数据来自数据库缓存的node_load。

如何强制此函数不使用数据库缓存?

示例

我的链接为< a href =http://mydomain.com/node/344983 =nofollow> http://mydomain.com/node/344983

现在:

$ node = node_load(array('nid'=> arg(1)),null,true);

echo $ node-> nid。 - arg(1);

输出

435632 - 435632

是一个随机节点id(在系统上可用)

和每次我ctrl + F5我的浏览器我得到新的nid!

i have a module and i am using node_load(array('nid' => arg(1))); now the problem is that this function keep getting its data for node_load from DB cache.
how can i force this function to not use DB cache?
Example
my link is http://mydomain.com/node/344983
now:
$node=node_load(array('nid'=>arg(1)),null,true);
echo $node->nid . " -- " arg(1);
output
435632 -- 435632
which is a randomly node id (available on the system)
and everytime i ctrl+F5 my browser i get new nid!!

感谢您的帮助

推荐答案

例如,您使用它作为template.php文件的一部分,作为页面的一部分,还是作为外部模块?

Where are you calling this? For example, are you using it as part of your template.php file, as part of a page, or as an external module?

除非你使用自己的命名空间包装函数,否则尝试以 $ node - 例如,将它命名为 $ my_node 。根据上下文,node名称很可能被Drupal核心和其他模块访问和修改。

Unless you have this wrapped in a function with its own namespace, try naming the variable differently than $node -- for example, name it $my_node. Depending on the context, the 'node' name is very likely to be accessed and modified by Drupal core and other modules.

如果这是在一个函数内部发生,请尝试并让我知道输出是什么:

If this is happening inside of a function, try the following and let me know what the output is:

$test_node_1 = node_load(344983); // Any hard-coded $nid that actually exists
echo $test_node_1->nid;

$test_node_2 = node_load(arg(1)); // Consider using hook_menu loaders instead of arg() in the future, but that's another discussion
echo $test_node_2->nid;

$test_node_3 = menu_get_object(); // Another method that is better than arg()
echo $test_node_3->nid;

编辑

因为你使用hook_block,我想我看到你的问题 - 块本身正在缓存,而不是节点。

Since you're using hook_block, I think I see your problem -- the block itself is being cached, not the node.

尝试在hook_block中设置 BLOCK_NO_CACHE BLOCK_CACHE_PER_PAGE http:// api。 drupal.org/api/drupal/developer-hooks--core.php/function/hook_block/6

Try setting BLOCK_NO_CACHE or BLOCK_CACHE_PER_PAGE in hook_block, per the documentation at http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_block/6

您还应该尽量避免使用arg )只要有可能 - 这是一个有点安全风险,有更好的方法来完成任何arg()将在一个模块环境中做。

You should also try to avoid arg() whenever possible -- it's a little bit of a security risk, and there are better ways to accomplish just about anything arg() would do in a module environment.

编辑:*

一些示例代码,到:

function foo_block ($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
      case 'list':
        $blocks[0] = array(
          'info' => 'I am a block!',
          'status' => 1,
          'cache' => BLOCK_NO_CACHE // Add this line
        );
        return $block;
      case 'view':
       .....
    }
}

这篇关于如何强制drupal函数不使用DB缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆