|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
|
4
|
|
|
exit('No direct script access allowed'); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Image CMS |
|
9
|
|
|
* Memcache Class |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class Mem_cache extends Memcache |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
public $CI; |
|
16
|
|
|
|
|
17
|
|
|
public $get = 0; |
|
18
|
|
|
|
|
19
|
|
|
public $set = 0; |
|
20
|
|
|
|
|
21
|
|
|
public $key_prefix = ''; |
|
22
|
|
|
|
|
23
|
|
|
//Cache config |
|
24
|
|
|
public $_Config = [ |
|
25
|
|
|
'store' => 'cache', |
|
26
|
|
|
'ttl' => 3600, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() { |
|
30
|
|
|
|
|
31
|
|
|
$this->CI = & get_instance(); |
|
32
|
|
|
$this->connect('localhost', 11211) or die('Could not connect to memcache server.'); |
|
33
|
|
|
$this->key_prefix = base_url(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Fetch Cache |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $key |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function fetch($key, $group = FALSE) { |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if (($ret = $this->_fetch($key)) === false) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} else { |
|
48
|
|
|
return $ret; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function _fetch($key) { |
|
53
|
|
|
|
|
54
|
|
|
$key = $this->generatekey($key); |
|
55
|
|
|
|
|
56
|
|
|
$this->get++; |
|
57
|
|
|
|
|
58
|
|
|
return $this->get($key); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Fetch cached function |
|
63
|
|
|
*/ |
|
64
|
|
|
public function fetch_func($object, $func, $args = []) { |
|
65
|
|
|
|
|
66
|
|
|
$key = $this->generatekey(get_class($object) . '::' . $func . '::' . serialize($args)); |
|
67
|
|
|
|
|
68
|
|
|
$this->get++; |
|
69
|
|
|
|
|
70
|
|
|
return $this->get($key); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Store Cache Item |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key |
|
77
|
|
|
* @param mixed $data |
|
78
|
|
|
* @param int $ttl |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function store($key, $data, $ttl = false, $group = false) { |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (!$ttl) { |
|
85
|
|
|
$ttl = $this->_Config['ttl']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->set($this->generatekey($key), $data, FALSE, $ttl); |
|
89
|
|
|
|
|
90
|
|
|
$this->set++; |
|
91
|
|
|
return TRUE; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Cache Function |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
* @access public |
|
99
|
|
|
*/ |
|
100
|
|
|
public function call($func = [], $args = [], $ttl = FALSE) { |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if ($ttl === FALSE) { |
|
103
|
|
|
$ttl = $this->_Config['ttl']; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$arguments = func_get_args(); |
|
107
|
|
|
|
|
108
|
|
|
//class_name::metohd |
|
109
|
|
|
$key = get_class($arguments[0][0]) . '::' . $arguments[0][1] . '::' . serialize($args); |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
if (($cache = $this->fetch($key)) !== false) { |
|
112
|
|
|
return $cache; |
|
113
|
|
|
} else { |
|
114
|
|
|
$target = array_shift($arguments); |
|
115
|
|
|
$result = call_user_func_array($target, $args); |
|
116
|
|
|
|
|
117
|
|
|
if (!$this->store($key, $result, false)) { |
|
118
|
|
|
return FALSE; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $result; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Clean all cache objects |
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function Clean() { |
|
131
|
|
|
|
|
132
|
|
|
$this->flush(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Clean all cache objects |
|
137
|
|
|
* |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
|
|
public function delete_group() { |
|
141
|
|
|
|
|
142
|
|
|
$this->flush(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Delete Cache Item |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $key - cache item key |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
|
|
|
|
|
151
|
|
|
*/ |
|
152
|
|
|
public function delete($key) { |
|
153
|
|
|
|
|
154
|
|
|
$this->delete($this->generatekey($key)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Delete Cached Function |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool |
|
|
|
|
|
|
161
|
|
|
*/ |
|
162
|
|
|
public function delete_func($object, $func, $args = []) { |
|
163
|
|
|
|
|
164
|
|
|
$file = $this->generatekey(get_class($object) . '::' . $func . '::' . serialize($args)); |
|
165
|
|
|
$this->delete($file); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Delete All Cache Items |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
|
|
|
|
|
172
|
|
|
* @access public |
|
173
|
|
|
*/ |
|
174
|
|
|
public function delete_all() { |
|
175
|
|
|
|
|
176
|
|
|
$this->flush(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Generate key |
|
181
|
|
|
* |
|
182
|
|
|
* @param $key |
|
183
|
|
|
* @return string */ |
|
184
|
|
|
public function generatekey($key) { |
|
185
|
|
|
|
|
186
|
|
|
return md5($this->key_prefix . $key); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/* End of cache.php */ |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.