Cached ajax callback in drupal 7

Hi, if you need to create menu callback for ajax functions you can use hook_menu() but there are a lot of nuances. First of all do you want to cache callback data in cache_page and/or reverse proxy(if you use it)? If yes it will increase your performance significantly. I have seen in multiple modules such code.


function mycustom_menu_callback() {

  // Doing something and putting result in $output.

  print $output;

  drupal_exit();

}

This is right approach only if you don't want to cache your data and it will hit your drupal every time this page is loaded. As for me it's not right if caching can be used.

function mycustom_menu_callback() {

  // Doing something and putting result in $output.

  print $output;

 return NULL;

}

If you will return NULL after printing your output, data will be stored in cache_page if will be accessed by anonymous user.

it will give you great performance enhancements especially if you use Varnish or any other reverse proxy.

One more thing if you use ajax to get your data please check request method it should be GET, POST request will not put page into cache.

For more details please look at this function https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal....

Thank you for reading!