-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCachingInfo.php
More file actions
116 lines (106 loc) · 4.32 KB
/
CachingInfo.php
File metadata and controls
116 lines (106 loc) · 4.32 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\ServerInfo;
use OCP\IConfig;
class CachingInfo {
public function __construct(
private IConfig $config,
) {
}
/**
* @return array{
* opcache: array{enabled: bool, hits: int, misses: int, hitRate: float, memoryUsedMB: float, memoryFreeMB: float, cachedScripts: int},
* apcu: array{enabled: bool, hits: int, misses: int, hitRate: float, memoryUsedMB: float, memoryFreeMB: float},
* redis: array{configured: bool, distributed: string, locking: string},
* memcache: array{local: string, distributed: string, locking: string}
* }
*/
public function getCachingInfo(): array {
return [
'opcache' => $this->opcacheInfo(),
'apcu' => $this->apcuInfo(),
'redis' => $this->redisInfo(),
'memcache' => [
'local' => $this->shortClassName($this->config->getSystemValue('memcache.local', '')),
'distributed' => $this->shortClassName($this->config->getSystemValue('memcache.distributed', '')),
'locking' => $this->shortClassName($this->config->getSystemValue('memcache.locking', '')),
],
];
}
/**
* @return array{enabled: bool, hits: int, misses: int, hitRate: float, memoryUsedMB: float, memoryFreeMB: float, cachedScripts: int}
*/
private function opcacheInfo(): array {
if (!extension_loaded('Zend OPcache') || !function_exists('opcache_get_status')) {
return ['enabled' => false, 'hits' => 0, 'misses' => 0, 'hitRate' => 0.0, 'memoryUsedMB' => 0.0, 'memoryFreeMB' => 0.0, 'cachedScripts' => 0];
}
$status = @opcache_get_status(false);
if (!is_array($status)) {
return ['enabled' => false, 'hits' => 0, 'misses' => 0, 'hitRate' => 0.0, 'memoryUsedMB' => 0.0, 'memoryFreeMB' => 0.0, 'cachedScripts' => 0];
}
$stats = $status['opcache_statistics'] ?? [];
$mem = $status['memory_usage'] ?? [];
$hits = (int)($stats['hits'] ?? 0);
$misses = (int)($stats['misses'] ?? 0);
$total = $hits + $misses;
return [
'enabled' => (bool)($status['opcache_enabled'] ?? false),
'hits' => $hits,
'misses' => $misses,
'hitRate' => $total > 0 ? ($hits / $total) * 100 : 0.0,
'memoryUsedMB' => isset($mem['used_memory']) ? round($mem['used_memory'] / (1024 * 1024), 2) : 0.0,
'memoryFreeMB' => isset($mem['free_memory']) ? round($mem['free_memory'] / (1024 * 1024), 2) : 0.0,
'cachedScripts' => (int)($stats['num_cached_scripts'] ?? 0),
];
}
/**
* @return array{enabled: bool, hits: int, misses: int, hitRate: float, memoryUsedMB: float, memoryFreeMB: float}
*/
private function apcuInfo(): array {
if (!extension_loaded('apcu') || !function_exists('apcu_cache_info')) {
return ['enabled' => false, 'hits' => 0, 'misses' => 0, 'hitRate' => 0.0, 'memoryUsedMB' => 0.0, 'memoryFreeMB' => 0.0];
}
$cache = @apcu_cache_info(true);
$sma = function_exists('apcu_sma_info') ? @apcu_sma_info(true) : false;
if (!is_array($cache)) {
return ['enabled' => false, 'hits' => 0, 'misses' => 0, 'hitRate' => 0.0, 'memoryUsedMB' => 0.0, 'memoryFreeMB' => 0.0];
}
$hits = (int)($cache['num_hits'] ?? 0);
$misses = (int)($cache['num_misses'] ?? 0);
$total = $hits + $misses;
$used = is_array($sma) && isset($sma['seg_size'], $sma['avail_mem']) ? (int)$sma['seg_size'] - (int)$sma['avail_mem'] : 0;
$free = is_array($sma) && isset($sma['avail_mem']) ? (int)$sma['avail_mem'] : 0;
return [
'enabled' => true,
'hits' => $hits,
'misses' => $misses,
'hitRate' => $total > 0 ? ($hits / $total) * 100 : 0.0,
'memoryUsedMB' => round($used / (1024 * 1024), 2),
'memoryFreeMB' => round($free / (1024 * 1024), 2),
];
}
/**
* @return array{configured: bool, distributed: string, locking: string}
*/
private function redisInfo(): array {
$distributed = (string)$this->config->getSystemValue('memcache.distributed', '');
$locking = (string)$this->config->getSystemValue('memcache.locking', '');
$usingRedis = stripos($distributed, 'Redis') !== false || stripos($locking, 'Redis') !== false;
return [
'configured' => $usingRedis,
'distributed' => $this->shortClassName($distributed),
'locking' => $this->shortClassName($locking),
];
}
private function shortClassName(string $cls): string {
if ($cls === '') {
return '';
}
$parts = explode('\\', $cls);
return end($parts) ?: $cls;
}
}