Enterprise_CacheFlush_MakeSnsEvent.php
5.45 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace W3TC;
/**
* Purge using AmazonSNS object
*/
class Enterprise_CacheFlush_MakeSnsEvent extends Enterprise_SnsBase {
private $messages = array();
private $messages_by_signature = array();
/**
* Flushes DB caches
*
*/
function dbcache_flush() {
$this->_prepare_message( array( 'action' => 'dbcache_flush' ) );
}
/**
* Flushes minify caches
*
*/
function minifycache_flush() {
$this->_prepare_message( array( 'action' => 'minifycache_flush' ) );
}
/**
* Flushes object caches
*
*/
function objectcache_flush() {
$this->_prepare_message( array( 'action' => 'objectcache_flush' ) );
}
/**
* Flushes fragment caches
*
*/
function fragmentcache_flush() {
$this->_prepare_message( array( 'action' => 'fragmentcache_flush' ) );
}
/**
* Flushes fragment cache based on group
*
*/
function fragmentcache_flush_group( $group ) {
$this->_prepare_message( array( 'action' => 'fragmentcache_flush_group',
'group' => $group ) );
}
/**
* Flushes query string
*
*/
function browsercache_flush() {
$this->_prepare_message( array( 'action' => 'browsercache_flush' ) );
}
/**
* Purges Files from Varnish (If enabled) and CDN
*
*/
function cdn_purge_files( $purgefiles ) {
$this->_prepare_message( array( 'action' => 'cdn_purge_files', 'purgefiles' => $purgefiles ) );
}
/**
* Performs garbage collection on the pgcache
*/
function pgcache_cleanup() {
$this->_prepare_message( array( 'action' => 'pgcache_cleanup' ) );
}
/**
* Flushes the system APC
*
* @return bool
*/
function opcache_flush() {
$this->_prepare_message( array( 'action' => 'opcache_flush' ) );
}
/**
* Reloads/compiles a PHP file.
*
* @param string $filename
* @return mixed
*/
function opcache_flush_file( $filename ) {
return $this->_prepare_message( array(
'action' => 'opcache_flush_file',
'filename' => $filename ) );
}
/**
* Purges/Flushes post page
*
* @param unknown $post_id
* @return boolean
*/
function flush_post( $post_id ) {
return $this->_prepare_message( array( 'action' => 'flush_post', 'post_id' => $post_id ) );
}
/**
* Purges/Flushes posts from caches
*
* @param unknown $extras
* @return boolean
*/
function flush_posts( $extras ) {
return $this->_prepare_message( array(
'action' => 'flush_posts',
'extras' => $extras ) );
}
/**
* Purges/Flushes all enabled caches
*
* @return boolean
*/
function flush_all( $extras ) {
return $this->_prepare_message( array(
'action' => 'flush_all',
'extras' => $extras
) );
}
/**
* Purges/Flushes url
*
* @param string $url
* @return boolean
*/
function flush_url( $url ) {
return $this->_prepare_message( array( 'action' => 'flush_url', 'url' => $url ) );
}
/**
* Makes get request to url specific to post, ie permalinks
*
* @param unknown $post_id
* @return mixed
*/
function prime_post( $post_id ) {
return $this->_prepare_message( array( 'action' => 'prime_post', 'post_id' => $post_id ) );
}
/**
* Setups message list and if it should be combined or separate
*
* @param unknown $message
* @return boolean
*/
private function _prepare_message( $message ) {
$message_signature = json_encode( $message );
if ( isset( $this->messages_by_signature[$message_signature] ) )
return true;
$this->messages_by_signature[$message_signature] = '*';
$this->messages[] = $message;
$action = $this->_get_action();
if ( !$action ) {
$this->execute_delayed_operations();
return true;
}
return true;
}
/**
* Sends messages stored in $messages
*
* @return boolean
*/
public function execute_delayed_operations() {
if ( count( $this->messages ) <= 0 )
return true;
$this->_log( $this->_get_action() . ' sending messages' );
$message = array();
$message['actions'] = $this->messages;
$message['blog_id'] = Util_Environment::blog_id();
$message['host'] = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : null;
$message['hostname'] = @gethostname();
$v = json_encode( $message );
try {
$api = $this->_get_api();
if ( defined( 'WP_CLI' ) && WP_CLI )
$origin = 'WP CLI';
else
$origin = 'WP';
$this->_log( $origin . ' sending message ' . $v );
$this->_log( 'Host: ' . $message['host'] );
if ( isset( $_SERVER['REQUEST_URI'] ) )
$this->_log( 'URL: ' . $_SERVER['REQUEST_URI'] );
if ( function_exists( 'current_filter' ) )
$this->_log( 'Current WP hook: ' . current_filter() );
$backtrace = debug_backtrace();
$backtrace_optimized = array();
foreach ( $backtrace as $b ) {
$opt = isset( $b['function'] ) ? $b['function'] . ' ' : '';
$opt .= isset( $b['file'] ) ? $b['file'] . ' ' : '';
$opt .= isset( $b['line'] ) ? '#' . $b['line'] . ' ' : '';
$backtrace_optimized[] = $opt;
}
$this->_log( 'Backtrace ', $backtrace_optimized );
$r = $api->publish( $this->_topic_arn, $v );
if ( $r->status != 200 ) {
$this->_log( "Error: {$r->body->Error->Message}" );
return false;
}
} catch ( \Exception $e ) {
$this->_log( 'Error ' . $e->getMessage() );
return false;
}
// on success - reset messages array, but not hash (not resent repeatedly the same messages)
$this->messages = array();
return true;
}
/**
* Gets the current running WP action if any. Returns empty string if not found.
*
* @return string
*/
private function _get_action() {
$action = '';
if ( function_exists( 'current_filter' ) )
$action = current_filter();
return $action;
}
}