1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cache |
4
|
|
|
* Dependency injected cache API |
5
|
|
|
* |
6
|
|
|
* @package erdiko/core |
7
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
8
|
|
|
* @author John Arroyo <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace erdiko\core; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Cache |
14
|
|
|
{ |
15
|
|
|
/** Cache singleton instance */ |
16
|
|
|
private static $instance = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the cache instance |
20
|
|
|
* |
21
|
|
|
* @param string $cacheConfig |
22
|
|
|
* @return object |
23
|
|
|
*/ |
24
|
|
|
public static function getCacheObject($cacheConfig = 'default') |
25
|
|
|
{ |
26
|
|
|
//Check if the caller requests an new object |
27
|
|
|
if (empty(static::$instance[$cacheConfig])) { |
|
|
|
|
28
|
|
|
$config = Helper::getConfig('application', $cacheConfig); |
29
|
|
|
|
30
|
|
|
//Check if the object already be created |
31
|
|
|
if (isset($config["cache"][$cacheConfig])) { |
32
|
|
|
static::$instance[$cacheConfig] = new $config["cache"][$cacheConfig]['class']; |
|
|
|
|
33
|
|
|
} else { |
34
|
|
|
throw new \Exception("There is no cache config defined ({$cacheConfig})"); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return static::$instance[$cacheConfig]; |
|
|
|
|
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the value stored at the given key |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* @param string $cacheConfig |
47
|
|
|
*/ |
48
|
|
|
public static function get($key, $cacheConfig = 'default') |
49
|
|
|
{ |
50
|
|
|
return static::getCacheObject($cacheConfig)->get($key); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Put the supplied value into the given key |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @param string $cacheConfig |
59
|
|
|
*/ |
60
|
|
|
public static function put($key, $value, $cacheConfig = 'default') |
61
|
|
|
{ |
62
|
|
|
return static::getCacheObject($cacheConfig)->put($key, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check if the key exist |
67
|
|
|
* |
68
|
|
|
* @param string $key |
69
|
|
|
* @param string $cacheConfig |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public static function has($key, $cacheConfig = 'default') |
73
|
|
|
{ |
74
|
|
|
return static::getCacheObject($cacheConfig)->has($key); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve the cache value and then delete it before returning that value |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param string $cacheConfig |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public static function pull($key, $cacheConfig = 'default') |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$value = static::get($key); |
87
|
|
|
static::delete($key); |
88
|
|
|
|
89
|
|
|
return $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Remove an item from the cache |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* @param string $cacheConfig |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public static function delete($key, $cacheConfig = 'default') |
100
|
|
|
{ |
101
|
|
|
return static::getCacheObject($cacheConfig)->delete($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Delete all cache keys (Purge) |
106
|
|
|
* |
107
|
|
|
* @param string $cacheConfig |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public static function clear($cacheConfig = 'default') |
111
|
|
|
{ |
112
|
|
|
return static::getCacheObject($cacheConfig)->clear(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: