PluginProbe
ezCache / 2.6.0
ezCache v2.6.0
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / Rest / RedisController.php

RedisController.php in ezCache 2.6.0, at includes/Rest/RedisController.php

78 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Redis REST Controller
4 *
5 * Handles GET /redis-status and POST /redis-flush endpoints.
6 *
7 * @package Upress\EzCache
8 */
9 namespace Upress\EzCache\Rest;
10
11 use Upress\EzCache\RedisObjectCache;
12 use Upress\EzCache\Settings;
13 use Upress\EzCache\PremiumFeatures;
14 use WP_REST_Request;
15
16 class RedisController {
17
18 /**
19 * GET /redis-status
20 * Returns Redis connection status, memory, hit rate, and key count.
21 */
22 public function status() {
23 $status = RedisObjectCache::get_status();
24 return wp_send_json_success( $status );
25 }
26
27 /**
28 * POST /redis-flush
29 * Flushes all ezcache:* keys from Redis.
30 */
31 public function flush() {
32 if ( ! PremiumFeatures::is_premium() ) {
33 return wp_send_json_error( [ 'message' => 'Premium required' ], 403 );
34 }
35
36 $result = RedisObjectCache::flush();
37 if ( $result ) {
38 return wp_send_json_success( [ 'message' => 'Redis cache flushed' ] );
39 }
40 return wp_send_json_error( [ 'message' => 'Failed to flush Redis — is it running?' ] );
41 }
42
43 /**
44 * POST /redis-toggle
45 * Enable or disable the Redis object cache (deploys / removes drop-in).
46 */
47 public function toggle( WP_REST_Request $request ) {
48 if ( ! PremiumFeatures::is_premium() ) {
49 return wp_send_json_error( [ 'message' => 'Premium required' ], 403 );
50 }
51
52 $params = (array) $request->get_json_params();
53 $enable = ! empty( $params['enable'] );
54 $fullpage = ! empty( $params['enable_fullpage'] );
55
56 Settings::set_settings( [
57 'enable_redis_object_cache' => $enable,
58 'enable_redis_fullpage' => $fullpage,
59 ] );
60
61 if ( $enable ) {
62 $deployed = RedisObjectCache::maybe_deploy_dropin();
63 if ( ! $deployed && ! RedisObjectCache::is_our_dropin() ) {
64 return wp_send_json_error( [
65 'message' => 'A foreign object-cache.php already exists. Remove it first.',
66 ] );
67 }
68 } else {
69 RedisObjectCache::remove_dropin();
70 }
71
72 return wp_send_json_success( [
73 'enabled' => $enable,
74 'status' => RedisObjectCache::get_status(),
75 ] );
76 }
77 }
78