Drupal Module Advanced application authcache-dynamic loading content Tutorial

Source: Internet
Author: User
Tags foreach php file drupal

Related article: Drupal module explain-authcache caching principle detailed tutorial

This article is about how to implement the personalized content of a cached page if you modify Authcache's core code.

Advanced application authcache-Dynamic loading content tutorial "/>


Common cache, more or less to be personalized processing, such as user name display, dynamic loading of user information, user friends and so on.

In general, this kind of local personalization is accomplished by two means: one is SSI and the other is CSI.

Authcache itself can implement local personalization, module called p13n.

The Authcache Ajax module belongs to the Csi,esi module and should be SSI, but because the ESI module needs to build the varnish server, configuring VCL, plus Server setup issues, can cause ESI error, and its own ESI pass cookies can also be problematic, Therefore, ESI is actually quite complex to implement.

Therefore, if we want to use the server-side personalization, modify some of the content with the PHP modification according to certain conditions, we need to hack some authcache code.
1. Autcache.module Documents

Find the following line 188
Invoke Cache backends and serve page.
Modified as follows:

The code is as follows Copy Code
Invoke Cache backends and serve page.
if (authcache_page_is_cacheable ()) {
$cache = Authcache_backend_cache_save ();
Authcache_serve_page_from_cache ($cache, Authcache_key ());
}
else {
Process HTML Result
Global $conf;
$conf [' page_compression '] = FALSE;

$cache = new StdClass ();

Process HTML Result
$cache->data[' body '] = ob_get_contents ();
Ob_clean ();

foreach (Variable_get (' authcache_page_process ', Array ()) as $include) {
Require_once Drupal_root. '/' . $include;
}
foreach (Variable_get (' Authcache_page_process_interface ', Array ()) as $process) {
Require_once Drupal_root. '/' . $include;
if (is_callable ($process)) {
$process ($cache);
}
}
echo $cache->data[' body '];
}
Exit
}


Among them, the main is added else after the processing code.
2. Authcache.cache.inc Documents

Start with 85 lines, to the end of the function, and modify to the following format.

The code is as follows Copy Code
$return _compressed = FALSE; NEW//don ' t send compressed content

if ($page _compression) {
Header (' vary:accept-encoding ', FALSE);
If page_compression is enabled, the cache contains gzipped data.
if ($return _compressed) {
$cache->data[' body '] is already gzip ' Ed, so make sure
Zlib.output_compression does not compress it once more.
Ini_set (' zlib.output_compression ', ' 0 ');
Header (' Content-encoding:gzip ');
}
else {
The client does not support compression, so unzip the "data in the"
Cache. Strip the gzip header and run uncompress.
$cache->data[' body '] = gzinflate (substr (substr ($cache->data[' body '), 10), 0,-8);
}
}

NEW
foreach (Variable_get (' authcache_page_process ', Array ()) as $include) {
Require_once Drupal_root. '/' . $include;
}
foreach (Variable_get (' Authcache_page_process_interface ', Array ()) as $process) {
if (is_callable ($process)) {
$process ($cache);
}
}



Note that there are two places where the///new callout indicates the new addition, and the middle section is the original code.

After we have finished, we will be finished.
How to use it?

Create a new file, such as under the custom module, called Custom_authcache.inc, and paste the following code:

The code is as follows Copy Code
<?php
/**
Add The following lines to settings.php
 
$conf [' authcache_page_process '] = ' sites/all/modules/ Custom/custom/custom_authcache.inc ';
$conf [' authcache_page_process_interface '] [] = ' custom_authcache_common_process ';
 
If You are want to add more process interface, add your function name as a item in this array, $conf [' AUTHCAC He_page_process_interface '].
If you are want to include file, please add the file name to this array, $conf [' authcache_page_process ']
 
Core Changes:
modules/authcache/authcache.cache.inc
modules/authcache/authcache.module
**/
 
/*
* Process authcache content to replace content
*/
Function custom_authcache_common_process (& $cache) {
  $cache->data[' body '] = str_ireplace (' <span id= ' replace_placeholder_1 '/> ', _get_real_data (), $cache- >data[' body ']);
}


Look at the comments above to copy two lines of code to the settings.php file. The
specific note notes are very detailed, I believe it should be no problem.

In this way, the Custom_authcache_common_process function can dynamically replace the contents of the HTML and achieve the purpose of personalizing the page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.