Cache Expire: Drupal 7 module to flush the cache
Cache Expire is a Drupal 7 module to automatically flush the cache when node or a comment is published or updated, this module only flushes the cache without rebuilding the system bootstrap.
The module empties the tables:
- cache
- cache_block
- cache_filter
- cache_form
- cache_menu
- cache_page
- cache_path
- cache_views
- cache_views_data
and it self integrate with Cloudflare Cache Clear and with APC patched for APCu.
Here the code:
<?php
/**
* @file
*
* Expires caches automatically when certain Drupal actions are taken.
*/
/**
* Implements hook_menu().
*/
function cache_expire_menu() {
$items['admin/config/system/cache_expire'] = array(
'title' => 'Cache Expire',
'description' => 'Configuration for Cache Expire.',
'page callback' => 'drupal_get_form',
'page arguments' => array('cache_expire_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_node_insert().
*/
function cache_expire_node_insert($node) {
if($node->status == 1) {
cache_expire();
}
}
/**
* Implements hook_node_publish(), hook_node_update() and hook_node_unpublish().
*/
function cache_expire_node_update($node) {
if($node->status == 1) {
cache_expire();
}
elseif (isset($node->original->status) && $node->original->status == 1 && $node->status == 0){
cache_expire();
}
}
/**
* Implements hook_node_delete().
*/
function cache_expire_node_delete($node) {
if($node->status == 1) {
cache_expire();
}
}
/**
* Implements hook_comment_insert().
*/
function cache_expire_comment_insert($comment) {
if(node_load($comment->nid)->status == 1) {
if($comment->status == 1) {
cache_expire();
}
}
}
/**
* Implements hook_comment_publish(), hook_comment_update() and hook_commento_unpublish().
*/
function cache_expire_comment_update($comment) {
if(node_load($comment->nid)->status == 1) {
if ($comment->status == 1){
cache_expire();
}
elseif (isset($comment->original->status) && $comment->original->status == 1 && $comment->status == 0){
cache_expire();
}
}
}
/**
* Implements hook_comment_delete().
*/
function cache_expire_comment_delete($comment) {
if(node_load($comment->nid)->status == 1) {
if($comment->status == 1) {
cache_expire();
}
}
}
function cache_expire() {
try {
$core = array('cache', 'cache_block', 'cache_filter', 'cache_form', 'cache_menu', 'cache_page', 'cache_path', 'cache_views', 'cache_views_data');
foreach($core as $table) {
cache_clear_all('*', $table, TRUE);
}
if (module_exists('cloudflare_cc') && function_exists('cloudflare_cc_clear_cache') && (variable_get('cache_expire_cloudflare_flushes', 1))) {
cloudflare_cc_clear_cache();
}
if (module_exists('apc') && function_exists('apc_clear_apcu_cache') && (variable_get('cache_expire_apcu_flushes', 1))) {
apc_clear_apcu_cache();
}
if (variable_get('cache_expire_log_flushes', 1)) {
watchdog('cache_expire', 'Cache Cleared Successfully', array(), WATCHDOG_INFO);
}
} catch (Exception $e) {
return $e;
}
return NULL;
}
/**
* Configuration form for Cache Expire.
*/
function cache_expire_admin_settings() {
if (module_exists('cloudflare_cc') && function_exists('cloudflare_cc_clear_cache')) {
$form['cache_expire_cloudflare_flushes'] = array(
'#type' => 'checkbox',
'#title' => t('Cloudflare Cache Flushes'),
'#default_value' => variable_get('cache_expire_cloudflare_flushes', 0),
'#description' => t('Flush Cloudflare Cache.'),
);
}
if (module_exists('apc') && function_exists('apc_clear_apcu_cache')) {
$form['cache_expire_apcu_flushes'] = array(
'#type' => 'checkbox',
'#title' => t('APCu Cache Flushes'),
'#default_value' => variable_get('cache_expire_apcu_flushes', 0),
'#description' => t('Flush APCu Cache.'),
);
}
$form['cache_expire_log_flushes'] = array(
'#type' => 'checkbox',
'#title' => t('Log Cache Flushes'),
'#default_value' => variable_get('cache_expire_log_flushes', 1),
'#description' => t('Writes an entry to the watchdog log every time Cache Expire flushes the Boost static cache.'),
);
return system_settings_form($form);
}
Attachment | Size |
---|---|
cache_expire.zip | 8.65 KB |
Section:
Add new comment