Caching AJAX results

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
Andy Inman's picture

A while ago I started working on an experimental module to cache the results of standard Drupal FAPI AJAX calls. An example use-case, you have form with a drop-down selector "country" and a second drop-down "city". The contents of the city selector are dependent on the selected country. So, when you change country, the list of relevant cities for that country gets loaded via AJAX. Now, that list is always the same (until a new city comes along or one gets renamed) so can feasibly be cached.

Under testing I got some pretty impressive results, I don't remember the figures but the speed-up was a factor of 10 or more. But, I didn't have nor currently have any real need for this module - most of my clients are small business who don't need especially high-performance site - so I shelved it. Likewise, I don't have access to a high-traffic site that would be needed for testing, so the best I could do would be to emulate relevant conditions. Nonetheless I'm interested in doing further development.

So, I'm wondering:

  • Is there already a module which does this (I haven't seen one.)
  • Does anybody need this functionality and could be prepared to help test it under live conditions?

Comments

Boost

mikeytown2's picture

Boost can cache AJAX calls as long as they are GET and not POST.

Ok, so another approach could

Andy Inman's picture

Ok, so another approach could be to make Drupal forms handling do GETs. Maybe that's been done already?



Currently part of the team at https://lastcallmedia.com in a senior Drupal specialist role.

No GET request in Drupal ajax.

denny84's picture

Drupal ajax system does not use GET request unfortunately. Its also hardcoded (very bad). That is one thing that needs to change in D8. So I am assuming FAPI is Drupal ajax, hence it will use POST requests. The only way to cache is through storing the response in a contextual global object by key. Where key is the ajax path. Something that has been implemented in D6 Ctools warmcaching method. But its bit complicated (at least for me).

Agreed, that was my

Andy Inman's picture

Agreed, that was my conclusion too - no way (other than patches) to make FAPI/AJAX do GETs. That was pretty much the reason I started work on this module in the first place. The ajax path tends to be system/ajax in most cases, so no use as a cache key. Result, additional stuff is needed, but we have hook_form_alter, so that starts to become feasible.



Currently part of the team at https://lastcallmedia.com in a senior Drupal specialist role.

Oh wait ..

denny84's picture

Are you talking about server side caching for POST requests? Because I was referring to client side. May be misunderstood?

Yes, was talking about

Andy Inman's picture

Yes, was talking about server-side caching, but it's the same issue - POST requests are not cached by Drupal nor most front-end caches. To make FAPI/AJAX use GET would presumably require patching of both core PHP and Javascript, so not trivial nor easily maintainable.



Currently part of the team at https://lastcallmedia.com in a senior Drupal specialist role.

Well we cannot have GET

denny84's picture

Well we cannot have GET requests with forms, because we are sending additional data to the server (like name filled in the form etc). So thats out of question. It must be a POST request. Unless off course you have a hook menu like

function mymodule_menu(myajaxpath/%/%/%/%){
// where the wildcard is the posted data from the client
}

Then you can make a GET request to a URL that will be dynamically formed using javascript.

var url = "myajaxpath /" + formdata1 + "/" + formdata2 + "/" + formdata3 .. and so on and so forth.

Secondly, Drupal may not cache the output html but it does cache the form POST data inside database cache table. So by using Redis / Memcache this can be solved for faster responses.

Technically caching POST requests (server side off course) is illegal and not allowed in http protocol unless proper headers are used.

Not caching at HTTP level

Andy Inman's picture

Technically caching POST requests (server side off course) is illegal and not allowed in http protocol unless proper headers are used.

In my original message I wasn't talking about an HTTP cache, but PHP-level functionality to cache the result from the relevant PHP code which calculates it. It becomes irrelevant whether this PHP function receives the form data via GET or POST.



Currently part of the team at https://lastcallmedia.com in a senior Drupal specialist role.

You are right

denny84's picture

that at php level GET or POST is irrelevant and thats what I mentioned earlier that Drupal actually caches the form request once, so yes, caching is available for all built in ajax requests for FAPI. But not for flat HTML output, for that special headers are required.

To summarize for all POST ajax requests

A. No cache available at client side (would be great if added in D8)
B. No cache available for flat HTML (would be great if added in D8)
C. Only php cache available for forms.