-
Notifications
You must be signed in to change notification settings - Fork 0
/
varnish_akamai_cache_clear.module
executable file
·120 lines (97 loc) · 4.42 KB
/
varnish_akamai_cache_clear.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
This module is intended for clearing cache on demand for any URL.
Jyothi K Reddy
*/
module_load_include('module', 'varnish');
module_load_include('module', 'akamai');
/**
* Implements hook_menu().
*/
function varnish_akamai_cache_clear_menu() {
$items['admin/config/varnish-akamai'] = array(
'title' => 'Varnish/Akamai Cache Clear',
'page callback' => 'drupal_get_form',
'page arguments' => array('varnish_akamai_cache_clear_cache_control'),
'access arguments' => array('administer varnish/akamai'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function varnish_akamai_cache_clear_permission() {
return array(
'administer varnish/akamai' => array(
'title' => t('Administer varnish/Akamai Cache control utility'),
'description' => t('Perform varnish/Akamai cache clear functions'),
),
);
}
function varnish_akamai_cache_clear_cache_control() {
$form = array();
$form['path'] = array(
'#type' => 'textfield',
'#title' => t('Path/URL'),
'#description' => t('Enter the URL that you want to clear cache on. URL entries should be relative to the basepath. (e.g. node/1, content/pretty-title, sites/default/files/some/image.png'),
);
$form['domain_override'] = array(
'#type' => 'select',
'#title' => t('Akamai Domain'),
'#default_value' => variable_get('akamai_domain', 'staging'),
'#options' => array(
'staging' => t('Staging'),
'production' => t('Production'),
),
'#description' => t('The Akamai domain to use for cache clearing. Defaults to the Domain setting from the settings page.')
);
$form['refresh'] = array(
'#type' => 'radios',
'#title' => t('Clearing Action Type'),
'#default_value' => variable_get('akamai_action', ''),
'#options' => array(
'remove' => t('Remove'),
'invalidate' => t('Invalidate'),
),
'#description' => t('<b>Remove:</b> Purge the content from Akamai edge server caches. The next time the edge server receives a request for the content, it will retrieve the current version from the origin server. If it cannot retrieve a current version, it will follow instructions in your edge server configuration.<br/><br/><b>Invalidate:</b> Mark the cached content as invalid. The next time the Akamai edge server receives a request for the content, it will send an HTTP conditional get (If-Modified-Since) request to the origin. If the content has changed, the origin server will return a full fresh copy; otherwise, the origin normally will respond that the content has not changed, and Akamai can serve the already-cached content.<br/><br/><b>Note that <em>Remove</em> can increase the load on the origin more than <em>Invalidate</em>.</b> With <em>Invalidate</em>, objects are not removed from cache and full objects are not retrieved from the origin unless they are newer than the cached versions.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Clear Varnish/Akamai Cache'),
);
return $form;
}
/**
* Implements hook_node_submit().
*/
function varnish_akamai_cache_clear_cache_control_submit($form, &$form_state) {
global $base_url;
//Determining the protocol on which the application runs.
if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$protocol = "https://";
}
else {
$protocol = "http://";
}
//Removing the protocol for clearning varnish
$url_no_protocol = str_replace($protocol, '', $base_url);
$alias = filter_xss($form_state['values']['path']);
$path = drupal_lookup_path("source", $alias);
if(!empty($path)) {
$did_clear = akamai_clear_url($path);
$version = floatval(variable_get('varnish_version'));
$command = $version >= 3 ? "ban" : "purge";
$result = _varnish_terminal_run(array("$command req.http.host ~ $url_no_protocol && req.url ~ \"$path\""));
$result = serialize($result);
if($result && $did_clear) {
drupal_set_message("Cache cleared for '".$alias."' from system with node path as '".$path."'");
drupal_set_message("Please allow 10 to 15 minutes for Akamai Cache to be cleared");
}
else{
drupal_set_message("cache not cleared" ,'error');
}
}
else{
drupal_set_message('Please enter a valid URL', 'error');
}
}