redis队列 list

1,添加队列

/**
	 * add list
	 * 
	 * @param key
	 * @param member
	 */
	protected static long lpush(String key, String member) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.lpush(key, 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 0;
	}

2,查询队列

/**
	 * 查询队列
	 * @param key
	 * @param start   开始位置
	 * @param end	结束位置
	 * @return
	 */
	protected static List lrange(String key,int start,int end) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			List ls=jedis.lrange(key, start, end);
			return ls;
		} 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,移除最先插入到队列的值

/**
	 * 移除最先插入到队列的值
	 * @param key
	 * @return 被移除的值
	 */
	protected static String rpop(String key) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			return jedis.rpop(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 null;
	}

4,获取队列长度

	/**
	 * 获取队列长度
	 * 
	 * @param key
	 * @param member
	 */
	protected static long llen(String key) {
		Jedis jedis = null;
		try {
			jedis = ConnectionManager.getConnection();
			Long l=jedis.llen(key);
			return l;
		} 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;
	}

你可能感兴趣的:(java,redis)