/**
*
*
* 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);
}
}