| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
use OPTN\Includes\Traits\Singleton; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Cache class |
| 12 |
*/ |
| 13 |
class Cache { |
| 14 |
use Singleton; |
| 15 |
|
| 16 |
/** |
| 17 |
* Gets value from cache by key |
| 18 |
* |
| 19 |
* @param string $key key of the value. |
| 20 |
* @param mixed $default_value default value if value in not in cache. |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
public static function get( $key, $default_value = null ) { |
| 24 |
$res = get_transient( $key ); |
| 25 |
return $res ? $res : $default_value; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Deletes value from cache by kea |
| 30 |
* |
| 31 |
* @param string $key key of the value. |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public static function remove( $key ) { |
| 35 |
return delete_transient( $key ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets a value in cache |
| 40 |
* |
| 41 |
* @param string $key key of the value. |
| 42 |
* @param mixed $value value to store in cache. |
| 43 |
* @param int|null $days cache expiration in days. |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
public static function set( $key, $value, $days = null ) { |
| 47 |
$d = empty( $days ) ? OPTN_CACHE_DAY : $days; |
| 48 |
return set_transient( $key, $value, DAY_IN_SECONDS * $d ); |
| 49 |
} |
| 50 |
} |
| 51 |
|