| 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 |
protected function __construct() { |
| 27 |
$this->key_group = $this->key_group_parent . $this->key_group_child; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Set cache |
| 32 |
* |
| 33 |
* @param string $key |
| 34 |
* @param mixed $data |
| 35 |
* @param int $expire |
| 36 |
*/ |
| 37 |
public function set_cache( string $key, $data, int $expire = 0 ) { |
| 38 |
wp_cache_set( $key, $data, $this->key_group, $expire ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get cache |
| 43 |
* |
| 44 |
* @param string $key |
| 45 |
* @return false|mixed |
| 46 |
*/ |
| 47 |
public function get_cache( string $key ) { |
| 48 |
return wp_cache_get( $key, $this->key_group ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set value for first load page on one process |
| 53 |
* Apply for query call same |
| 54 |
* |
| 55 |
* @param string $type |
| 56 |
* @param string $key |
| 57 |
* @param $val mixed |
| 58 |
* |
| 59 |
* @author tungnx |
| 60 |
* @version 1.0.0 |
| 61 |
* @sicne 4.1.4.1 |
| 62 |
* @return false|mixed|string |
| 63 |
*/ |
| 64 |
public static function cache_load_first( string $type = 'get', string $key = '', $val = '' ) { |
| 65 |
static $first_set_value = array(); |
| 66 |
|
| 67 |
if ( 'get' === $type ) { |
| 68 |
if ( ! array_key_exists( $key, $first_set_value ) ) { |
| 69 |
return false; |
| 70 |
} else { |
| 71 |
return $first_set_value[ $key ]; |
| 72 |
} |
| 73 |
} elseif ( 'set' === $type ) { |
| 74 |
$first_set_value[ $key ] = $val; |
| 75 |
|
| 76 |
return $first_set_value[ $key ]; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Clear cache by key |
| 82 |
* |
| 83 |
* @param $key |
| 84 |
*/ |
| 85 |
public function clear( $key ) { |
| 86 |
wp_cache_delete( $key, $this->key_group ); |
| 87 |
} |
| 88 |
|
| 89 |
public function clear_all() { |
| 90 |
wp_cache_flush(); |
| 91 |
} |
| 92 |
} |
| 93 |
|