Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 267 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal 7 temporary cache item won't expire

#1
I have a fairly expensive server call that I need to cache for 30 seconds. It seems however that I can not get the cache to expire.

In the code below, after the first time it caches, it will never get past $return->cache_data, even after the time() + 30 seconds.

Note, I can even print $cache->expire and it is definitely set to a time past 30 seconds ago and never updates.

I've manually cleared cache many times to confirm I get the same results.

Does anything look wrong with this?

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;
}
Reply

#2
There's nothing wrong with your code as such, I think the problem is in how `cache_set` behaves. From the [docs page](

[To see links please register here]

), passing a UNIX timestamp:

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

`CACHE_TEMPORARY` behaves like this:

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

My best guess is that because you're not implicitly forcing that general cache wipe (using [`cache_clear_all()`](

[To see links please register here]

)) the cache object will persist.

I think a simple way around it would just be to manually test the expiry time after your cache check, and let it fall through to re-setting that cache object if it has expired:

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through