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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / cache / TutorCache.php
tutor / cache Last commit date
AbstractCache.php 3 years ago FlashMessage.php 10 months ago QuizAttempts.php 11 months ago TutorCache.php 3 years ago
TutorCache.php
128 lines
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