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
learnpress / inc / cache / class-lp-cache.php

class-lp-cache.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9, at inc/cache/class-lp-cache.php

229 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Cache
5 *
6 * @author tungnx
7 * @since 4.0.8
8 * @version 1.0.2
9 */
10 defined( 'ABSPATH' ) || exit();
11
12 class LP_Cache {
13 /**
14 * @var string Key group parent
15 */
16 protected $key_group_parent = 'learn_press';
17 /**
18 * @var string Key group child(external)
19 */
20 protected $key_group_child = '';
21 /**
22 * @var string Add key group parent with key group child
23 */
24 protected $key_group = '';
25 /**
26 * @var string Add key group parent with key group child
27 */
28 protected $has_thim_cache = false;
29
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;
43 }
44
45 /**
46 * Set cache
47 *
48 * @param string $key
49 * @param mixed $data
50 * @param int $expire
51 *
52 * @since 4.0.8
53 * @version 1.0.2
54 */
55 public function set_cache( string $key, $data, int $expire = 0 ) {
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 }
73 }
74
75 /**
76 * Get cache
77 *
78 * @param string $key
79 * @return false|mixed
80 */
81 public function get_cache( string $key ) {
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;
95 }
96
97 /**
98 * Set value for first load page on one process
99 * Apply for query call same many times.
100 *
101 * @param string $type
102 * @param string $key
103 * @param $val mixed
104 *
105 * @author tungnx
106 * @version 1.0.0
107 * @sicne 4.1.4.1
108 * @return false|mixed|string
109 */
110 public static function cache_load_first( string $type = 'get', string $key = '', $val = '' ) {
111 static $first_set_value = array();
112
113 if ( 'get' === $type ) {
114 if ( ! array_key_exists( $key, $first_set_value ) ) {
115 return false;
116 } else {
117 return $first_set_value[ $key ];
118 }
119 } elseif ( 'set' === $type ) {
120 $first_set_value[ $key ] = $val;
121
122 return $first_set_value[ $key ];
123 } elseif ( 'clear' === $type ) {
124 unset( $first_set_value[ $key ] );
125 }
126
127 return $first_set_value;
128 }
129
130 /**
131 * Clear cache by key
132 *
133 * @param $key
134 */
135 public function clear( $key ) {
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 }
149 }
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 */
220 public function clear_all() {
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 }
227 }
228 }
229