| 1 |
<?php |
| 2 |
/** |
| 3 |
* A run-time cache management for tutor. |
| 4 |
* |
| 5 |
* @package Tutor\Cache |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 2.1.9 |
| 9 |
*/ |
| 10 |
namespace Tutor\Cache; |
| 11 |
|
| 12 |
/** |
| 13 |
* TutorCache class |
| 14 |
* |
| 15 |
* @since 2.1.9 |
| 16 |
*/ |
| 17 |
final class TutorCache { |
| 18 |
/** |
| 19 |
* Store instance once and provide it for entire lifecycle |
| 20 |
* |
| 21 |
* @since 2.1.9 |
| 22 |
* |
| 23 |
* @var self |
| 24 |
*/ |
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Hold all run-time cache data. |
| 29 |
* |
| 30 |
* @since 2.1.9 |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $data = array(); |
| 35 |
|
| 36 |
// Prevent to make instance |
| 37 |
private function __construct(){} |
| 38 |
// Prevent to clone instance |
| 39 |
private function __clone(){} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get the current class instance. |
| 43 |
* |
| 44 |
* @since 2.1.9 |
| 45 |
* @return self |
| 46 |
*/ |
| 47 |
public static function getInstance() { |
| 48 |
if ( is_null( self::$instance ) ) { |
| 49 |
self::$instance = new self(); |
| 50 |
} |
| 51 |
return self::$instance; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check valid cache key. |
| 56 |
* |
| 57 |
* @since 2.1.9 |
| 58 |
* |
| 59 |
* @param string $key cache key. |
| 60 |
* |
| 61 |
* @return boolean |
| 62 |
*/ |
| 63 |
public function is_valid_key( $key ) { |
| 64 |
if ( is_int( $key ) ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
if ( is_string( $key ) && trim( $key ) !== '' ) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get cache data by key. |
| 77 |
* |
| 78 |
* @since 2.1.9 |
| 79 |
* |
| 80 |
* @param string $key cache key. |
| 81 |
* @param mixed $default default value if key does not exit. |
| 82 |
* |
| 83 |
* @return mixed |
| 84 |
*/ |
| 85 |
public static function get( $key, $default = false ) { |
| 86 |
$instance = self::getInstance(); |
| 87 |
if ( ! $instance->is_valid_key( $key ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
if ( array_key_exists( $key, $instance->data ) ) { |
| 92 |
return $instance->data[ $key ]; |
| 93 |
} |
| 94 |
|
| 95 |
return $default; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Set cache data to a cache key. |
| 100 |
* |
| 101 |
* @since 2.1.9 |
| 102 |
* |
| 103 |
* @param string $key cache key. |
| 104 |
* @param mixed $value cache value. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public static function set( $key, $value ) { |
| 109 |
$instance = self::getInstance(); |
| 110 |
if ( ! $instance->is_valid_key( $key ) ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
$instance->data[ $key ] = $value; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get all cached data. |
| 119 |
* |
| 120 |
* @since 2.1.9 |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public static function get_all() { |
| 125 |
return self::getInstance()->data; |
| 126 |
} |
| 127 |
} |
| 128 |
|