Jump to content

Recommended Posts

I am trying to work with caching for the first time and I seem to struggle with the cache expiration time. As I was able to find out in the manual, I can either "Optionally specify max age (in seconds) OR oldest date string."

The max age is fairly easy, there are examples so that's fine. I kind of seem to fail though with the date. I set up a few variables I use for caching:

  • upcoming midnight (empty cache on midnight)
  • next Sunday midnight (empty cache on Sunday at midnight)
  • last day of the month midnight (empty cache at the end of month)
  • etc.

I think I saw "strtotime" in the code so I am using this for midnight

$midnight = date("Y-m-d 23:59:59")

which I pass as the expire argument. It does not seem to work though and the cache variable did not really refresh by midnight. I have all other caching disabled, only trying to use the $cache API variable as of now.

Could someone give me hint here what might be wrong with it?

 

Link to post
Share on other sites

The phrase you quote ("Optionally specify...") comes from the doc page for $cache->get(). With this function, you are specifying the maximum age of the cache you are trying to get (for example, up to 3600 second old).

I could be misunderstanding you, but it sounds as if you're trying to set the expiry time for caches that you have created, in which case use $cache->save(). Or perhaps that is what you're using, in which case follow the instructions for the expire argument on the relevant page (https://processwire.com/api/ref/wire-cache/save/).

Note: if you want them, the WireCache::expire constants are on the $cache page (https://processwire.com/api/ref/wire-cache/).

 

Link to post
Share on other sites

Hi and thank you, @BillH.

My aim is this:

  1. if the cache value has not expired,use the cached value
  2. if it has expired or does not exist, set the cache value and either
    1. set the expiration value as integer which then saves the value for eg. 3600 seconds
    2. set the expiration value as date and time which would be the latest date and time this value can be used before it expires

That's how I understood the cache->get method. The problem here is the "expire at midnight" part because I have quite a few content, such as news, which expire daily. There is no need to check it hourly but having it expire daily means in 86400 seconds, which can be quite late after that midnight timestamp.

Link to post
Share on other sites

To set a cache that will expire at midnight today:

// Expiry time string
$midnight = 'today 23:59:59';

// Alternatively, the start of tomorrow is midnight today
// $midnight = 'tomorrow';

// Save the cache
$cache->save('test-cache', 'Value for cache', $midnight);

So, to achieve what you want (if I understand you correctly), you could use the get() method something like this:

$midnight = 'tomorrow';
$str = $cache->get('test-cache', $midnight, function() {
	return "The next cache value";
});

By the way, if you haven't discovered it, the getInfo() function is really useful for debugging, for example:

print_r($cache->getInfo(false, 'test-cache'));

 

  • Like 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...