|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cache |
|
5
|
|
|
* |
|
6
|
|
|
* Multi-strategy cache store. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.com |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Cache { |
|
14
|
|
|
use Module, Events; |
|
15
|
|
|
|
|
16
|
|
|
protected static $driver = null, |
|
17
|
|
|
$enabled = true; |
|
18
|
|
|
|
|
19
|
|
|
public static function get($key, $default='', $expire=0){ |
|
20
|
|
|
if (static::$enabled){ |
|
21
|
|
|
$hash = static::hash($key); |
|
22
|
|
|
if(static::$driver->exists($hash) && $results = static::$driver->get($hash)){ |
|
23
|
|
|
return $results; |
|
24
|
|
|
} else { |
|
25
|
|
|
if($data = is_callable($default)?call_user_func($default):$default){ |
|
26
|
|
|
static::$driver->set($hash,$data,$expire); |
|
27
|
|
|
} |
|
28
|
|
|
return $data; |
|
29
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
|
|
return is_callable($default) ? call_user_func($default) : $default; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Load cache drivers with a FCFS strategy |
|
37
|
|
|
* |
|
38
|
|
|
* @method using |
|
39
|
|
|
* @param mixed $driver can be a single driver name string, an array of driver names or a map [ driver_name => driver_options array ] |
|
40
|
|
|
* @return bool true if a driver was loaded |
|
41
|
|
|
* @example |
|
42
|
|
|
* |
|
43
|
|
|
* Cache::using('redis'); |
|
44
|
|
|
* Cache::using(['redis','files','memory']); // Prefer "redis" over "files" over "memory" caching |
|
45
|
|
|
* Cache::using([ |
|
46
|
|
|
* 'redis' => [ |
|
47
|
|
|
* 'host' => '127.0.0.1', |
|
48
|
|
|
* 'prefix' => 'mycache', |
|
49
|
|
|
* ], |
|
50
|
|
|
* 'files' => [ |
|
51
|
|
|
* 'cache_dir' => '/tmp', |
|
52
|
|
|
* ], |
|
53
|
|
|
* 'memory' |
|
54
|
|
|
* ]); |
|
55
|
|
|
* |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function using($driver){ |
|
58
|
|
|
foreach((array)$driver as $key => $value){ |
|
59
|
|
|
if(is_numeric($key)){ |
|
60
|
|
|
$drv = $value; |
|
61
|
|
|
$conf = []; |
|
62
|
|
|
} else { |
|
63
|
|
|
$drv = $key; |
|
64
|
|
|
$conf = $value; |
|
65
|
|
|
} |
|
66
|
|
|
$class = 'Cache\\' . ucfirst(strtolower($drv)); |
|
67
|
|
|
if(class_exists($class) && $class::valid()) { |
|
68
|
|
|
static::$driver = new $class($conf); |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns/Set master switch on cache. |
|
77
|
|
|
* |
|
78
|
|
|
* @method enabled |
|
79
|
|
|
* |
|
80
|
|
|
* @param boolean $enabled Enable/Disable the cache status. |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean Cache on/off status |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function enabled($enabled=null){ |
|
85
|
|
|
return $enabled ? static::$enabled : static::$enabled = $enabled; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function set($key, $value, $expire=0){ |
|
89
|
|
|
return static::$driver->set(static::hash($key),$value,$expire); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function delete($key){ |
|
93
|
|
|
return static::$driver->delete(static::hash($key)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function exists($key){ |
|
97
|
|
|
return static::$enabled && static::$driver->exists(static::hash($key)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function flush(){ |
|
101
|
|
|
return static::$driver->flush(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function inc($key, $value=1){ |
|
105
|
|
|
return static::$driver->inc(static::hash($key),$value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function dec($key, $value=1){ |
|
109
|
|
|
return static::$driver->dec(static::hash($key),$value); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static function hash($key, $group=null){ |
|
113
|
|
|
static $hashes = []; |
|
114
|
|
|
if (false === isset($hashes[$group][$key])){ |
|
115
|
|
|
$k = $key; |
|
116
|
|
|
if(is_array($key) && count($key)>1) list($group,$key) = $k; |
|
117
|
|
|
$hashes[$group][$key] = ($group?$group.'-':'') . md5($key); |
|
118
|
|
|
} |
|
119
|
|
|
return $hashes[$group][$key]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
Cache::using(['files','memory']); |
|
125
|
|
|
|