redis队列工具类

/**
 *
 *
 * This software code is only used for the authorized 
 * person to maintain and two development. It is not 
 * allowed to be issued to the outside world. It is 
 * necessary to strictly observe the confidentiality 
 * agreement we have signed.
 * copyright by 1sdk.cn
 *
 */
package com.sf.chsdk.sftp.redis;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;

/**
 * @author huangxianbo
 * @date 2019年11月26日
 * @version V1.0
 */
public class SimpleRedisClient {
	/** redis服务器IP */
	private static final String redisIp = "服务器地址";
	/** redis服务器端口 */
	private static final int redisPort = 6379;
	
	private final static Logger logger = LoggerFactory.getLogger(SimpleRedisClient.class);
	
	/**
	 *   向Redis队列中写入数据(从左侧放数据)
	 * @param key
	 * @param value
	 */
	public static void writeRedisQueue(String key, String value) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		jedis.lpush(key, value);
		jedis.close();
	}
	
	/**
	 *  反向向Redis队列中写入数据(从右侧放数据)
	 * @param key
	 * @param value
	 */
	public static void writeRedisQueueReverse(String key, String value) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		jedis.rpush(key, value);
		jedis.close();
	}
	
	/**
	 *  从队列中读数据(右侧读数据)
	 * @param key
	 * @return
	 */
	public static String readRedisQueue(String key) {
		// 创建jedis实例,传入当前redis的IP地址和端口号
		Jedis jedis = new Jedis(redisIp, redisPort);
		String result = jedis.rpop(key);
		jedis.close();
		return result;
	}
	
	/**
	 *   移除队列中的value值
	 * @param key 
	 * @param count 0:队列中有相同的value则全部移除
	 * @param value
	 * @return
	 */
	public static Long removeObjRedisQueueByValue(String key, long count, String value) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		Long result = jedis.lrem(key, count, value);
		jedis.close();
		return result;
	}
	
	/**
	 *  根据队列中指定的key和范围取数据
	 * @param key
	 * @param startIndex
	 * @param endIndex
	 * @return
	 */
	public static List<String> readRedisQueueByRange(String key, long startIndex, long endIndex) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		List<String> result = jedis.lrange(key, startIndex, endIndex);
		jedis.close();
		return result;
	}
	
	/**
	 *  获取队列的长度
	 * @param key
	 * @return
	 */
	public static Long llen(String key) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		Long result = jedis.llen(key);
		jedis.close();
		return result;
	}
	
	public static boolean existsRedis(String key) {
		Jedis jedis = new Jedis(redisIp, redisPort);
		return (null != jedis.get(key));
	}
	
	public static <T> T parseObject(String text, Class<T> clazz) {
		return JSON.parseObject(text, clazz);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值