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