Home > php教程 > PHP源码 > redis排序集

redis排序集

PHP中文网
Release: 2016-05-25 17:04:05
Original
1218 people have browsed it

业务需求,需要对数据做排序统计,redis数据存储方式中有一个数据集(Sorted sets)的概念,可以满足我们的需求,具体性能还没测试

PHP代码

<?php
$redis = new Redis();
$redis->connect(&#39;192.168.1.233&#39;,&#39;6379&#39;);
 
$b = $redis->zincrby(&#39;myadd&#39;,1,&#39;b&#39;);  //b的score加1,并返回当前b的score
$a = $redis->zincrby(&#39;myadd&#39;,1,&#39;a&#39;);
$c = $redis->zincrby(&#39;myadd&#39;,1,&#39;c&#39;);
$d = $redis->zincrby(&#39;myadd&#39;,1,&#39;d&#39;);
 
echo &#39;a&#39;.&#39;:&#39;.$redis->zscore(&#39;myadd&#39;, &#39;a&#39;)."\n"; //返回a的score
echo &#39;b&#39;.&#39;:&#39;.$redis->zscore(&#39;myadd&#39;, &#39;b&#39;)."\n"; 
echo &#39;c&#39;.&#39;:&#39;.$redis->zscore(&#39;myadd&#39;, &#39;c&#39;)."\n";
echo &#39;d&#39;.&#39;:&#39;.$redis->zscore(&#39;myadd&#39;, &#39;d&#39;)."\n";
 
var_dump($redis->zrank(&#39;myadd&#39;,&#39;c&#39;));//以score升序的方式,返回该value的在集合中的位置,0为第一个
var_dump($redis->zrange(&#39;myadd&#39;,0,-1,true)); //以value升序的方式显示
 
?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template