PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | inc/cache/class-lp-cache.php +144 -8 4.1.6.9 → 4.4.9 View file →
@@ -12,9 +12,9 @@
12 12 class LP_Cache {
13 13 /**
14 14 * @var string Key group parent
15 15 */
16 - protected $key_group_parent = 'learn_press/';
16 + protected $key_group_parent = 'learn_press';
17 17 /**
18 18 * @var string Key group child(external)
19 19 */
20 20 protected $key_group_child = '';
@@ -21,11 +21,26 @@
21 21 /**
22 22 * @var string Add key group parent with key group child
23 23 */
24 24 protected $key_group = '';
25 + /**
26 + * @var string Add key group parent with key group child
27 + */
28 + protected $has_thim_cache = false;
25 29
26 - protected function __construct() {
27 - $this->key_group = $this->key_group_parent . $this->key_group_child;
30 + /**
31 + * If set $has_thim_cache = true, will use thim cache
32 + * Set/Update will check key from table thim_cache
33 + * else only WP Cache
34 + */
35 + public function __construct( $has_thim_cache = false ) {
36 + $this->key_group = $this->key_group_parent;
37 +
38 + if ( ! empty( $this->key_group_child ) ) {
39 + $this->key_group .= '/' . $this->key_group_child;
40 + }
41 +
42 + $this->has_thim_cache = $has_thim_cache;
28 43 }
29 44
30 45 /**
31 46 * Set cache
@@ -32,11 +47,30 @@
32 47 *
33 48 * @param string $key
34 49 * @param mixed $data
35 50 * @param int $expire
51 + *
52 + * @since 4.0.8
53 + * @version 1.0.2
36 54 */
37 55 public function set_cache( string $key, $data, int $expire = 0 ) {
38 - wp_cache_set( $key, $data, $this->key_group, $expire );
56 + try {
57 + // Cache WP
58 + wp_cache_set( $key, $data, $this->key_group, $expire );
59 + // Cache thim_cache
60 + if ( $this->can_handle_with_thim_cache() ) {
61 + $key = "{$this->key_group}/{$key}";
62 +
63 + if ( ! is_string( $data ) ) {
64 + $data = wp_json_encode( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
65 + }
66 +
67 + $expiration = $expire > 0 ? time() + $expire : 0;
68 + Thim_Cache_DB::instance()->set_value( $key, $data, $expiration );
69 + }
70 + } catch ( Throwable $e ) {
71 + LP_Debug::error_log( $e );
72 + }
39 73 }
40 74
41 75 /**
42 76 * Get cache
@@ -44,14 +78,26 @@
44 78 * @param string $key
45 79 * @return false|mixed
46 80 */
47 81 public function get_cache( string $key ) {
48 - return wp_cache_get( $key, $this->key_group );
82 + // Get WP Cache
83 + $cache = wp_cache_get( $key, $this->key_group );
84 + // Get thim_cache
85 + if ( false === $cache && $this->can_handle_with_thim_cache() ) {
86 + $key = "{$this->key_group}/{$key}";
87 + $cache = Thim_Cache_DB::instance()->get_value( $key );
88 + try {
89 + $cache = LP_Helper::json_decode( $cache, true );
90 + } catch ( Throwable $e ) {
91 + }
92 + }
93 +
94 + return $cache;
49 95 }
50 96
51 97 /**
52 98 * Set value for first load page on one process
53 - * Apply for query call same
99 + * Apply for query call same many times.
54 100 *
55 101 * @param string $type
56 102 * @param string $key
57 103 * @param $val mixed
@@ -73,9 +119,13 @@
73 119 } elseif ( 'set' === $type ) {
74 120 $first_set_value[ $key ] = $val;
75 121
76 122 return $first_set_value[ $key ];
123 + } elseif ( 'clear' === $type ) {
124 + unset( $first_set_value[ $key ] );
77 125 }
126 +
127 + return $first_set_value;
78 128 }
79 129
80 130 /**
81 131 * Clear cache by key
@@ -82,11 +132,97 @@
82 132 *
83 133 * @param $key
84 134 */
85 135 public function clear( $key ) {
86 - wp_cache_delete( $key, $this->key_group );
136 + try {
137 + if ( empty( $key ) ) {
138 + return;
139 + }
140 +
141 + wp_cache_delete( $key, $this->key_group );
142 + if ( $this->can_handle_with_thim_cache() ) {
143 + $key = "{$this->key_group}/{$key}";
144 + Thim_Cache_DB::instance()->remove_cache( $key );
145 + }
146 + } catch ( Throwable $e ) {
147 + error_log( $e->getMessage() );
148 + }
87 149 }
88 150
151 + /**
152 + * Store list keys cache to a group.
153 + *
154 + * @param string $key_group
155 + * @param string $key_cache_new
156 + * @since 4.2.5.4
157 + * @version 1.0.0
158 + */
159 + public function save_cache_keys( string $key_group, string $key_cache_new ) {
160 + try {
161 + $keys = $this->get_cache( $key_group );
162 + if ( false === $keys ) {
163 + $keys_cache = array();
164 + } else {
165 + $keys_cache = LP_Helper::json_decode( $keys, true );
166 + }
167 +
168 + $keys_cache[] = $key_cache_new;
169 + $this->set_cache( $key_group, json_encode( $keys_cache ) );
170 + } catch ( Throwable $e ) {
171 + error_log( __METHOD__ . ': ' . $e->getMessage() );
172 + }
173 + }
174 +
175 + /**
176 + * Clear cache on group store list keys.
177 + *
178 + * @param string $key_group
179 + * @since 4.2.5.4
180 + * @version 1.0.0
181 + */
182 + public function clear_cache_on_group( string $key_group ) {
183 + try {
184 + $keys_cache_of_group_str = $this->get_cache( $key_group );
185 + if ( false === $keys_cache_of_group_str ) {
186 + return;
187 + }
188 + $keys_cache_of_group = LP_Helper::json_decode( $keys_cache_of_group_str, true );
189 + if ( ! empty( $keys_cache_of_group ) ) {
190 + foreach ( $keys_cache_of_group as $key ) {
191 + // Clear cache by key
192 + $this->clear( $key );
193 + }
194 + // Clear cache store list keys
195 + $this->clear( $key_group );
196 + }
197 + } catch ( Throwable $e ) {
198 + error_log( __METHOD__ . ': ' . $e->getMessage() );
199 + }
200 + }
201 +
202 + /**
203 + * Check can handle with thim cache
204 + *
205 + * @since 4.2.5.4
206 + * @version 1.0.0
207 + * @return bool
208 + */
209 + public function can_handle_with_thim_cache(): bool {
210 + return $this->has_thim_cache && LP_Settings::is_created_tb_thim_cache();
211 + }
212 +
213 + /**
214 + * Clear all cache
215 + *
216 + * @since 4.0.8
217 + * @version 1.0.1
218 + * @return void
219 + */
89 220 public function clear_all() {
90 - wp_cache_flush();
221 + try {
222 + wp_cache_flush();
223 + Thim_Cache_DB::instance()->remove_all_cache();
224 + } catch ( Throwable $e ) {
225 + error_log( __METHOD__ . ': ' . $e->getMessage() );
226 + }
91 227 }
92 228 }