gpt4 book ai didi

php - Drupal 7 临时缓存项不会过期

转载 作者:可可西里 更新时间:2023-11-01 13:15:16 24 4
gpt4 key购买 nike

我有一个相当昂贵的服务器调用,我需要缓存 30 秒。但是,我似乎无法让缓存过期。

在下面的代码中,在第一次缓存之后,它永远不会超过 $return->cache_data,即使在 time() + 30 秒之后也是如此。

请注意,我什至可以打印 $cache->expire 并且它肯定设置为 30 秒前的时间并且永远不会更新。

我已经多次手动清除缓存以确认我得到了相同的结果。

这看起来有什么问题吗?

function mymodule_get_something($id) {
// set the unique cache id
$cid = 'id-'. $id;

// return data if there's an un-expired cache entry
// *** $cache ALWAYS gets populated with my expired data
if ($cache = cache_get($cid, 'cache_mymodule')) {
return $cache->data;
}

// set my super expensive data call here
$something = array('Double Decker Taco', 'Burrito Supreme');

// set the cache to expire in 30 seconds
cache_set($cid, $something, 'cache_mymodule', time() + 30);

// return my data
return $something;
}

最佳答案

您的代码没有任何问题,我认为问题在于 cache_set 的行为方式。来自docs page ,传递一个 UNIX 时间戳:

Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

CACHE_TEMPORARY 行为如下:

Indicates that the item should be removed at the next general cache wipe.

我最好的猜测是,因为您没有隐式地强制执行一般缓存删除(使用 cache_clear_all() ),所以缓存对象将持续存在。

我认为一个简单的解决方法是在缓存检查后手动测试过期时间,如果缓存对象过期则让它失败以重新设置该缓存对象:

if ($cache = cache_get($cid, 'cache_mymodule')) {
if ($cache->expire > REQUEST_TIME) {
return $cache->data;
}
}

关于php - Drupal 7 临时缓存项不会过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com