Hi to everybody.

I'm using authcache and filecache with a drupal 7 installation, everithing is going fine and the pages are cached well.

The problem is that i have a custom module with a couple of pages created with the hook_menu().

So for example i have this page: '/get/data' witch print (not return) some data for an ajax request, this page with the normal drupal cache has been cached, but not with authcache.

So giving a look to the headers the request is always:
Cache-Control max-age=0

And the answer si always:

Cache-Control	no-cache, must-revalidate, post-check=0, pre-check=0
X-Drupal-Cache	MISS

How can include this page/menu item in the authcache+filecache system?

Regards

Comments

znerol’s picture

The first thing you should ensure is that your menu-callback does not call exit(). If you need to terminate a request earlry, then use drupal_exit. This will invoke all exit hooks (see hook_exit) and will allow authcache to store the page.

If this is not the cause, then I recommend to enable the Authcache Debug module and enable logging to watchdog for authcache enabled roles. You find the setting in Administration » Configuration » System » Authcache » Debug. The log can be accessed from Administration » Reports » Recent log messages.

andrea.cavattoni’s picture

Thank you, it helped me a lot.

First of all drupal_exit was necessary for call authcache and second the headers must be setted with "drupal_add_http_header" and not with php "headers".

So the code looks like this:

function mymodule_menu() {
  $items = array();
  $items['get/pages/%'] = array(
    'page callback' => 'page_get',
    'access callback' => TRUE,
    'type'            => MENU_CALLBACK,
  );
    return $items;
}

function page_get($nid = NULL) {
	//GET THE DATA ($data)
	drupal_add_http_header('Content-Type', 'application/json');
	print json_encode($data, TRUE);
	drupal_exit();
 }
}

Hope it helps.

znerol’s picture

Perfect! When working with JSON, you might consider reusing drupal_json_output. This actually sets the proper header and also does a check-for-null before the echo. Also note that it shouldn't be necessary to terminate the request early. There are some JSON examples in drupal core and none of them calls drupal_exit directly. For reference here is how user/autocomplete works:

The hook_menu implementation (user_menu):

function user_menu() {
  $items['user/autocomplete'] = array(
    'title' => 'User autocomplete',
    'page callback' => 'user_autocomplete',
    'access callback' => 'user_access',
    'access arguments' => array('access user profiles'),
    'type' => MENU_CALLBACK,
    'file' => 'user.pages.inc',
  );

And the corresponding page callback (user_autocomplete):

function user_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_select('users')->fields('users', array('name'))->condition('name', db_like($string) . '%', 'LIKE')->range(0, 10)->execute();
    foreach ($result as $user) {
      $matches[$user->name] = check_plain($user->name);
    }
  }

  drupal_json_output($matches);
}
znerol’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.