PluginProbe
ezCache / 2.5
ezCache v2.5
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 / object-cache-dropin.php

object-cache-dropin.php in ezCache 2.5, at includes/object-cache-dropin.php

166 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ezCache Redis Object Cache Drop-in
4 *
5 * Automatically managed by the ezCache plugin. Do not edit manually.
6 *
7 * @package ezCache Redis Object Cache Drop-in
8 */
9 if ( ! defined( 'ABSPATH' ) ) { exit; }
10
11 class WP_Object_Cache {
12 private $redis = null;
13 private $connected = false;
14 private $cache = [];
15 private $non_persistent_groups = [];
16 public $cache_hits = 0;
17 public $cache_misses = 0;
18 private $prefix = '';
19
20 public function __construct() {
21 $this->prefix = (defined('DB_NAME') ? DB_NAME : 'wp') . ':' . ($GLOBALS['table_prefix'] ?? 'wp_') . ':';
22 $this->connect();
23 }
24
25 private function connect() {
26 if ( class_exists( 'Redis' ) ) {
27 try {
28 $this->redis = new Redis();
29 $this->redis->connect( '127.0.0.1', 6379, 1 );
30 $this->connected = true;
31 } catch ( Exception $e ) {
32 $this->connected = false;
33 }
34 }
35 }
36
37 private function build_key( $key, $group ) {
38 return 'ezcache:obj:' . $this->prefix . ( $group ?: 'default' ) . ':' . $key;
39 }
40
41 public function add( $key, $data, $group = 'default', $expire = 0 ) {
42 if ( $this->get( $key, $group ) !== false ) { return false; }
43 return $this->set( $key, $data, $group, $expire );
44 }
45
46 public function add_global_groups( $groups ) {}
47 public function add_non_persistent_groups( $groups ) {
48 $this->non_persistent_groups = array_merge( $this->non_persistent_groups, (array) $groups );
49 }
50
51 public function decr( $key, $offset = 1, $group = 'default' ) {
52 $val = (int) $this->get( $key, $group ) - $offset;
53 $this->set( $key, $val, $group );
54 return $val;
55 }
56
57 public function delete( $key, $group = 'default' ) {
58 $rkey = $this->build_key( $key, $group );
59 unset( $this->cache[ $rkey ] );
60 if ( $this->connected ) {
61 try { $this->redis->del( [ $rkey ] ); } catch ( Exception $e ) {}
62 }
63 return true;
64 }
65
66 public function flush() {
67 $this->cache = [];
68 if ( $this->connected ) {
69 try {
70 $keys = $this->redis->keys( 'ezcache:obj:' . $this->prefix . '*' );
71 if ( $keys ) { $this->redis->del( $keys ); }
72 } catch ( Exception $e ) {}
73 }
74 return true;
75 }
76
77 public function get( $key, $group = 'default', $force = false, &$found = null ) {
78 $rkey = $this->build_key( $key, $group );
79 if ( ! $force && array_key_exists( $rkey, $this->cache ) ) {
80 $found = true; $this->cache_hits++;
81 return is_object( $this->cache[ $rkey ] ) ? clone $this->cache[ $rkey ] : $this->cache[ $rkey ];
82 }
83 if ( in_array( $group, $this->non_persistent_groups, true ) || ! $this->connected ) {
84 $found = false; $this->cache_misses++; return false;
85 }
86 try {
87 $raw = $this->redis->get( $rkey );
88 if ( $raw === false ) { $found = false; $this->cache_misses++; return false; }
89 $data = maybe_unserialize( $raw );
90 $this->cache[ $rkey ] = $data;
91 $found = true; $this->cache_hits++;
92 return is_object( $data ) ? clone $data : $data;
93 } catch ( Exception $e ) { $found = false; $this->cache_misses++; return false; }
94 }
95
96 public function get_multiple( $keys, $group = 'default', $force = false ) {
97 $result = [];
98 foreach ( $keys as $key ) { $result[ $key ] = $this->get( $key, $group, $force ); }
99 return $result;
100 }
101
102 public function incr( $key, $offset = 1, $group = 'default' ) {
103 $val = (int) $this->get( $key, $group ) + $offset;
104 $this->set( $key, $val, $group );
105 return $val;
106 }
107
108 public function replace( $key, $data, $group = 'default', $expire = 0 ) {
109 if ( $this->get( $key, $group ) === false ) { return false; }
110 return $this->set( $key, $data, $group, $expire );
111 }
112
113 public function reset() { $this->cache = []; }
114
115 public function set( $key, $data, $group = 'default', $expire = 0 ) {
116 $rkey = $this->build_key( $key, $group );
117 $value = is_object( $data ) ? clone $data : $data;
118 $this->cache[ $rkey ] = $value;
119 if ( in_array( $group, $this->non_persistent_groups, true ) || ! $this->connected ) { return true; }
120 try {
121 $s = maybe_serialize( $value );
122 if ( $expire > 0 ) { $this->redis->setex( $rkey, $expire, $s ); }
123 else { $this->redis->set( $rkey, $s ); }
124 } catch ( Exception $e ) {}
125 return true;
126 }
127
128 public function set_multiple( $data, $group = 'default', $expire = 0 ) {
129 $result = [];
130 foreach ( $data as $k => $v ) { $result[ $k ] = $this->set( $k, $v, $group, $expire ); }
131 return $result;
132 }
133
134 public function stats() {
135 echo '<p><strong>Cache Hits:</strong> ' . $this->cache_hits . '<br />';
136 echo '<strong>Cache Misses:</strong> ' . $this->cache_misses . '</p>';
137 }
138
139 public function switch_to_blog( $blog_id ) {}
140 public function delete_multiple( $keys, $group = 'default' ) {
141 $result = [];
142 foreach ( $keys as $key ) { $result[ $key ] = $this->delete( $key, $group ); }
143 return $result;
144 }
145 }
146
147 function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->add( $key, $data, $group, $expire ); }
148 function wp_cache_add_global_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_global_groups( $groups ); }
149 function wp_cache_add_non_persistent_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_non_persistent_groups( $groups ); }
150 function wp_cache_close() { return true; }
151 function wp_cache_decr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->decr( $key, $offset, $group ); }
152 function wp_cache_delete( $key, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete( $key, $group ); }
153 function wp_cache_delete_multiple( $keys, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete_multiple( $keys, $group ); }
154 function wp_cache_flush() { global $wp_object_cache; return $wp_object_cache->flush(); }
155 function wp_cache_flush_runtime() { global $wp_object_cache; $wp_object_cache->reset(); return true; }
156 function wp_cache_flush_group( $group ) { return true; }
157 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { global $wp_object_cache; return $wp_object_cache->get( $key, $group, $force, $found ); }
158 function wp_cache_get_multiple( $keys, $group = '', $force = false ) { global $wp_object_cache; return $wp_object_cache->get_multiple( $keys, $group, $force ); }
159 function wp_cache_incr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->incr( $key, $offset, $group ); }
160 function wp_cache_init() { global $wp_object_cache; $wp_object_cache = new WP_Object_Cache(); }
161 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->replace( $key, $data, $group, $expire ); }
162 function wp_cache_reset() { global $wp_object_cache; $wp_object_cache->reset(); }
163 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set( $key, $data, $group, $expire ); }
164 function wp_cache_set_multiple( $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set_multiple( $data, $group, $expire ); }
165 function wp_cache_switch_to_blog( $blog_id ) { global $wp_object_cache; $wp_object_cache->switch_to_blog( $blog_id ); }
166