Home >php教程 >php手册 >批量获取memcache值并按key的顺序返回的实现代码

批量获取memcache值并按key的顺序返回的实现代码

WBOY
WBOYOriginal
2016-06-06 20:37:191034browse

一台memcache时返回是正确的,在有多台memcache时就无法一一对应的按顺序返回。

通过memcached的getMulti函数来批量获取如下15个ID的值。
31639,33878,177410,9735,589,12076,25953,22447,15368,15358,33853,26658,26659,12477,15366

$md->getMulti($arr_id);
返回的顺序:
line_31639,line_33878,line_177410,line_9735,line_589,line_12076,line_25953,line_22447,line_15368,line_15358,line_33853,line_26658,line_26659,line_12477,line_15366,

一台memcache时返回是正确的,在有多台memcache时就无法一一对应的按顺序返回。

这时需要使用Memcached::GET_PRESERVE_ORDER才能按顺序返回数据:
$arrs = $mem->getMulti($arr_id, $cas, Memcached::GET_PRESERVE_ORDER);
返回的顺序:
line_31639,line_33878,line_9735,line_589,line_22447,line_15358,line_33853,line_26658,line_177410,line_12076,line_25953,line_15368,line_26659,line_12477,line_15366,

这其中,如果有没有命中的变量,那么$cas就起到作用了。$cas返回了命中的变量,遍历$cas取出已命中的变量组成数据,然后通过array_diff函数与$arr_id比较取出差,再去一一设置。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn