PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.2.0 All 139 releases
← All changes | inc/cache/class-lp-cache.php +132 -8 4.1.7.24.4.8 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,21 @@
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 . '/' . $this->key_group_child;
37 + $this->has_thim_cache = $has_thim_cache;
28 38 }
29 39
30 40 /**
31 41 * Set cache
@@ -32,11 +42,24 @@
32 42 *
33 43 * @param string $key
34 44 * @param mixed $data
35 45 * @param int $expire
46 + *
47 + * @since 4.0.8
48 + * @version 1.0.2
36 49 */
37 50 public function set_cache( string $key, $data, int $expire = 0 ) {
38 - wp_cache_set( $key, $data, $this->key_group, $expire );
51 + try {
52 + // Cache WP
53 + wp_cache_set( $key, $data, $this->key_group, $expire );
54 + // Cache thim_cache
55 + if ( $this->can_handle_with_thim_cache() ) {
56 + $key = "{$this->key_group}/{$key}";
57 + Thim_Cache_DB::instance()->set_value( $key, $data, $expire );
58 + }
59 + } catch ( Throwable $e ) {
60 + LP_Debug::error_log( $e );
61 + }
39 62 }
40 63
41 64 /**
42 65 * Get cache
@@ -44,14 +67,25 @@
44 67 * @param string $key
45 68 * @return false|mixed
46 69 */
47 70 public function get_cache( string $key ) {
48 - return wp_cache_get( $key, $this->key_group );
71 + // Get WP Cache
72 + $cache = wp_cache_get( $key, $this->key_group );
73 + // Get thim_cache
74 + if ( false === $cache && $this->can_handle_with_thim_cache() ) {
75 + $key = "{$this->key_group}/{$key}";
76 + $cache = Thim_Cache_DB::instance()->get_value( $key );
77 + /*if ( is_string( $cache ) ) {
78 + $cache = wp_unslash( $cache );
79 + }*/
80 + }
81 +
82 + return $cache;
49 83 }
50 84
51 85 /**
52 86 * Set value for first load page on one process
53 - * Apply for query call same
87 + * Apply for query call same many times.
54 88 *
55 89 * @param string $type
56 90 * @param string $key
57 91 * @param $val mixed
@@ -73,9 +107,13 @@
73 107 } elseif ( 'set' === $type ) {
74 108 $first_set_value[ $key ] = $val;
75 109
76 110 return $first_set_value[ $key ];
111 + } elseif ( 'clear' === $type ) {
112 + unset( $first_set_value[ $key ] );
77 113 }
114 +
115 + return $first_set_value;
78 116 }
79 117
80 118 /**
81 119 * Clear cache by key
@@ -82,11 +120,97 @@
82 120 *
83 121 * @param $key
84 122 */
85 123 public function clear( $key ) {
86 - wp_cache_delete( $key, $this->key_group );
124 + try {
125 + if ( empty( $key ) ) {
126 + return;
127 + }
128 +
129 + wp_cache_delete( $key, $this->key_group );
130 + if ( $this->can_handle_with_thim_cache() ) {
131 + $key = "{$this->key_group}/{$key}";
132 + Thim_Cache_DB::instance()->remove_cache( $key );
133 + }
134 + } catch ( Throwable $e ) {
135 + error_log( $e->getMessage() );
136 + }
87 137 }
88 138
139 + /**
140 + * Store list keys cache to a group.
141 + *
142 + * @param string $key_group
143 + * @param string $key_cache_new
144 + * @since 4.2.5.4
145 + * @version 1.0.0
146 + */
147 + public function save_cache_keys( string $key_group, string $key_cache_new ) {
148 + try {
149 + $keys = $this->get_cache( $key_group );
150 + if ( false === $keys ) {
151 + $keys_cache = array();
152 + } else {
153 + $keys_cache = LP_Helper::json_decode( $keys, true );
154 + }
155 +
156 + $keys_cache[] = $key_cache_new;
157 + $this->set_cache( $key_group, json_encode( $keys_cache ) );
158 + } catch ( Throwable $e ) {
159 + error_log( __METHOD__ . ': ' . $e->getMessage() );
160 + }
161 + }
162 +
163 + /**
164 + * Clear cache on group store list keys.
165 + *
166 + * @param string $key_group
167 + * @since 4.2.5.4
168 + * @version 1.0.0
169 + */
170 + public function clear_cache_on_group( string $key_group ) {
171 + try {
172 + $keys_cache_of_group_str = $this->get_cache( $key_group );
173 + if ( false === $keys_cache_of_group_str ) {
174 + return;
175 + }
176 + $keys_cache_of_group = LP_Helper::json_decode( $keys_cache_of_group_str, true );
177 + if ( ! empty( $keys_cache_of_group ) ) {
178 + foreach ( $keys_cache_of_group as $key ) {
179 + // Clear cache by key
180 + $this->clear( $key );
181 + }
182 + // Clear cache store list keys
183 + $this->clear( $key_group );
184 + }
185 + } catch ( Throwable $e ) {
186 + error_log( __METHOD__ . ': ' . $e->getMessage() );
187 + }
188 + }
189 +
190 + /**
191 + * Check can handle with thim cache
192 + *
193 + * @since 4.2.5.4
194 + * @version 1.0.0
195 + * @return bool
196 + */
197 + public function can_handle_with_thim_cache(): bool {
198 + return $this->has_thim_cache && LP_Settings::is_created_tb_thim_cache();
199 + }
200 +
201 + /**
202 + * Clear all cache
203 + *
204 + * @since 4.0.8
205 + * @version 1.0.1
206 + * @return void
207 + */
89 208 public function clear_all() {
90 - wp_cache_flush();
209 + try {
210 + wp_cache_flush();
211 + Thim_Cache_DB::instance()->remove_all_cache();
212 + } catch ( Throwable $e ) {
213 + error_log( __METHOD__ . ': ' . $e->getMessage() );
214 + }
91 215 }
92 216 }