-
Notifications
You must be signed in to change notification settings - Fork 1
/
apc.module
171 lines (153 loc) · 4.53 KB
/
apc.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Implements hook_xmlrpc().
*/
function apc_xmlrpc() {
$methods[] = array(
'apc_drush_flush', // Method name.
'apc_drush_flush', // Callback.
array(
'array', // Return variable.
'array', // Input variable.
),
t('XMLRPC callback to enable cache clear from Drush/CLI.'), // Description
);
return $methods;
}
/**
* XMLRPC callback to enable cache clear from Drush/CLI.
*/
function apc_drush_flush($variables) {
$cron_key = isset($states['cron_key']) ? $states['cron_key'] : NULL;
$clears = isset($variables['clears']) ? $variables['clears'] : array();
if (empty($cron_key) || state_get('cron_key', 'backdrop') != $cron_key) {
watchdog('apc', 'APC could not flush cache(s) because an invalid key was used.', array(), WATCHDOG_ERROR);
return array(
'success' => FALSE,
'message' => t('APC could not flush cache(s) because an invalid key was used.'),
);
}
else {
foreach ($clears as $bin => $cids) {
$cache = _cache_get_object($bin);
foreach ($cids as $serialized_cid => $wildcard) {
$cache->clear(unserialize($serialized_cid), $wildcard);
}
}
return array(
'success' => TRUE,
'message' => t('APC all requested flushes done.'),
);
}
}
/**
* Implementation of hook_init().
*
* Used for displaying the APC stats for debug purposes.
*/
function apc_init() {
global $user;
if (($user->uid == 0) || !settings_get('apc_show_debug', FALSE)
|| !user_access('access apc statistics') || strstr($_SERVER['PHP_SELF'], 'update.php')
|| strstr($_GET['q'], 'autocomplete')) {
return;
}
register_shutdown_function('apc_shutdown');
}
/**
* @file
* This integrates the Backdrop APC cache module.
*/
/**
* Implementation of hook_permission().
*/
function apc_permission() {
return array(
'access apc statistics' => array(
'title' => t('Access apc statistics'),
'description' => t('Allows access to the statistics reports of APC.'),
),
);
}
/**
* See apc_init() which registers this function as a shutdown function.
* Displays apc stats in the footer.
*/
function apc_shutdown() {
global $apc_statistics;
// Don't call theme() during shutdown if the registry has been rebuilt (such
// as when enabling/disabling modules on admin/build/modules) as things break.
// Instead, simply exit without displaying admin statistics for this page
// load. See http://drupal.org/node/616282 for discussion.
if (!function_exists('theme_get_registry') || !theme_get_registry()) {
return;
}
// Try not to break non-HTML pages.
if (function_exists('backdrop_get_http_header')) {
$header = backdrop_get_http_header('content-type');
if ($header) {
$formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
foreach ($formats as $format) {
if (strstr($header, $format)) {
return;
}
}
}
}
if (isset($apc_statistics) && is_array($apc_statistics)) {
print '<div id="apc-devel"><h2>' . t('APC statistics') . '</h2>';
$rows = array();
foreach ($apc_statistics as $row) {
if (is_array($row[2])) {
$row[2] = implode(',<br />', $row[2]);
}
$rows[] = $row;
}
print theme('table', array(
'header' => array(('Type'), t('Bin'), t('Cid(s)')),
'rows' => $rows,
));
print '</div>';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function apc_form_system_performance_settings_alter(&$form, $form_state) {
$form['clear_cache']['clear']['#submit'][] = 'apc_clear_user_cache';
$form['clear_cache']['clear']['#submit'][] = 'apc_clear_opcode_cache';
$form['clear_cache']['apc_user'] = array(
'#type' => 'submit',
'#value' => t('Clear APC user cache'),
'#submit' => array('apc_clear_user_cache'),
);
if (!extension_loaded('apcu')) {
$form['clear_cache']['apc_opcode'] = array(
'#type' => 'submit',
'#value' => t('Clear APC opcode cache'),
'#submit' => array('apc_clear_opcode_cache'),
);
}
}
/**
* Helper function to clear user cache.
*/
function apc_clear_user_cache() {
if (apc_clear_cache('user')) {
backdrop_set_message(t('Cleared APC user cache.'));
}
else {
backdrop_set_message(t('Unable to clear APC user cache.'), 'error');
}
}
/**
* Helper function to clear opcode cache.
*/
function apc_clear_opcode_cache() {
if (apc_clear_cache()) {
backdrop_set_message(t('Cleared APC opcode cache.'));
}
else {
backdrop_set_message(t('Unable to clear APC opcode cache.'), 'error');
}
}