-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
cachify_db.class.php
236 lines (216 loc) · 4.49 KB
/
cachify_db.class.php
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
<?php
/**
* Cachify_DB
*/
final class Cachify_DB {
/**
* Availability check
*
* @since 2.0.7
* @change 2.0.7
*
* @return boolean true/false TRUE when installed
*/
public static function is_available() {
return true;
}
/**
* Caching method as string
*
* @since 2.1.2
* @change 2.1.2
*
* @return string Caching method
*/
public static function stringify_method() {
return 'DB';
}
/**
* Store item in cache
*
* @since 2.0
* @change 2.0
*
* @param string $hash Hash of the entry.
* @param string $data Content of the entry.
* @param integer $lifetime Lifetime of the entry.
*/
public static function store_item( $hash, $data, $lifetime ) {
/* Do not store empty data. */
if ( empty( $data ) ) {
trigger_error( __METHOD__ . ": Empty input.", E_USER_WARNING );
return;
}
/* Store */
set_transient(
$hash,
array(
'data' => $data,
'meta' => array(
'queries' => self::_page_queries(),
'timer' => self::_page_timer(),
'memory' => self::_page_memory(),
'time' => current_time( 'timestamp' ),
),
),
$lifetime
);
}
/**
* Read item from cache
*
* @since 2.0
* @change 2.0
*
* @param string $hash Hash of the entry.
* @return mixed Content of the entry
*/
public static function get_item( $hash ) {
return get_transient( $hash );
}
/**
* Delete item from cache
*
* @since 2.0
* @change 2.0
*
* @param string $hash Hash of the entry.
* @param string $url URL of the entry [optional].
*/
public static function delete_item( $hash, $url = '' ) {
delete_transient( $hash );
}
/**
* Clear the cache
*
* @since 2.0
* @change 2.0
*/
public static function clear_cache() {
/* Init */
global $wpdb;
/* Clear */
$wpdb->query( 'DELETE FROM `' . $wpdb->options . "` WHERE `option_name` LIKE ('\_transient%.cachify')" );
}
/**
* Print the cache
*
* @since 2.0
* @change 2.3.0
*
* @param bool $sig_detail Show details in signature.
* @param array $cache Array of cache values.
*/
public static function print_cache( $sig_detail, $cache ) {
/* No array? */
if ( ! is_array( $cache ) ) {
return;
}
/* Content */
echo $cache['data'];
/* Signature - might contain runtime information, so it's generated at this point */
if ( isset( $cache['meta'] ) ) {
echo self::_cache_signature( $sig_detail, $cache['meta'] );
}
/* Quit */
exit;
}
/**
* Get the cache size
*
* @since 2.0
* @change 2.0
*
* @return integer Column size
*/
public static function get_stats() {
/* Init */
global $wpdb;
/* Read */
return $wpdb->get_var(
'SELECT SUM( CHAR_LENGTH(option_value) ) FROM `' . $wpdb->options . "` WHERE `option_name` LIKE ('\_transient%.cachify')"
);
}
/**
* Generate signature
*
* @since 2.0
* @change 2.3.0
*
* @param bool $detail Show details in signature.
* @param array $meta Content of metadata.
* @return string Signature string
*/
private static function _cache_signature( $detail, $meta ) {
/* No array? */
if ( ! is_array( $meta ) ) {
return;
}
if ( $detail ) {
return sprintf(
"\n\n<!-- %s\n%s @ %s\n%s\n%s\n-->",
'Cachify | http://cachify.de',
'DB Cache',
date_i18n(
'd.m.Y H:i:s',
$meta['time']
),
sprintf(
'Without Cachify: %d DB queries, %s seconds, %s',
$meta['queries'],
$meta['timer'],
$meta['memory']
),
sprintf(
'With Cachify: %d DB queries, %s seconds, %s',
self::_page_queries(),
self::_page_timer(),
self::_page_memory()
)
);
} else {
return sprintf(
"\n\n<!-- %s\n%s @ %s -->",
'Cachify | http://cachify.de',
__( 'Generated', 'cachify' ),
date_i18n(
'd.m.Y H:i:s',
$meta['time']
)
);
}
}
/**
* Return query count
*
* @since 0.1
* @change 2.0
*
* @return integer Number of queries
*/
private static function _page_queries() {
return $GLOBALS['wpdb']->num_queries;
}
/**
* Return execution time
*
* @since 0.1
* @change 2.0
*
* @return integer Execution time in seconds
*/
private static function _page_timer() {
return timer_stop( 0, 2 );
}
/**
* Return memory consumption
*
* @since 0.7
* @change 2.0
*
* @return string Formatted memory size
*/
private static function _page_memory() {
return ( function_exists( 'memory_get_usage' ) ? size_format( memory_get_usage(), 2 ) : 0 );
}
}