Jump to content

conditional output and cache


joe_g
 Share

Recommended Posts

Hi there,

If I do some conditional output, for example

if($config->ajax) {

   echo 'something';

} else {

   echo 'somtething else';

}

And then switch on cache for this particular page. I guess the first hit will determine what gets stored in the cache? The second hit will get the same results regardless.

So that means I cant really have conditional output and caching at the same time, right?

If I use ProCache, it's cached by URL instead, right? So then there can be multiple versions of the same page, depending on the URL?

For example if I use #!, then the ?_escaped_fragment= version of the page would be cached separately, I suppose?

Just trying to see If I understand how it works correctly

thanks,

J

*edit: procache question

Link to comment
Share on other sites

And then switch on cache for this particular page. I guess the first hit will determine what gets stored in the cache? The second hit will get the same results regardless.

That's correct.

So that means I cant really have conditional output and caching at the same time, right?

Incorrect. You'd want to add some additional GET or POST variable to your AJAX requests in order to differentiate them and bypass the cache. The drawback is that only your regular requests will be cached, and your AJAX requests won't. You can always get around this by using MarkupCache for caching either type of request and delivering the appropriate one according to the request method (regular vs. ajax), but it'd have to be a pretty slow/heavy request before I'd bother with that. 

Perhaps a better way to achieve it would be to use pagination, as paginated URLs are fully cached (using PW's template cache). That's assuming you don't need pagination on the template for another reason. But by enabling page numbers, you could delegate your regular full-page request to /path/to/page/, and your ajax requests to /path/to/page/page2/, and page3, page4, etc., as needed. Your template code could detect it like this:

if($input->pageNum == 2) {
  // deliver ajax request
} else if($input->pageNum == 1) {
  // deliver regular full-page request
} else {
  throw new Wire404Exception(); // prevent caching
}
If I use ProCache, it's cached by URL instead, right? So then there can be multiple versions of the same page, depending on the URL?
For example if I use #!, then the ?_escaped_fragment= version of the page would be cached separately, I suppose?

Partially correct. If you use ProCache, you can cache URL segments, but not the results of #anchors, or requests containing GET/POST variables. You'd want to convert those to URL segments and then you could cache everything as needed. For example, rather than /path/to/page/?fragment=version you'd use /path/to/page/fragment/version/, and "fragment" would be represented by $input->urlSegment1 and "version" would be represented by $input->urlSegment2. 

  • 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...