Skip to content

Commit 5beca2e

Browse files
authored
Merge pull request #2 from JorgenEvens/chore/detect-apcu-vs-apc
[apcu] Use apcu interface by default.
2 parents 8462cac + b403699 commit 5beca2e

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

cache.php

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
2+
if (!extension_loaded('apcu')) {
3+
function apcu_cache_info($limited = false) { return apc_cache_info('user', $limited); }
4+
function apcu_sma_info($limited = false) { return apc_sma_info($limited); }
5+
function apcu_fetch($key, &$success) { return apc_fetch($key, $success); }
6+
function apcu_delete($key) { return apc_delete($key); }
7+
class ApcuIterator extends ApcIterator {}
8+
}
9+
10+
$apcVersion = extension_loaded('apcu') ? 'APCu' : 'APC';
211
$opcache = opcache_get_status(true);
312
$apc = array(
4-
'cache' => apc_cache_info('user'),
5-
'sma' => apc_sma_info(true)
13+
'cache' => apcu_cache_info(),
14+
'sma' => apcu_sma_info(true)
615
);
716

817
function percentage( $a, $b ) {
@@ -46,7 +55,7 @@ function opcache_stat( $stat ) {
4655
return $opcache['opcache_statistics'][$stat];
4756
}
4857

49-
function apc_mem( $key ) {
58+
function apcu_mem( $key ) {
5059
global $apc;
5160

5261
if( $key == 'total' )
@@ -56,13 +65,13 @@ function apc_mem( $key ) {
5665
return $apc['sma']['avail_mem'];
5766

5867
if( $key == 'used' )
59-
return apc_mem('total') - apc_mem('free');
68+
return apcu_mem('total') - apcu_mem('free');
6069

6170
return 0;
6271

6372
}
6473

65-
function apc_ref() {
74+
function apcu_ref() {
6675
global $apc;
6776

6877
if( !empty( $apc['cache']['cache_list'] ) )
@@ -154,14 +163,14 @@ function sort_list(&$list) {
154163
}
155164

156165
// APC
157-
if( isset( $_GET['action'] ) && $_GET['action'] == 'apc_restart' ) {
158-
apc_delete( new ApcIterator('#.*#') );
166+
if( isset( $_GET['action'] ) && $_GET['action'] == 'apcu_restart' ) {
167+
apcu_delete( new ApcuIterator('#.*#') );
159168
redirect('?');
160169
}
161170

162-
if( isset( $_GET['action'] ) && $_GET['action'] == 'apc_delete' ) {
163-
apc_delete( new ApcIterator('user',get_selector()) );
164-
redirect( '?action=apc_select&selector=' . $_GET['selector'] );
171+
if( isset( $_GET['action'] ) && $_GET['action'] == 'apcu_delete' ) {
172+
apcu_delete( new ApcuIterator('user',get_selector()) );
173+
redirect( '?action=apcu_select&selector=' . $_GET['selector'] );
165174
}
166175
?><html>
167176
<head>
@@ -192,7 +201,7 @@ function sort_list(&$list) {
192201
<body>
193202
<div class="wrap">
194203
<div>
195-
Goto: <a href="#opcache">PHP Opcache</a> or <a href="#apcu">APCu</a>
204+
Goto: <a href="#opcache">PHP Opcache</a> or <a href="#apcu"><?=$apcVersion?></a>
196205
</div>
197206
<h2 id="opcache">PHP Opcache</h2>
198207
<div>
@@ -266,42 +275,42 @@ function sort_list(&$list) {
266275
</div>
267276
<?php endif; ?>
268277

269-
<h2 id="apcu">APCu</h2>
278+
<h2 id="apcu"><?=$apcVersion?></h2>
270279
<div>
271-
<h3>Memory <?=human_size(apc_mem('used'))?> of <?=human_size(apc_mem('total'))?></h3>
280+
<h3>Memory <?=human_size(apcu_mem('used'))?> of <?=human_size(apcu_mem('total'))?></h3>
272281
<div class="full bar green">
273-
<div class="orange" style="width: <?=percentage(apc_mem('used'), apc_mem('total'))?>%"></div>
282+
<div class="orange" style="width: <?=percentage(apcu_mem('used'), apcu_mem('total'))?>%"></div>
274283
</div>
275284
</div>
276285
<div>
277286
<h3>Actions</h3>
278287
<form action="?" method="GET">
279288
<label>Cache:
280-
<button name="action" value="apc_restart">Restart</button>
289+
<button name="action" value="apcu_restart">Restart</button>
281290
</label>
282291
</form>
283292
<form action="?" method="GET">
284293
<label>Key(s):
285294
<input name="selector" type="text" value="" placeholder=".*" />
286295
</label>
287-
<button type="submit" name="action" value="apc_select">Select</button>
288-
<button type="submit" name="action" value="apc_delete">Delete</button>
289-
<label><input type="checkbox" name="apc_show_expired" <?=isset($_GET['apc_show_expired'])?'checked="checked"':''?> />Show expired</label>
296+
<button type="submit" name="action" value="apcu_select">Select</button>
297+
<button type="submit" name="action" value="apcu_delete">Delete</button>
298+
<label><input type="checkbox" name="apcu_show_expired" <?=isset($_GET['apcu_show_expired'])?'checked="checked"':''?> />Show expired</label>
290299
</form>
291300
</div>
292-
<?php if( isset( $_GET['action'] ) && $_GET['action'] == 'apc_view' ): ?>
301+
<?php if( isset( $_GET['action'] ) && $_GET['action'] == 'apcu_view' ): ?>
293302
<div>
294303
<h3>Value for <?=htmlentities('"'.$_GET['selector'].'"')?></h3>
295-
<pre><?php var_dump( apc_fetch(urldecode($_GET['selector'])) ); ?></pre>
304+
<pre><?php var_dump( apcu_fetch(urldecode($_GET['selector'])) ); ?></pre>
296305
</div>
297306
<?php endif; ?>
298-
<?php if( isset( $_GET['action'] ) && $_GET['action'] == 'apc_select' ): ?>
307+
<?php if( isset( $_GET['action'] ) && $_GET['action'] == 'apcu_select' ): ?>
299308
<div>
300309
<h3>Keys matching <?=htmlentities('"'.$_GET['selector'].'"')?></h3>
301310
<table>
302311
<thead>
303312
<tr>
304-
<th><a href="<?=sort_url(has_key(apc_ref(), 'key', 'info'))?>">Key</a></th>
313+
<th><a href="<?=sort_url(has_key(apcu_ref(), 'key', 'info'))?>">Key</a></th>
305314
<th><a href="<?=sort_url('nhits')?>">Hits</a></th>
306315
<th><a href="<?=sort_url('mem_size')?>">Size</a></th>
307316
<th><a href="<?=sort_url('ttl')?>">TTL</a></th>
@@ -314,7 +323,7 @@ function sort_list(&$list) {
314323

315324
<tbody>
316325
<?php foreach( sort_list($apc['cache']['cache_list']) as $item ):
317-
$expired = !isset( $_GET['apc_show_expired'] ) && $item['ttl'] > 0 && get_key($item, 'mtime', 'modification_time') + $item['ttl'] < time();
326+
$expired = !isset( $_GET['apcu_show_expired'] ) && $item['ttl'] > 0 && get_key($item, 'mtime', 'modification_time') + $item['ttl'] < time();
318327
if( !preg_match(get_selector(), get_key($item, 'key', 'info')) || $expired ) continue;?>
319328
<tr>
320329
<td><?=get_key($item, 'key', 'info')?></td>
@@ -323,8 +332,8 @@ function sort_list(&$list) {
323332
<td><?=$item['ttl']?></td>
324333
<td><?=date('Y-m-d H:i', get_key($item, 'mtime', 'modification_time') + $item['ttl'] )?></td>
325334
<td>
326-
<a href="?action=apc_delete&selector=<?=urlencode('^'.get_key($item, 'key', 'info').'$')?>">Delete</a>
327-
<a href="?action=apc_view&selector=<?=urlencode(get_key($item, 'key', 'info'))?>">View</a>
335+
<a href="?action=apcu_delete&selector=<?=urlencode('^'.get_key($item, 'key', 'info').'$')?>">Delete</a>
336+
<a href="?action=apcu_view&selector=<?=urlencode(get_key($item, 'key', 'info'))?>">View</a>
328337
</td>
329338
</tr>
330339
<?php endforeach; ?>
@@ -334,4 +343,4 @@ function sort_list(&$list) {
334343
<?php endif; ?>
335344
</div>
336345
</body>
337-
</html>
346+
</html>

0 commit comments

Comments
 (0)