redis实现搜索排行榜后记

记得以前写过一篇文章redis实现搜索排行榜,今天在测试redis的时候,发现了其中的一处bug。

我们再统计搜索排行榜的时候,用jedis.zrevrange()方法是不能按照score降序排列得到结果

Set set=jedis.zrevrange("sort", 0, 6);

应该用zrevrangeWithScores(String key,long start,long end)去获取
简单例子如下:

public  Map zrevrangeWithScores(String key,long start,long end){
		Jedis jedis = getJedisPool().getResource();
		Map map=new LinkedHashMap();
		try {
 
			Set tuples=  jedis.zrevrangeWithScores(key, start, end);
			if(tuples==null ){
				return map;
			}
			for(Tuple tuple : tuples){
				double score=tuple.getScore();
				String member=tuple.getElement();
				map.put(member, score);
			}
		} finally {
			  /// ... it's important to return the Jedis instance to the pool once you've finished using it
			getJedisPool().returnResource(jedis);
		}
			/// ... when closing your application:
		//getJedisPool().destroy(); 
		return map;
	}
//备注:得到结果应该由LinkedHashMap去存储。这样可以保证排行榜的第一名存储在第一个位置。以此类推。

你可能感兴趣的:(redis)