【发布时间】:2011-11-13 20:14:31
【问题描述】:
我有一个相当昂贵的服务器调用,我需要缓存 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;
}
【问题讨论】: