Jump to content

Best practice for fetching a page in a loop and avoiding cached objects


bracketfire
 Share

Recommended Posts

I've got a cron job that bootstraps processwire as shown in https://processwire.com/api/include/

It watches pages in a loop (auctions happening live) and checks fields periodically.

while(true) {
    wire('pages')->uncacheAll();
    $auction = wire('pages')->get($auction_id);

It appears that without the uncacheAll(), changes to the page (changes made external to this script) aren't picked up by the get().

Is this the best way to assure I don't get cached pages?  I found this method by looking at wire/core/Pages.php and didn't see a way to invalidate a certain page or pass an option to get that says to avoid any caching.

Link to comment
Share on other sites

Looks good to me to uncacheAll pages to make sure you get newly fetched page. But I'm surprised this is needed at all here. But then I think I maybe don't really understand all context and what uncacheAll() does really. Are pages cached across other processes/requests and scripts?

Link to comment
Share on other sites

From my understanding, as the script is always running, the initial request will get the page and it will be cached internally. Subsequent requests would then use the local cached copy instead of hitting the database.

It looks like Page has an uncache() method which accepts a single parameter page ID. So you could replace your call with:

wire('pages')->uncache($pageId);
  • Like 1
Link to comment
Share on other sites

I see uncache() now, I guess what's confusing is that there's a page cache and a selector cache.  It looks like you are right, in this case where I'm using a simple id=XX query, using uncache() on the specific page is probably enough.

And yes, Craig, you are right - this is a loop in a cron job so it's a little different that a normal page generation deal that creates HTML for the browser.

Link to comment
Share on other sites

There's a little more to it.  A seperate cron launches every minute, checks for auctions that are close (within 5 minutes) of closing, and launches this script in the background for each one.  while(true) allows this script to poll the PW page for last minute bids, etc. and when the auction closes it breaks the loop, emails the winner, etc. and exits.  So this script isn't actually a cron, it's launched by a cron to watch a specific page for a while, then exits.

  • Like 1
Link to comment
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
 Share

×
×
  • Create New...