| 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 . '/' . $this->key_group_child; |
| 37 |
$this->has_thim_cache = $has_thim_cache; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Set cache |
| 42 |
* |
| 43 |
* @param string $key |
| 44 |
* @param mixed $data |
| 45 |
* @param int $expire |
| 46 |
* |
| 47 |
* @since 4.0.8 |
| 48 |
* @version 1.0.2 |
| 49 |
*/ |
| 50 |
public function set_cache( string $key, $data, int $expire = 0 ) { |
| 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 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get cache |
| 66 |
* |
| 67 |
* @param string $key |
| 68 |
* @return false|mixed |
| 69 |
*/ |
| 70 |
public function get_cache( string $key ) { |
| 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; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Set value for first load page on one process |
| 87 |
* Apply for query call same many times. |
| 88 |
* |
| 89 |
* @param string $type |
| 90 |
* @param string $key |
| 91 |
* @param $val mixed |
| 92 |
* |
| 93 |
* @author tungnx |
| 94 |
* @version 1.0.0 |
| 95 |
* @sicne 4.1.4.1 |
| 96 |
* @return false|mixed|string |
| 97 |
*/ |
| 98 |
public static function cache_load_first( string $type = 'get', string $key = '', $val = '' ) { |
| 99 |
static $first_set_value = array(); |
| 100 |
|
| 101 |
if ( 'get' === $type ) { |
| 102 |
if ( ! array_key_exists( $key, $first_set_value ) ) { |
| 103 |
return false; |
| 104 |
} else { |
| 105 |
return $first_set_value[ $key ]; |
| 106 |
} |
| 107 |
} elseif ( 'set' === $type ) { |
| 108 |
$first_set_value[ $key ] = $val; |
| 109 |
|
| 110 |
return $first_set_value[ $key ]; |
| 111 |
} elseif ( 'clear' === $type ) { |
| 112 |
unset( $first_set_value[ $key ] ); |
| 113 |
} |
| 114 |
|
| 115 |
return $first_set_value; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Clear cache by key |
| 120 |
* |
| 121 |
* @param $key |
| 122 |
*/ |
| 123 |
public function clear( $key ) { |
| 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 |
} |
| 137 |
} |
| 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 |
*/ |
| 208 |
public function clear_all() { |
| 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 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|