redis有序集合

彥楠發表於2019-07-02

**

1,新增或更新有序集合

**

	/**
	 * 
	 * 修改有序集合 key member的值
	   	Redis Zincrby 命令對有序集合中指定成員的分數加上增量 increment

		可以通過傳遞一個負數值 increment ,讓分數減去相應的值,比如 ZINCRBY key -5 member ,就是讓 member 的 score 值減去 5 。
		
		當 key 不存在,或分數不是 key 的成員時, ZINCRBY key increment member 等同於 ZADD key increment member 。
		
		當 key 不是有序集型別時,返回一個錯誤。
		
		分數值可以是整數值或雙精度浮點數。
	 * @param key
	 * @param increment
	 * @param member
	 */
	protected static Double zincrby(String key, double increment, String member) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.zincrby(key, increment, member);
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
			
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage());
				// TODO Auto-generated catch block
				
			}
		} 
		return null;
	}

2,查詢有序集合

/**
	 * 顯示有序集 名字  分數  由大到小
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	protected static Set<Tuple> rangeWithScores(String key, long start, long end) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.zrevrangeWithScores(key, start, end);
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
			
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage());
				// TODO Auto-generated catch block
				
			}
		}
		return null;
	}

3,查詢有序集合總數

/**
	 * 查詢zadd的總數
	 * @param key
	 * @return
	 */
	protected static long ZCARD(String key) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.zcard(key);
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
			
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage());
				// TODO Auto-generated catch block
				
			}
		}
		
		return 0;
	}

**

4,移除有序集合

**

/**
	 * 移除 有序集合
	 * 
	 * @param key
	 * @param members
	 * return 0,移除不存在元素,1移除一個元素
	 */
	protected static long ZREM(String key, String members) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.zrem(key, members);
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
			
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage());
				// TODO Auto-generated catch block
				
			}
		}
		return 0;
	}

5,移除有序集中,指定分數(score)區間內的所有成員

/**
	 * Redis Zremrangebyscore 命令用於移除有序集中,指定分數(score)區間內的所有成員。
	 */
	protected static Long zremrangebyscore(String key, double start, double end) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.zremrangeByScore(key, start, end);
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
			
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage());
				// TODO Auto-generated catch block
				
			}
		}
		return 0L;
	}

**

6,獲取排名

**

/**
	 * 
	 * @param key
	 * @param member
	 * @return 排名
	 */
	protected static Long rank(String key, String member) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			Long zk = jedis.zrevrank(key, member);
			return zk;
		} catch (Exception e) {
			logger.info("redis bug:" + e.getMessage());
		} finally {
			try {
				ConnectionManager.closeConnection(jedis);
			} catch (Exception e) {
				logger.info("redis bug:" + e.getMessage()); 				
				logger.info(" kkkredis 關閉jedis異常" + e.getMessage());
			}
		}
	
		return -1l;
	}

相關文章