-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAcceleratorCacheClearer.php
103 lines (85 loc) · 2.69 KB
/
AcceleratorCacheClearer.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
<?php
namespace SmartCore\Bundle\AcceleratorCacheBundle;
/**
* @author Kevin Bond <[email protected]>
*/
class AcceleratorCacheClearer
{
/**
* @param bool $user
* @param bool $opcode
*
* @return array
*/
public static function clearCache($user = true, $opcode = true)
{
if (!$user && !$opcode) {
throw new \InvalidArgumentException('No caches to clear.');
}
$messages = array('Clear PHP Accelerator Cache...');
$success = true;
if ($user) {
try {
$messages[] = self::clearUserCache();
} catch (\RuntimeException $e) {
$success = false;
$messages[] = $e->getMessage();
}
}
if ($opcode) {
try {
$messages[] = self::clearOpcodeCache();
} catch (\RuntimeException $e) {
$success = false;
$messages[] = $e->getMessage();
}
}
return array('success' => $success, 'message' => implode(' ', $messages));
}
/**
* @return string
*/
private static function clearUserCache()
{
if (function_exists('wincache_ucache_clear') && wincache_ucache_clear()) {
return 'Wincache User Cache: success.';
}
if (function_exists('apcu_clear_cache') && apcu_clear_cache()) {
return 'APC User Cache: success.';
}
if (function_exists('apc_clear_cache') && function_exists('opcache_reset') && apc_clear_cache()) {
return 'APC User Cache: success.';
}
if (function_exists('apc_clear_cache') && apc_clear_cache('user')) {
return 'APC User Cache: success.';
}
if (function_exists('xcache_clear_cache')) {
$cnt = xcache_count(XC_TYPE_VAR);
for ($i=0; $i < $cnt; $i++) {
xcache_clear_cache(XC_TYPE_VAR, $i);
}
return 'XCache User Cache: success.';
}
return 'User Cache: failure.';
}
/**
* @return string
*/
private static function clearOpcodeCache()
{
if (function_exists('opcache_reset') && opcache_reset()) {
return 'Zend OPcache: success.';
}
if (function_exists('apc_clear_cache') && apc_clear_cache('opcode')) {
return 'APC Opcode Cache: success.';
}
if (function_exists('xcache_clear_cache')) {
$cnt = xcache_count(XC_TYPE_PHP);
for ($i=0; $i < $cnt; $i++) {
xcache_clear_cache(XC_TYPE_PHP, $i);
}
return 'XCache Opcode Cache: success.';
}
return 'Opcode Cache: failure.';
}
}