Authored by 郝肖肖

添加getBy方法

1 -<?php  
2 -/**  
3 - * Created by PhpStorm.  
4 - * User: Zip  
5 - * Date: 15/4/28  
6 - * Time: 下午3:16  
7 - */  
8 -  
9 -namespace Hood\Cache;  
10 -  
11 -use Hood\Core\Root;  
12 -  
13 -class Memcache extends Root implements CacheInterface  
14 -{  
15 - private $mcInstances = array();  
16 -  
17 - private $persistentIDs = array();  
18 -  
19 - private $section = 'memcached';  
20 -  
21 - private $node = 'servers';  
22 -  
23 - private $tagName = '';  
24 -  
25 - private $prefix = '';  
26 -  
27 - private $persistentID = 'hood.cache';  
28 -  
29 - private $childNodes = 'hosts';  
30 -  
31 - public function __construct($prefix = '', $persistentID = 'hood.cache')  
32 - {  
33 - parent::__construct();  
34 - $this->prefix = $prefix;  
35 - $this->persistentIDs[] = $this->persistentID = $persistentID;  
36 - }  
37 -  
38 - public function init()  
39 - {  
40 - if (isset($this->mcInstances[$this->persistentID])) {  
41 - $mc = $this->mcInstances[$this->persistentID];  
42 - } else {  
43 - $instance = new \Memcache();  
44 - $server = $this->getServerHost('cache');  
45 - $_serverHosts = $server->getServerConfig($this->section, $this->node);  
46 - $mcServers = $this->_makeHosts($server->getServer($_serverHosts[$this->childNodes], 2));  
47 - foreach ($mcServers as $key => $val) {  
48 - $weight = 100;  
49 - if (count($val) == 3) {  
50 - list($host, $port, $weight) = $val;  
51 - } else {  
52 - list($host, $port) = $val;  
53 - }  
54 - $instance->addServer($host, $port, $this->persistentID, $weight);  
55 - }  
56 - $this->mcInstances[$this->persistentID] = $mc = $instance;  
57 - }  
58 - return $mc;  
59 - }  
60 -  
61 - /**  
62 - * 组织host  
63 - * @param array $hosts  
64 - * @return array  
65 - */  
66 - private function _makeHosts(array $hosts)  
67 - {  
68 - $_server = array();  
69 - foreach ($hosts as $key => $val) {  
70 - $_server[] = explode(':', $val);  
71 - }  
72 - return $_server;  
73 - }  
74 -  
75 - /**  
76 - * 设置mc配置的块节点  
77 - * @param $node  
78 - * @return $this  
79 - */  
80 - public function setNode($node = null)  
81 - {  
82 - if ($node != null) $this->node = $node;  
83 - return $this;  
84 - }  
85 -  
86 - /**  
87 - * 设置子节点  
88 - * @param $childNode  
89 - * @return $this  
90 - */  
91 - public function setChildNodes($childNode)  
92 - {  
93 - $this->childNodes = $childNode;  
94 - return $this;  
95 - }  
96 -  
97 - /**  
98 - * 构建tag  
99 - * @param bool $mode  
100 - * @return string  
101 - */  
102 - private function _makeTag($mode = false)  
103 - {  
104 - if (empty($this->tagName)) return '';  
105 - $_tagVal = $this->init()->get($this->tagName);  
106 - if (empty($_tagVal) && $mode == true) {  
107 - $_tagVal = md5(microtime() . mt_rand() . uniqid());  
108 - $this->init()->set($this->tagName, $_tagVal, 0);  
109 - }  
110 - unset($this->tagName);  
111 - return empty($_tagVal) ? '' : $_tagVal . '.';  
112 - }  
113 -  
114 - /**  
115 - * 检索一个元素  
116 - * @param $key  
117 - * @param callable $flags  
118 - * @return mixed  
119 - */  
120 - public function get($key, &$flags = \MEMCACHE_COMPRESSED)  
121 - {  
122 - return $this->init()->get($this->_makeTag() . $key, $flags);  
123 - }  
124 -  
125 - /**  
126 - * 向一个新的key下面增加一个元素  
127 - * @param $key  
128 - * @param $value  
129 - * @param $expiration  
130 - * @return bool  
131 - */  
132 - public function add($key, $value, $expiration = 0)  
133 - {  
134 - return $this->init()->add($this->_makeTag(true) . $key, $value, $expiration);  
135 - }  
136 -  
137 -  
138 - /**  
139 - * 减小数值元素的值  
140 - * @param $key  
141 - * @param int $offset  
142 - * @return int  
143 - */  
144 - public function decrement($key, $offset = 1)  
145 - {  
146 - return $this->init()->decrement($this->_makeTag() . $key, $offset);  
147 - }  
148 -  
149 - /**  
150 - * @param $key  
151 - * @param int $time  
152 - * @return bool  
153 - */  
154 - public function delete($key, $time = 0)  
155 - {  
156 - return $this->init()->delete($this->_makeTag() . $key, $time);  
157 - }  
158 -  
159 -  
160 - /**  
161 - * 增加数值元素的值  
162 - * @param $key  
163 - * @param int $offset  
164 - * @param int $initialValue  
165 - * @param int $expiry  
166 - * @return int  
167 - */  
168 - public function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)  
169 - {  
170 - return $this->init()->increment($this->_makeTag() . $key, $offset, $initialValue, $expiry);  
171 - }  
172 -  
173 -  
174 - /**  
175 - * 设置  
176 - * @param $key  
177 - * @param $value  
178 - * @param int $expiration  
179 - * @return bool  
180 - */  
181 - public function set($key, $var, $expire = 0, $flag = \MEMCACHE_COMPRESSED)  
182 - {  
183 - return $this->init()->set($this->_makeTag(true) . $key, $var, $flag, $expire);  
184 - }  
185 -  
186 - /**  
187 - * 设置tag  
188 - * @param $tagName  
189 - * @return $this  
190 - */  
191 - public function tag($tagName)  
192 - {  
193 - $this->tagName = $tagName;  
194 - return $this;  
195 - } 1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Zip
  5 + * Date: 15/4/28
  6 + * Time: 下午3:16
  7 + */
  8 +
  9 +namespace Hood\Cache;
  10 +
  11 +use Hood\Core\Root;
  12 +
  13 +class Memcache extends Root implements CacheInterface
  14 +{
  15 + private $mcInstances = array();
  16 +
  17 + private $persistentIDs = array();
  18 +
  19 + private $section = 'memcached';
  20 +
  21 + private $node = 'servers';
  22 +
  23 + private $tagName = '';
  24 +
  25 + private $prefix = '';
  26 +
  27 + private $persistentID = 'hood.cache';
  28 +
  29 + private $childNodes = 'hosts';
  30 +
  31 + public function __construct($prefix = '', $persistentID = 'hood.cache')
  32 + {
  33 + parent::__construct();
  34 + $this->prefix = $prefix;
  35 + $this->persistentIDs[] = $this->persistentID = $persistentID;
  36 + }
  37 +
  38 + public function init()
  39 + {
  40 + if (isset($this->mcInstances[$this->persistentID])) {
  41 + $mc = $this->mcInstances[$this->persistentID];
  42 + } else {
  43 + $instance = new \Memcache();
  44 + $server = $this->getServerHost('cache');
  45 + $_serverHosts = $server->getServerConfig($this->section, $this->node);
  46 + $mcServers = $this->_makeHosts($server->getServer($_serverHosts[$this->childNodes], 2));
  47 + foreach ($mcServers as $key => $val) {
  48 + $weight = 100;
  49 + if (count($val) == 3) {
  50 + list($host, $port, $weight) = $val;
  51 + } else {
  52 + list($host, $port) = $val;
  53 + }
  54 + $instance->addServer($host, $port, $this->persistentID, $weight);
  55 + }
  56 + $this->mcInstances[$this->persistentID] = $mc = $instance;
  57 + }
  58 + return $mc;
  59 + }
  60 +
  61 + /**
  62 + * 组织host
  63 + * @param array $hosts
  64 + * @return array
  65 + */
  66 + private function _makeHosts(array $hosts)
  67 + {
  68 + $_server = array();
  69 + foreach ($hosts as $key => $val) {
  70 + $_server[] = explode(':', $val);
  71 + }
  72 + return $_server;
  73 + }
  74 +
  75 + /**
  76 + * 设置mc配置的块节点
  77 + * @param $node
  78 + * @return $this
  79 + */
  80 + public function setNode($node = null)
  81 + {
  82 + if ($node != null) $this->node = $node;
  83 + return $this;
  84 + }
  85 +
  86 + /**
  87 + * 设置子节点
  88 + * @param $childNode
  89 + * @return $this
  90 + */
  91 + public function setChildNodes($childNode)
  92 + {
  93 + $this->childNodes = $childNode;
  94 + return $this;
  95 + }
  96 +
  97 + /**
  98 + * 构建tag
  99 + * @param bool $mode
  100 + * @return string
  101 + */
  102 + private function _makeTag($mode = false)
  103 + {
  104 + if (empty($this->tagName)) return '';
  105 + $_tagVal = $this->init()->get($this->tagName);
  106 + if (empty($_tagVal) && $mode == true) {
  107 + $_tagVal = md5(microtime() . mt_rand() . uniqid());
  108 + $this->init()->set($this->tagName, $_tagVal, 0);
  109 + }
  110 + unset($this->tagName);
  111 + return empty($_tagVal) ? '' : $_tagVal . '.';
  112 + }
  113 +
  114 + /**
  115 + * 检索一个元素
  116 + * @param $key
  117 + * @param callable $flags
  118 + * @return mixed
  119 + */
  120 + public function get($key, &$flags = \MEMCACHE_COMPRESSED)
  121 + {
  122 + return $this->init()->get($this->_makeTag() . $key, $flags);
  123 + }
  124 +
  125 + public function getBy($key, $cacheCb = null, &$casToken = null)
  126 + {
  127 + return $this->init()->get($key, $cacheCb, $casToken);
  128 + }
  129 +
  130 + /**
  131 + * 向一个新的key下面增加一个元素
  132 + * @param $key
  133 + * @param $value
  134 + * @param $expiration
  135 + * @return bool
  136 + */
  137 + public function add($key, $value, $expiration = 0)
  138 + {
  139 + return $this->init()->add($this->_makeTag(true) . $key, $value, $expiration);
  140 + }
  141 +
  142 +
  143 + /**
  144 + * 减小数值元素的值
  145 + * @param $key
  146 + * @param int $offset
  147 + * @return int
  148 + */
  149 + public function decrement($key, $offset = 1)
  150 + {
  151 + return $this->init()->decrement($this->_makeTag() . $key, $offset);
  152 + }
  153 +
  154 + /**
  155 + * @param $key
  156 + * @param int $time
  157 + * @return bool
  158 + */
  159 + public function delete($key, $time = 0)
  160 + {
  161 + return $this->init()->delete($this->_makeTag() . $key, $time);
  162 + }
  163 +
  164 +
  165 + /**
  166 + * 增加数值元素的值
  167 + * @param $key
  168 + * @param int $offset
  169 + * @param int $initialValue
  170 + * @param int $expiry
  171 + * @return int
  172 + */
  173 + public function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)
  174 + {
  175 + return $this->init()->increment($this->_makeTag() . $key, $offset, $initialValue, $expiry);
  176 + }
  177 +
  178 +
  179 + /**
  180 + * 设置
  181 + * @param $key
  182 + * @param $value
  183 + * @param int $expiration
  184 + * @return bool
  185 + */
  186 + public function set($key, $var, $expire = 0, $flag = \MEMCACHE_COMPRESSED)
  187 + {
  188 + return $this->init()->set($this->_makeTag(true) . $key, $var, $flag, $expire);
  189 + }
  190 +
  191 + /**
  192 + * 设置tag
  193 + * @param $tagName
  194 + * @return $this
  195 + */
  196 + public function tag($tagName)
  197 + {
  198 + $this->tagName = $tagName;
  199 + return $this;
  200 + }
196 } 201 }
1 -<?php  
2 -/**  
3 - * Created by PhpStorm.  
4 - * User: Zip  
5 - * Date: 14/11/23  
6 - * Time: 上午1:39  
7 - */  
8 -  
9 -namespace Hood\Cache;  
10 -  
11 -use Hood\Core\Root;  
12 -use Hood\Debug\DebugException;  
13 -  
14 -class Memcached extends Root implements CacheInterface  
15 -{  
16 - private $mcInstances = array();  
17 -  
18 - private $persistentIDs = array();  
19 -  
20 - private $timeout = 150;  
21 -  
22 - private $section = 'memcached';  
23 -  
24 - private $node = 'servers';  
25 -  
26 - private $tagName = '';  
27 -  
28 - private $prefix = '';  
29 -  
30 - private $persistentID = 'hood.cache';  
31 -  
32 - private $childNodes = 'hosts';  
33 -  
34 - public function __construct($prefix = '', $persistentID = 'hood.cache')  
35 - {  
36 - parent::__construct();  
37 - $this->prefix = $prefix;  
38 - $this->persistentIDs[] = $this->persistentID = $persistentID;  
39 - }  
40 -  
41 - /**  
42 - * 设置子节点  
43 - * @param $childNode  
44 - * @return $this  
45 - */  
46 - public function setChildNodes($childNode)  
47 - {  
48 - $this->childNodes = $childNode;  
49 - return $this;  
50 - }  
51 -  
52 - /**  
53 - * 设置前缀  
54 - * @param $prefix  
55 - * @return $this  
56 - */  
57 - public function setPrefix($prefix)  
58 - {  
59 - $this->prefix = $prefix;  
60 - return $this;  
61 - }  
62 -  
63 - /**  
64 - * 设置共享连接ID  
65 - * @param $persistentID  
66 - * @return $this  
67 - */  
68 - public function setPersistentID($persistentID)  
69 - {  
70 - $this->persistentID = $persistentID;  
71 - return $this;  
72 - }  
73 -  
74 - /**  
75 - * @param $persistentID  
76 - * @return \Memcached  
77 - * @throws \Hood\Debug\DebugException  
78 - */  
79 - private function init()  
80 - {  
81 - if (isset($this->mcInstances[$this->persistentID])) {  
82 - $mc = $this->mcInstances[$this->persistentID];  
83 - } else {  
84 - $instance = new \Memcached();  
85 - $instance->setOption(\Memcached::OPT_PREFIX_KEY, $this->prefix);  
86 - $instance->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); // 开启一致性哈希 取模(默认)/ 一致性  
87 - $instance->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);//ketama算法兼容 设置为md5并且分布算法将会 采用带有权重的一致性hash分布  
88 - $instance->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->timeout);  
89 - if (count($instance->getServerList()) < 1) {  
90 - $server = $this->getServerHost('cache');  
91 - $_serverHosts = $server->getServerConfig($this->section, $this->node);  
92 - if (empty($_serverHosts[$this->childNodes])) {  
93 - throw new DebugException('Memcache Host Config is Null.');  
94 - }  
95 - $mcServers = $this->_makeHosts($server->getServer($_serverHosts[$this->childNodes], 2));  
96 - $instance->addServers($mcServers);  
97 - unset($mcServers);  
98 - }  
99 - $this->mcInstances[$this->persistentID] = $mc = $instance;  
100 - }  
101 - return $mc;  
102 - }  
103 -  
104 -  
105 - /**  
106 - * 设置mc配置的块  
107 - * @param $section  
108 - * @return $this  
109 - */  
110 - public function setSection($section)  
111 - {  
112 - $this->section = $section;  
113 - return $this;  
114 - }  
115 -  
116 - /**  
117 - * 设置mc配置的块节点  
118 - * @param $node  
119 - * @return $this  
120 - */  
121 - public function setNode($node = null)  
122 - {  
123 - if ($node != null) $this->node = $node;  
124 - return $this;  
125 - }  
126 -  
127 - /**  
128 - * 组织host  
129 - * @param array $hosts  
130 - * @return array  
131 - */  
132 - private function _makeHosts(array $hosts)  
133 - {  
134 - $_server = array();  
135 - foreach ($hosts as $key => $val) {  
136 - $_server[] = explode(':', $val);  
137 - }  
138 - return $_server;  
139 - }  
140 -  
141 - /**  
142 - * 构建tag  
143 - * @param bool $mode  
144 - * @return string  
145 - */  
146 - private function _makeTag($mode = false)  
147 - {  
148 - if (empty($this->tagName)) return '';  
149 - $_tagVal = $this->init()->get($this->tagName);  
150 - if (empty($_tagVal) && $mode == true) {  
151 - $_tagVal = md5(microtime() . mt_rand() . uniqid());  
152 - $this->init()->set($this->tagName, $_tagVal, 0);  
153 - }  
154 - unset($this->tagName);  
155 - return empty($_tagVal) ? '' : $_tagVal . '.';  
156 - }  
157 -  
158 - /**  
159 - * 检索一个元素  
160 - * @param $key  
161 - * @param callable $cache_cb  
162 - * @param float $cas_token  
163 - * @return mixed  
164 - */  
165 - public function get($key, $cacheCb = null, &$casToken = null)  
166 - {  
167 - return $this->init()->get($this->_makeTag() . $key, $cacheCb, $casToken);  
168 - }  
169 -  
170 - /**  
171 - * 向一个新的key下面增加一个元素  
172 - * @param $key  
173 - * @param $value  
174 - * @param $expiration  
175 - * @return bool  
176 - */  
177 - public function add($key, $value, $expiration = 0)  
178 - {  
179 - return $this->init()->add($this->_makeTag(true) . $key, $value, $expiration);  
180 - }  
181 -  
182 - /**  
183 - * 向已存在元素后追加数据  
184 - * @param $key  
185 - * @param $value  
186 - * @return bool  
187 - */  
188 - public function append($key, $value)  
189 - {  
190 - return $this->init()->append($this->_makeTag(true) . $key, $value);  
191 - }  
192 -  
193 - /**  
194 - * 比较并交换值  
195 - * @param $casToken  
196 - * @param $key  
197 - * @param $value  
198 - * @param int $expiration  
199 - * @return bool  
200 - */  
201 - public function cas($casToken, $key, $value, $expiration = 0)  
202 - {  
203 - return $this->init()->cas($casToken, $this->_makeTag(true) . $key, $value, $expiration);  
204 - }  
205 -  
206 - /**  
207 - * 减小数值元素的值  
208 - * @param $key  
209 - * @param int $offset  
210 - * @return int  
211 - */  
212 - public function decrement($key, $offset = 1)  
213 - {  
214 - return $this->init()->decrement($this->_makeTag() . $key, $offset);  
215 - }  
216 -  
217 - /**  
218 - * @param $key  
219 - * @param int $time  
220 - * @return bool  
221 - */  
222 - public function delete($key, $time = 0)  
223 - {  
224 - return $this->init()->delete($this->_makeTag() . $key, $time);  
225 - }  
226 -  
227 - /**  
228 - * 删除多个数据  
229 - * @param array $keys  
230 - * @param int $time  
231 - * @return bool  
232 - */  
233 - public function deleteMulti(array $keys, $time = 0)  
234 - {  
235 - return $this->init()->deleteMulti($this->_makeMultiKey($keys), $time);  
236 - }  
237 -  
238 - /**  
239 - * 组合多key 数据  
240 - * @param $keys  
241 - * @return array  
242 - */  
243 - private function _makeMultiKey($keys, $mode = false)  
244 - {  
245 - $_keys = array();  
246 - $tag = $this->_makeTag($mode);  
247 - foreach ($keys as $key) {  
248 - $_keys[] = $tag . $key;  
249 - }  
250 - return $_keys;  
251 - }  
252 -  
253 - /**  
254 - * 请求多个元素  
255 - * @param array $keys  
256 - * @param null $withCas  
257 - * @param callable $valueCb  
258 - * @return bool  
259 - */  
260 - public function getDelayed(array $keys, $withCas = null, callable $valueCb = null)  
261 - {  
262 - return $this->init()->getDelayed($this->_makeMultiKey($keys), $withCas, $valueCb);  
263 - }  
264 -  
265 - /**  
266 - * 抓取所有剩余的结果  
267 - * @return array  
268 - */  
269 - public function fetchAll()  
270 - {  
271 - return $this->init()->fetchAll();  
272 - }  
273 -  
274 - /**  
275 - * 检索多个元素  
276 - * @param array $keys  
277 - * @param array $cas_tokens  
278 - * @param null $flags  
279 - * @return mixed  
280 - */  
281 - public function getMulti(array $keys, array &$casTokens = null, $flags = null)  
282 - {  
283 - return $this->init()->getMulti($this->_makeMultiKey($keys), $casTokens, $flags);  
284 - }  
285 -  
286 - /**  
287 - * 增加数值元素的值  
288 - * @param $key  
289 - * @param int $offset  
290 - * @param int $initialValue  
291 - * @param int $expiry  
292 - * @return int  
293 - */  
294 - public function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)  
295 - {  
296 - return $this->init()->increment($this->_makeTag() . $key, $offset, $initialValue, $expiry);  
297 - }  
298 -  
299 - /**  
300 - * 检查memcache是否长连接  
301 - * @return bool  
302 - */  
303 - public function isPersistent()  
304 - {  
305 - return $this->init()->isPersistent();  
306 - }  
307 -  
308 - /**  
309 - * 设置  
310 - * @param $key  
311 - * @param $value  
312 - * @param int $expiration  
313 - * @return bool  
314 - */  
315 - public function set($key, $value, $expiration = 0)  
316 - {  
317 - return $this->init()->set($this->_makeTag(true) . $key, $value, $expiration);  
318 - }  
319 -  
320 - /**  
321 - * 设置多个数据  
322 - * @param array $items  
323 - * @param int $expiration  
324 - * @return bool  
325 - */  
326 - public function setMulti(array $items, $expiration = 0)  
327 - {  
328 - $_items = array();  
329 - $tag = $this->_makeTag(true);  
330 - foreach ($items as $key => $val) {  
331 - $_items[$tag . $key] = $val;  
332 - }  
333 - return $this->init()->setMulti($_items, $expiration);  
334 - }  
335 -  
336 - /**  
337 - * 设置tag  
338 - * @param $tagName  
339 - * @return $this  
340 - */  
341 - public function tag($tagName)  
342 - {  
343 - $this->tagName = $tagName;  
344 - return $this;  
345 - }  
346 -  
347 - /**  
348 - * 清除服务列表  
349 - * @return $this  
350 - */  
351 - public function resetServerList()  
352 - {  
353 - $this->init()->resetServerList();  
354 - return $this;  
355 - } 1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Zip
  5 + * Date: 14/11/23
  6 + * Time: 上午1:39
  7 + */
  8 +
  9 +namespace Hood\Cache;
  10 +
  11 +use Hood\Core\Root;
  12 +use Hood\Debug\DebugException;
  13 +
  14 +class Memcached extends Root implements CacheInterface
  15 +{
  16 + private $mcInstances = array();
  17 +
  18 + private $persistentIDs = array();
  19 +
  20 + private $timeout = 150;
  21 +
  22 + private $section = 'memcached';
  23 +
  24 + private $node = 'servers';
  25 +
  26 + private $tagName = '';
  27 +
  28 + private $prefix = '';
  29 +
  30 + private $persistentID = 'hood.cache';
  31 +
  32 + private $childNodes = 'hosts';
  33 +
  34 + public function __construct($prefix = '', $persistentID = 'hood.cache')
  35 + {
  36 + parent::__construct();
  37 + $this->prefix = $prefix;
  38 + $this->persistentIDs[] = $this->persistentID = $persistentID;
  39 + }
  40 +
  41 + /**
  42 + * 设置子节点
  43 + * @param $childNode
  44 + * @return $this
  45 + */
  46 + public function setChildNodes($childNode)
  47 + {
  48 + $this->childNodes = $childNode;
  49 + return $this;
  50 + }
  51 +
  52 + /**
  53 + * 设置前缀
  54 + * @param $prefix
  55 + * @return $this
  56 + */
  57 + public function setPrefix($prefix)
  58 + {
  59 + $this->prefix = $prefix;
  60 + return $this;
  61 + }
  62 +
  63 + /**
  64 + * 设置共享连接ID
  65 + * @param $persistentID
  66 + * @return $this
  67 + */
  68 + public function setPersistentID($persistentID)
  69 + {
  70 + $this->persistentID = $persistentID;
  71 + return $this;
  72 + }
  73 +
  74 + /**
  75 + * @param $persistentID
  76 + * @return \Memcached
  77 + * @throws \Hood\Debug\DebugException
  78 + */
  79 + private function init()
  80 + {
  81 + if (isset($this->mcInstances[$this->persistentID])) {
  82 + $mc = $this->mcInstances[$this->persistentID];
  83 + } else {
  84 + $instance = new \Memcached();
  85 + $instance->setOption(\Memcached::OPT_PREFIX_KEY, $this->prefix);
  86 + $instance->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); // 开启一致性哈希 取模(默认)/ 一致性
  87 + $instance->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);//ketama算法兼容 设置为md5并且分布算法将会 采用带有权重的一致性hash分布
  88 + $instance->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->timeout);
  89 + if (count($instance->getServerList()) < 1) {
  90 + $server = $this->getServerHost('cache');
  91 + $_serverHosts = $server->getServerConfig($this->section, $this->node);
  92 + if (empty($_serverHosts[$this->childNodes])) {
  93 + throw new DebugException('Memcache Host Config is Null.');
  94 + }
  95 + $mcServers = $this->_makeHosts($server->getServer($_serverHosts[$this->childNodes], 2));
  96 + $instance->addServers($mcServers);
  97 + unset($mcServers);
  98 + }
  99 + $this->mcInstances[$this->persistentID] = $mc = $instance;
  100 + }
  101 + return $mc;
  102 + }
  103 +
  104 +
  105 + /**
  106 + * 设置mc配置的块
  107 + * @param $section
  108 + * @return $this
  109 + */
  110 + public function setSection($section)
  111 + {
  112 + $this->section = $section;
  113 + return $this;
  114 + }
  115 +
  116 + /**
  117 + * 设置mc配置的块节点
  118 + * @param $node
  119 + * @return $this
  120 + */
  121 + public function setNode($node = null)
  122 + {
  123 + if ($node != null) $this->node = $node;
  124 + return $this;
  125 + }
  126 +
  127 + /**
  128 + * 组织host
  129 + * @param array $hosts
  130 + * @return array
  131 + */
  132 + private function _makeHosts(array $hosts)
  133 + {
  134 + $_server = array();
  135 + foreach ($hosts as $key => $val) {
  136 + $_server[] = explode(':', $val);
  137 + }
  138 + return $_server;
  139 + }
  140 +
  141 + /**
  142 + * 构建tag
  143 + * @param bool $mode
  144 + * @return string
  145 + */
  146 + private function _makeTag($mode = false)
  147 + {
  148 + if (empty($this->tagName)) return '';
  149 + $_tagVal = $this->init()->get($this->tagName);
  150 + if (empty($_tagVal) && $mode == true) {
  151 + $_tagVal = md5(microtime() . mt_rand() . uniqid());
  152 + $this->init()->set($this->tagName, $_tagVal, 0);
  153 + }
  154 + unset($this->tagName);
  155 + return empty($_tagVal) ? '' : $_tagVal . '.';
  156 + }
  157 +
  158 + /**
  159 + * 检索一个元素
  160 + * @param $key
  161 + * @param callable $cache_cb
  162 + * @param float $cas_token
  163 + * @return mixed
  164 + */
  165 + public function get($key, $cacheCb = null, &$casToken = null)
  166 + {
  167 + return $this->init()->get($this->_makeTag() . $key, $cacheCb, $casToken);
  168 + }
  169 +
  170 + public function getBy($key, $cacheCb = null, &$casToken = null)
  171 + {
  172 + return $this->init()->get($key, $cacheCb, $casToken);
  173 + }
  174 +
  175 + /**
  176 + * 向一个新的key下面增加一个元素
  177 + * @param $key
  178 + * @param $value
  179 + * @param $expiration
  180 + * @return bool
  181 + */
  182 + public function add($key, $value, $expiration = 0)
  183 + {
  184 + return $this->init()->add($this->_makeTag(true) . $key, $value, $expiration);
  185 + }
  186 +
  187 + /**
  188 + * 向已存在元素后追加数据
  189 + * @param $key
  190 + * @param $value
  191 + * @return bool
  192 + */
  193 + public function append($key, $value)
  194 + {
  195 + return $this->init()->append($this->_makeTag(true) . $key, $value);
  196 + }
  197 +
  198 + /**
  199 + * 比较并交换值
  200 + * @param $casToken
  201 + * @param $key
  202 + * @param $value
  203 + * @param int $expiration
  204 + * @return bool
  205 + */
  206 + public function cas($casToken, $key, $value, $expiration = 0)
  207 + {
  208 + return $this->init()->cas($casToken, $this->_makeTag(true) . $key, $value, $expiration);
  209 + }
  210 +
  211 + /**
  212 + * 减小数值元素的值
  213 + * @param $key
  214 + * @param int $offset
  215 + * @return int
  216 + */
  217 + public function decrement($key, $offset = 1)
  218 + {
  219 + return $this->init()->decrement($this->_makeTag() . $key, $offset);
  220 + }
  221 +
  222 + /**
  223 + * @param $key
  224 + * @param int $time
  225 + * @return bool
  226 + */
  227 + public function delete($key, $time = 0)
  228 + {
  229 + return $this->init()->delete($this->_makeTag() . $key, $time);
  230 + }
  231 +
  232 + /**
  233 + * 删除多个数据
  234 + * @param array $keys
  235 + * @param int $time
  236 + * @return bool
  237 + */
  238 + public function deleteMulti(array $keys, $time = 0)
  239 + {
  240 + return $this->init()->deleteMulti($this->_makeMultiKey($keys), $time);
  241 + }
  242 +
  243 + /**
  244 + * 组合多key 数据
  245 + * @param $keys
  246 + * @return array
  247 + */
  248 + private function _makeMultiKey($keys, $mode = false)
  249 + {
  250 + $_keys = array();
  251 + $tag = $this->_makeTag($mode);
  252 + foreach ($keys as $key) {
  253 + $_keys[] = $tag . $key;
  254 + }
  255 + return $_keys;
  256 + }
  257 +
  258 + /**
  259 + * 请求多个元素
  260 + * @param array $keys
  261 + * @param null $withCas
  262 + * @param callable $valueCb
  263 + * @return bool
  264 + */
  265 + public function getDelayed(array $keys, $withCas = null, callable $valueCb = null)
  266 + {
  267 + return $this->init()->getDelayed($this->_makeMultiKey($keys), $withCas, $valueCb);
  268 + }
  269 +
  270 + /**
  271 + * 抓取所有剩余的结果
  272 + * @return array
  273 + */
  274 + public function fetchAll()
  275 + {
  276 + return $this->init()->fetchAll();
  277 + }
  278 +
  279 + /**
  280 + * 检索多个元素
  281 + * @param array $keys
  282 + * @param array $cas_tokens
  283 + * @param null $flags
  284 + * @return mixed
  285 + */
  286 + public function getMulti(array $keys, array &$casTokens = null, $flags = null)
  287 + {
  288 + return $this->init()->getMulti($this->_makeMultiKey($keys), $casTokens, $flags);
  289 + }
  290 +
  291 + /**
  292 + * 增加数值元素的值
  293 + * @param $key
  294 + * @param int $offset
  295 + * @param int $initialValue
  296 + * @param int $expiry
  297 + * @return int
  298 + */
  299 + public function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)
  300 + {
  301 + return $this->init()->increment($this->_makeTag() . $key, $offset, $initialValue, $expiry);
  302 + }
  303 +
  304 + /**
  305 + * 检查memcache是否长连接
  306 + * @return bool
  307 + */
  308 + public function isPersistent()
  309 + {
  310 + return $this->init()->isPersistent();
  311 + }
  312 +
  313 + /**
  314 + * 设置
  315 + * @param $key
  316 + * @param $value
  317 + * @param int $expiration
  318 + * @return bool
  319 + */
  320 + public function set($key, $value, $expiration = 0)
  321 + {
  322 + return $this->init()->set($this->_makeTag(true) . $key, $value, $expiration);
  323 + }
  324 +
  325 + /**
  326 + * 设置多个数据
  327 + * @param array $items
  328 + * @param int $expiration
  329 + * @return bool
  330 + */
  331 + public function setMulti(array $items, $expiration = 0)
  332 + {
  333 + $_items = array();
  334 + $tag = $this->_makeTag(true);
  335 + foreach ($items as $key => $val) {
  336 + $_items[$tag . $key] = $val;
  337 + }
  338 + return $this->init()->setMulti($_items, $expiration);
  339 + }
  340 +
  341 + /**
  342 + * 设置tag
  343 + * @param $tagName
  344 + * @return $this
  345 + */
  346 + public function tag($tagName)
  347 + {
  348 + $this->tagName = $tagName;
  349 + return $this;
  350 + }
  351 +
  352 + /**
  353 + * 清除服务列表
  354 + * @return $this
  355 + */
  356 + public function resetServerList()
  357 + {
  358 + $this->init()->resetServerList();
  359 + return $this;
  360 + }
356 } 361 }