/** * 通过key将获取score从start到end中zset的value * * <p>socre从大到小排序 * * <p>当start为0 end为-1时返回全部 * * @param key * @param start * @param end * @return */ public Set<String> zrevrange(String key, long start, long end) { ShardedJedis jedis = null; Set<String> res = null; try { jedis = pool.getResource(); res = jedis.zrevrange(key, start, end); } catch (Exception e) { pool.returnBrokenResource(jedis); e.printStackTrace(); } finally { returnResource(pool, jedis); } return res; }
/** * 获得排序集合 * * @param key * @param startRange * @param endRange * @param orderByDesc * @return */ public Set<String> getSoredSetByRange( String key, int startRange, int endRange, boolean orderByDesc) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); if (orderByDesc) { return shardedJedis.zrevrange(key, startRange, endRange); } else { return shardedJedis.zrange(key, startRange, endRange); } } catch (Exception ex) { logger.error("getSoredSetByRange error.", ex); } finally { returnResource(shardedJedis); } return null; }
public List<String> zQueryByRank(String cacheKey, long startIndex, long endIndex, Order order) throws Exception { ShardedJedis jedis = null; List<String> valueList = null; try { jedis = jedisPool.getResource(); Set<String> valueSet = null; if (order.equals(Order.Asc)) { valueSet = jedis.zrange(cacheKey, startIndex, endIndex); } else { valueSet = jedis.zrevrange(cacheKey, startIndex, endIndex); } valueList = new ArrayList<String>(valueSet); } catch (Exception e) { throw e; } finally { if (jedis != null) { jedisPool.returnResource(jedis); } } return valueList; }