PluginProbe
phpinfo() WP – Site Health, PHP Compatibility & Server Audit / 7.2.6
phpinfo() WP – Site Health, PHP Compatibility & Server Audit v7.2.6
7.2.7 7.2.6 7.2.5 7.2.4 7.2.3 7.2.0 7.2.1 7.2.2 7.1.0 7.0.3 7.0.4 7.0.5 trunk 6.0 7.0.0 7.0.1 7.0.2
phpinfo-wp / includes / class-object-cache.php

class-object-cache.php in phpinfo() WP – Site Health, PHP Compatibility & Server Audit 7.2.6, at includes/class-object-cache.php

139 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') or die('Unauthorized Access');
3
4 class Phpinfo_WP_Object_Cache {
5
6 private static function _pro(): bool { return Phpinfo_WP_License::is_valid(); }
7
8 public static function has_dropin(): bool {
9 return file_exists(WP_CONTENT_DIR . '/object-cache.php');
10 }
11
12 public static function is_redis_extension_loaded(): bool {
13 return extension_loaded('redis');
14 }
15
16 public static function is_memcached_extension_loaded(): bool {
17 return extension_loaded('memcached') || extension_loaded('memcache');
18 }
19
20 public static function active_type(): ?string {
21 if (!self::has_dropin()) return null;
22
23 $dropin = file_get_contents(WP_CONTENT_DIR . '/object-cache.php');
24 if (stripos($dropin, 'redis') !== false) {
25 return 'redis';
26 }
27 if (stripos($dropin, 'memcache') !== false) {
28 return 'memcached';
29 }
30 return 'unknown';
31 }
32
33 public static function status(): ?array {
34 $type = self::active_type();
35 if (!$type) return null;
36
37 $status = [
38 'type' => $type,
39 'hit_rate' => null,
40 'hits' => 0,
41 'misses' => 0,
42 'memory_used' => 0,
43 'memory_total' => 0,
44 'memory_pct' => 0,
45 'evictions' => 0,
46 'keys' => 0,
47 'connection' => 'unknown',
48 ];
49
50 global $wp_object_cache;
51
52 // Try to get stats from the drop-in object
53 if (is_object($wp_object_cache)) {
54 // Some drop-ins store connection info here
55 if ($type === 'redis') {
56 if (method_exists($wp_object_cache, 'redis_status')) {
57 // Try to infer from redis Object Cache dropin
58 if (isset($wp_object_cache->redis_client)) {
59 try {
60 $info = $wp_object_cache->redis_client->info();
61 if ($info) {
62 $status['memory_used'] = $info['used_memory'] ?? 0;
63 $status['memory_total'] = $info['maxmemory'] ?? 0;
64 $status['evictions'] = $info['evicted_keys'] ?? 0;
65 if (isset($info['db0'])) {
66 preg_match('/keys=(\d+)/', $info['db0'], $matches);
67 $status['keys'] = isset($matches[1]) ? (int)$matches[1] : 0;
68 }
69 $status['hits'] = $info['keyspace_hits'] ?? 0;
70 $status['misses'] = $info['keyspace_misses'] ?? 0;
71 }
72 } catch (Exception $e) {}
73 }
74 }
75
76 // Connection type
77 if (defined('WP_REDIS_SCHEME') && WP_REDIS_SCHEME === 'unix') {
78 $status['connection'] = 'UNIX Socket (Fast)';
79 } elseif (defined('WP_REDIS_HOST')) {
80 $status['connection'] = 'TCP ' . WP_REDIS_HOST . ':' . (defined('WP_REDIS_PORT') ? WP_REDIS_PORT : '6379');
81 }
82 } elseif ($type === 'memcached') {
83 if (method_exists($wp_object_cache, 'get_stats')) {
84 $stats = $wp_object_cache->get_stats();
85 if (!empty($stats) && is_array($stats)) {
86 $first = reset($stats); // usually keyed by server
87 if (is_array($first)) {
88 $status['hits'] = $first['get_hits'] ?? 0;
89 $status['misses'] = $first['get_misses'] ?? 0;
90 $status['memory_used'] = $first['bytes'] ?? 0;
91 $status['memory_total'] = $first['limit_maxbytes'] ?? 0;
92 $status['evictions'] = $first['evictions'] ?? 0;
93 $status['keys'] = $first['curr_items'] ?? 0;
94 }
95 }
96 }
97 $status['connection'] = 'TCP (Standard)';
98 }
99
100 // General WP Cache stats fallback
101 if ($status['hits'] === 0 && isset($wp_object_cache->cache_hits)) {
102 $status['hits'] = $wp_object_cache->cache_hits;
103 $status['misses'] = $wp_object_cache->cache_misses;
104 }
105 }
106
107 if (($status['hits'] + $status['misses']) > 0) {
108 $status['hit_rate'] = round($status['hits'] / ($status['hits'] + $status['misses']) * 100, 2);
109 }
110
111 if ($status['memory_total'] > 0) {
112 $status['memory_pct'] = round($status['memory_used'] / $status['memory_total'] * 100, 1);
113 }
114
115 return $status;
116 }
117
118 public static function flush(): bool {
119 if (!self::_pro()) return false;
120 if (function_exists('wp_cache_flush')) {
121 return wp_cache_flush();
122 }
123 return false;
124 }
125
126 public static function format_bytes(int $bytes): string {
127 if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB';
128 if ($bytes >= 1024) return round($bytes / 1024, 1) . ' KB';
129 return $bytes . ' B';
130 }
131
132 public static function hit_rate_class(float $rate): string {
133 if ($rate >= 90) return 'grade-a';
134 if ($rate >= 70) return 'grade-b';
135 if ($rate >= 50) return 'grade-c';
136 return 'grade-f';
137 }
138 }
139