15
15
*/
16
16
class Cache {
17
17
/**
18
- *
18
+ *
19
19
* @var Storage
20
20
*/
21
21
private $ driver ;
22
22
private static $ inst ;
23
23
/**
24
- * Creates and returns a single instance of the class .
25
- *
26
- * @return Cache
24
+ * Removes an item from the cache given its unique identifier .
25
+ *
26
+ * @param string $key
27
27
*/
28
- private static function getInst () : Cache {
29
- if (self ::$ inst === null ) {
30
- self ::$ inst = new Cache ();
31
- self ::setDriver (new FileStorage ());
32
- }
33
- return self ::$ inst ;
28
+ public static function delete (string $ key ) {
29
+ self ::getDriver ()->delete ($ key );
34
30
}
35
31
/**
36
- * Sets storage engine which is used to store, read, update and delete items
37
- * from the cache.
38
- *
39
- * @param Storage $driver
32
+ * Removes all items from the cache.
40
33
*/
41
- public static function setDriver (Storage $ driver ) {
42
- self ::getInst ()->driver = $ driver ;
34
+ public static function flush () {
35
+ self ::getDriver ()->flush ();
36
+ }
37
+ /**
38
+ * Returns or creates a cache item given its key.
39
+ *
40
+ *
41
+ * @param string $key The unique identifier of the item.
42
+ *
43
+ * @param callable $generator A callback which is used as a fallback to
44
+ * create new cache entry or re-create an existing one if it was expired.
45
+ * This callback must return the data that will be cached.
46
+ *
47
+ * @param int $ttl Time to live of the item in seconds.
48
+ *
49
+ * @param array $params Any additional parameters to be passed to the callback
50
+ * which is used to generate cache data.
51
+ * @return null
52
+ */
53
+ public static function get (string $ key , callable $ generator = null , int $ ttl = 60 , array $ params = []) {
54
+ $ data = self ::getDriver ()->read ($ key );
55
+
56
+ if ($ data !== null && $ data !== false ) {
57
+ return $ data ;
58
+ }
59
+
60
+ if (!is_callable ($ generator )) {
61
+ return null ;
62
+ }
63
+ $ newData = call_user_func_array ($ generator , $ params );
64
+ $ item = new Item ($ key , $ newData , $ ttl , defined ('CACHE_SECRET ' ) ? CACHE_SECRET : '' );
65
+ self ::getDriver ()->cache ($ item );
66
+
67
+ return $ newData ;
43
68
}
44
69
/**
45
70
* Returns storage engine which is used to store, read, update and delete items
46
71
* from the cache.
47
- *
72
+ *
48
73
* @return Storage
49
74
*/
50
75
public static function getDriver () : Storage {
51
76
return self ::getInst ()->driver ;
52
77
}
53
78
/**
54
- * Removes an item from the cache given its unique identifier.
55
- *
56
- * @param string $key
79
+ * Reads an item from the cache and return its information.
80
+ *
81
+ * @param string $key The unique identifier of the item.
82
+ *
83
+ * @return Item|null If such item exist and not yet expired, an object
84
+ * of type 'Item' is returned which has all cached item information. Other
85
+ * than that, null is returned.
57
86
*/
58
- public static function delete (string $ key ) {
59
- self ::getDriver ()->delete ($ key );
87
+ public static function getItem (string $ key ) {
88
+ return self ::getDriver ()->readItem ($ key );
60
89
}
61
90
/**
62
91
* Checks if the cache has in item given its unique identifier.
63
- *
92
+ *
64
93
* @param string $key
65
- *
94
+ *
66
95
* @return bool If the item exist and is not yet expired, true is returned.
67
96
* Other than that, false is returned.
68
97
*/
69
98
public static function has (string $ key ) : bool {
70
99
return self ::getDriver ()->has ($ key );
71
100
}
72
- /**
73
- * Removes all items from the cache.
74
- */
75
- public static function flush () {
76
- self ::getDriver ()->flush ();
77
- }
78
101
/**
79
102
* Creates new item in the cache.
80
- *
103
+ *
81
104
* Note that the item will only be added if it does not exist or already
82
105
* expired or the override option is set to true in case it was already
83
106
* created and not expired.
84
- *
107
+ *
85
108
* @param string $key The unique identifier of the item.
86
- *
109
+ *
87
110
* @param mixed $data The data that will be cached.
88
- *
111
+ *
89
112
* @param int $ttl The time at which the data will be kept in the cache (in seconds).
90
- *
113
+ *
91
114
* @param bool $override If cache item already exist which has given key and not yet
92
115
* expired and this one is set to true, the existing item will be overridden by
93
116
* provided data and ttl.
94
- *
117
+ *
95
118
* @return bool If successfully added, the method will return true. False
96
119
* otherwise.
97
120
*/
@@ -100,68 +123,50 @@ public static function set(string $key, $data, int $ttl = 60, bool $override = f
100
123
$ item = new Item ($ key , $ data , $ ttl , defined ('CACHE_SECRET ' ) ? CACHE_SECRET : '' );
101
124
self ::getDriver ()->cache ($ item );
102
125
}
126
+
103
127
return false ;
104
128
}
105
129
/**
106
- * Reads an item from the cache and return its information.
107
- *
108
- * @param string $key The unique identifier of the item.
109
- *
110
- * @return Item|null If such item exist and not yet expired, an object
111
- * of type 'Item' is returned which has all cached item information. Other
112
- * than that, null is returned.
130
+ * Sets storage engine which is used to store, read, update and delete items
131
+ * from the cache.
132
+ *
133
+ * @param Storage $driver
113
134
*/
114
- public static function getItem ( string $ key ) {
115
- return self ::getDriver ()->readItem ( $ key ) ;
135
+ public static function setDriver ( Storage $ driver ) {
136
+ self ::getInst ()->driver = $ driver ;
116
137
}
117
138
/**
118
139
* Updates TTL of specific cache item.
119
- *
140
+ *
120
141
* @param string $key The unique identifier of the item.
121
- *
142
+ *
122
143
* @param int $ttl The new value for TTL.
123
- *
144
+ *
124
145
* @return bool If item is updated, true is returned. Other than that, false
125
146
* is returned.
126
147
*/
127
148
public static function setTTL (string $ key , int $ ttl ) {
128
149
$ item = self ::getItem ($ key );
150
+
129
151
if ($ item === null ) {
130
152
return false ;
131
153
}
132
154
$ item ->setTTL ($ ttl );
133
155
self ::getDriver ()->cache ($ item );
156
+
134
157
return true ;
135
158
}
136
159
/**
137
- * Returns or creates a cache item given its key.
138
- *
139
- *
140
- * @param string $key The unique identifier of the item.
141
- *
142
- * @param callable $generator A callback which is used as a fallback to
143
- * create new cache entry or re-create an existing one if it was expired.
144
- * This callback must return the data that will be cached.
145
- *
146
- * @param int $ttl Time to live of the item in seconds.
147
- *
148
- * @param array $params Any additional parameters to be passed to the callback
149
- * which is used to generate cache data.
150
- * @return null
160
+ * Creates and returns a single instance of the class.
161
+ *
162
+ * @return Cache
151
163
*/
152
- public static function get (string $ key , callable $ generator = null , int $ ttl = 60 , array $ params = []) {
153
- $ data = self ::getDriver ()->read ($ key );
154
-
155
- if ($ data !== null && $ data !== false ) {
156
- return $ data ;
164
+ private static function getInst () : Cache {
165
+ if (self ::$ inst === null ) {
166
+ self ::$ inst = new Cache ();
167
+ self ::setDriver (new FileStorage ());
157
168
}
158
169
159
- if (!is_callable ($ generator )) {
160
- return null ;
161
- }
162
- $ newData = call_user_func_array ($ generator , $ params );
163
- $ item = new Item ($ key , $ newData , $ ttl , defined ('CACHE_SECRET ' ) ? CACHE_SECRET : '' );
164
- self ::getDriver ()->cache ($ item );
165
- return $ newData ;
170
+ return self ::$ inst ;
166
171
}
167
172
}
0 commit comments