Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:03+00:00 2026-05-26T22:54:03+00:00

I have a fairly expensive server call that I need to cache for 30

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:54:03+00:00Added an answer on May 26, 2026 at 10:54 pm

    There’s nothing wrong with your code as such, I think the problem is in how cache_set behaves. From the docs page, 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()) 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;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple, though expensive, task that I need to run in
I have a fairly small MySQL database (a Textpattern install) on a server that
I have a service that uses a fairly expensive object to create. I would
I have a fairly expensive array calculation (SpectralResponse) which I like to keep to
I have a fairly standards compliant XHTML+CSS site that looks great on all browsers
I have a fairly large codebase that depends on MooTools v1.11 and am about
I have a fairly good feel for what MySQL replication can do. I'm wondering
I have a fairly simple const struct in some C code that simply holds
My problem doesn't seem like a unique one. Basically I have a fairly expensive
I have fairly large script within an .svg file and I'd like to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.