PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.2
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.2
1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / modules / ObjectCache / ObjectCacheModule.php

ObjectCacheModule.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.0.2, at includes/modules/ObjectCache/ObjectCacheModule.php

201 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Object Cache module — status, settings, wp-config snippet, flush.
4 *
5 * Tier: Free per FEATURES.md "Object Cache" §1-11 (LiteSpeed parity).
6 *
7 * We don't ship our own object-cache.php drop-in in this Free release
8 * (see Object_Cache class docblock for rationale). The settings here
9 * are surfaced via the wp-config snippet; advanced consumers (Pro,
10 * external drop-ins like Redis Object Cache) can read them too.
11 *
12 * @package XSpeed
13 */
14
15 declare(strict_types=1);
16
17 namespace XSpeed\Modules\ObjectCache;
18
19 use XSpeed\Module;
20 use XSpeed\Object_Cache;
21
22 final class ObjectCacheModule extends Module {
23
24 public const SLUG = 'object-cache';
25 public const TIER = self::TIER_FREE;
26 public const VERSION = '1.0.0';
27
28 public function ui_metadata(): array {
29 return array(
30 'label' => 'Object Cache',
31 'icon' => 'Server',
32 'description' => 'Configure a persistent object cache (Redis / Memcached) and generate a paste-ready wp-config.php snippet.',
33 'custom_panel' => 'ObjectCachePanel',
34 );
35 }
36
37 public function settings_schema(): array {
38 return array(
39 'backend' => array(
40 'type' => 'enum',
41 'default' => 'redis',
42 'options' => array( 'redis', 'memcached' ),
43 'option_labels' => array(
44 'redis' => 'Redis',
45 'memcached' => 'Memcached',
46 ),
47 'label' => 'Backend',
48 'description' => 'Which cache server you intend to use. Affects the generated wp-config snippet.',
49 ),
50 'redis_host' => array(
51 'type' => 'string',
52 'default' => '127.0.0.1',
53 'label' => 'Redis Host',
54 'description' => 'Hostname or IP of the Redis server. Use 127.0.0.1 for a local socket on the same machine as PHP.',
55 ),
56 'redis_port' => array(
57 'type' => 'int',
58 'default' => 6379,
59 'min' => 1,
60 'max' => 65535,
61 'label' => 'Redis Port',
62 'description' => 'Default Redis port is 6379.',
63 ),
64 'redis_password' => array(
65 'type' => 'string',
66 'default' => '',
67 'label' => 'Redis Password',
68 'description' => 'Leave blank if your Redis server runs without auth.',
69 ),
70 'redis_database' => array(
71 'type' => 'int',
72 'default' => 0,
73 'min' => 0,
74 'max' => 15,
75 'label' => 'Redis Database',
76 'description' => 'Redis logical DB number (0-15). Use a dedicated DB per site if Redis is shared.',
77 ),
78 'memcached_host' => array(
79 'type' => 'string',
80 'default' => '127.0.0.1',
81 'label' => 'Memcached Host',
82 'description' => 'Used when Backend = Memcached.',
83 ),
84 'memcached_port' => array(
85 'type' => 'int',
86 'default' => 11211,
87 'min' => 1,
88 'max' => 65535,
89 'label' => 'Memcached Port',
90 'description' => 'Default Memcached port is 11211.',
91 ),
92 'key_prefix' => array(
93 'type' => 'string',
94 'default' => '',
95 'label' => 'Cache Key Prefix',
96 'description' => 'Unique salt for this site\'s cache keys. Critical when multiple WP sites share one Redis/Memcached server.',
97 ),
98 'connection_timeout' => array(
99 'type' => 'int',
100 'default' => 1,
101 'min' => 0,
102 'max' => 60,
103 'label' => 'Connection Timeout (seconds)',
104 'description' => 'How long to wait for a connection. Keep low (1-2s) so a misconfigured cache never stalls the page.',
105 ),
106 'persistent' => array(
107 'type' => 'bool',
108 'default' => true,
109 'label' => 'Persistent Connections',
110 'description' => 'Reuse the connection across PHP requests when supported. Generally a win unless the cache server complains about idle connections.',
111 ),
112 );
113 }
114
115 public function rest_routes(): array {
116 $default = parent::rest_routes();
117 return array_merge(
118 $default,
119 array(
120 array(
121 'path' => '/detect',
122 'methods' => 'GET',
123 'callback' => array( $this, 'rest_detect' ),
124 ),
125 array(
126 'path' => '/flush',
127 'methods' => 'POST',
128 'callback' => array( $this, 'rest_flush' ),
129 ),
130 array(
131 'path' => '/snippet',
132 'methods' => 'GET',
133 'callback' => array( $this, 'rest_snippet' ),
134 ),
135 )
136 );
137 }
138
139 public function rest_detect( \WP_REST_Request $request ) {
140 return rest_ensure_response( Object_Cache::detect() );
141 }
142
143 public function rest_flush( \WP_REST_Request $request ) {
144 $ok = Object_Cache::flush();
145 if ( $ok && class_exists( '\\XSpeed\\Activity_Log' ) ) {
146 \XSpeed\Activity_Log::record(
147 'object_cache_flushed',
148 'Object cache flushed.',
149 \XSpeed\Activity_Log::INFO
150 );
151 }
152 return rest_ensure_response( array( 'ok' => $ok ) );
153 }
154
155 public function rest_snippet( \WP_REST_Request $request ) {
156 return rest_ensure_response(
157 array( 'snippet' => Object_Cache::render_config_snippet( $this->get_settings() ) )
158 );
159 }
160
161 public function cli_commands(): array {
162 return array(
163 array(
164 'name' => 'xspeed objcache',
165 'callback' => array( $this, 'cli_handler' ),
166 'shortdesc' => 'Show object cache status, flush, or print the wp-config snippet.',
167 'synopsis' => array(
168 array(
169 'type' => 'positional',
170 'name' => 'action',
171 'options' => array( 'status', 'flush', 'snippet' ),
172 'optional' => true,
173 ),
174 ),
175 ),
176 );
177 }
178
179 public function cli_handler( array $args, array $assoc ): void {
180 $action = $args[0] ?? 'status';
181 switch ( $action ) {
182 case 'status':
183 $d = Object_Cache::detect();
184 \WP_CLI::log( 'drop-in installed: ' . ( $d['drop_in_installed'] ? 'yes' : 'no' ) );
185 \WP_CLI::log( 'label: ' . $d['drop_in_label'] );
186 \WP_CLI::log( 'backend: ' . $d['backend'] );
187 \WP_CLI::log( 'ext object cache: ' . ( $d['wp_cache_active'] ? 'yes' : 'no' ) );
188 return;
189 case 'flush':
190 $ok = Object_Cache::flush();
191 $ok ? \WP_CLI::success( 'Flushed.' ) : \WP_CLI::error( 'Flush failed.' );
192 return;
193 case 'snippet':
194 \WP_CLI::log( Object_Cache::render_config_snippet( $this->get_settings() ) );
195 return;
196 default:
197 \WP_CLI::error( "Unknown action: $action" );
198 }
199 }
200 }
201