相关代码如下:
package com.vip.space.service;
import com.vip.space.log.VenusAuditLoggerFactory;
import com.vip.venus.log.VenusLogger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.*;
import redis.clients.jedis.params.SetParams;
import scala.Tuple2;
import java.time.Duration;
import java.util.*;
@Service
public class RedisService {
private static VenusLogger logger = VenusAuditLoggerFactory.getLogger(RedisService.class);
private static final String REDIS_LOCK_PRE = "space_redis_key_";
private static final int REDIS_LOCK_TIMEOUT_MS = 10 * 60 * 1000;
private static final String LOCK_SUCCESS = "OK";
private static final String REDIS_RELEASE_LOCK_SCRIPT
= "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
private static JedisCluster jedis = null;
//可用连接实例的最大数目,默认为8;
//如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
private static Integer MAX_TOTAL = 2000;
//控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
private static Integer MAX_IDLE = 200;
//等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
//如果超过等待时间,则直接抛出JedisConnectionException
private static Integer MAX_WAIT_MILLIS = 1000 * 30;
//在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
//如果为true,则得到的jedis实例均是可用的
private static Boolean TEST_ON_BORROW = true;
//在空闲时检查有效性, 默认false
private static Boolean TEST_WHILE_IDLE = true;
//是否进行有效性检查
private static Boolean TEST_ON_RETURN = true;
@Value("${redis.nodes}")
private String redisNodes;
// public RedisUtils() {
// this.redisNodes = redisNodes;
// createRedisConn();
// }
public void createRedisConn(){
try {
if (jedis == null) {
synchronized (this) {
if (jedis == null) {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(MAX_TOTAL);
poolConfig.setMaxIdle(MAX_IDLE);
poolConfig.setMaxWait(Duration.ofMillis(MAX_WAIT_MILLIS));
poolConfig.setTestOnBorrow(TEST_ON_BORROW);
poolConfig.setTestWhileIdle(TEST_WHILE_IDLE);
poolConfig.setTestOnReturn(TEST_ON_RETURN);
Set jedisClusterNode = new HashSet();
if (StringUtils.isNotEmpty(redisNodes)) {
String[] nodes = redisNodes.split(",");
for (int i = 0; i <= nodes.length - 1; i++) {
String[] node = nodes[i].split(":");
String host = node[0];
int port = Integer.valueOf(node[1]);
jedisClusterNode.add(new HostAndPort(host, port));
}
}
jedis = new JedisCluster(jedisClusterNode,
1000, 1000, 5, poolConfig);
logger.info("Create redis connection success for " + redisNodes);
}
}
}
} catch (Exception e) {
logger.error("Exceptions thrown while create jedis, error is " + e.getMessage(), e);
}
}
public JedisCluster getJedis() {
if (jedis == null) {
logger.info("Jedis connection is lost, need to reconnect " + redisNodes);
createRedisConn();
}
return jedis;
}
public static void close() {
jedis.close();
}
public static String get(final String key) {
String value = null;
try {
value = jedis.get(key);
} catch (Exception e) {
logger.error("get redis fail " + key, e);
}
return value;
}
public static Set getAllKeys(String pattern) {
Set keys = null;
try {
keys = jedis.keys(pattern);
} catch (Exception e) {
logger.error(pattern + " get All keys fail " + e.getMessage(), e);
}
return keys;
}
public static void set(final String key, final String value) {
try {
jedis.set(key, value);
} catch (Exception e) {
logger.error("set key [" + key + "] value fail " + e.getMessage(), e);
}
}
public static void rename(String oldKey, String newKey) {
try {
if (jedis.exists(oldKey)) {
jedis.rename(oldKey, newKey);
}
} catch (Exception e) {
logger.error(oldKey + " rename new key " + newKey + " fail " + e.getMessage(), e);
}
}
public static void lrem(String key, String value) {
try {
jedis.lrem(key, 0, value);
} catch (Exception e) {
logger.error(key + " lrem fail " + e.getMessage(), e);
}
}
public static void removeKey(String key) {
try {
jedis.del(key);
} catch (Exception e) {
logger.error(key + " remove fail " + e.getMessage() , e);
}
}
public static boolean exists(String key) {
try {
return jedis.exists(key);
} catch (Exception e) {
logger.error(key + " exists fail " + e.getMessage(), e);
return false;
}
}
public static void subscribe(final JedisPubSub jedisPubSub, final String... channels) {
try {
jedis.subscribe(jedisPubSub, channels);
} catch (Exception e) {
logger.error("subscribe operation fail " + e.getMessage(), e);
}
}
public static String hmset(final String key, final Map hash) {
String result = null;
try {
jedis.hmset(key, hash);
} catch (Exception e) {
logger.error(key + " hmset fail " + e.getMessage(), e);
}
return result;
}
public static Map hmget(final String key) {
Map result = null;
try {
result = jedis.hgetAll(key);
} catch (Exception e) {
logger.error(key + " hmget fail " + e.getMessage(), e);
}
return result;
}
public static List hmget(final String key, final String... fields) {
List result = null;
try {
result = jedis.hmget(key, fields);
} catch (Exception e) {
logger.error(key + " hmget fail " + e.getMessage(), e);
}
return result;
}
public static String hget(String key, String field) {
String result = null;
try {
result = jedis.hget(key, field);
} catch (Exception e) {
logger.error(key + " hget fail " + e.getMessage(), e);
}
return result;
}
public static void hset(String key, String field, String value) {
try {
jedis.hset(key, field, value);
} catch (Exception e) {
logger.error(key + " hset fail " + e.getMessage(), e);
}
}
public static Long publish(final String channel, final String message) {
Long result = null;
try {
result = jedis.publish(channel, message);
} catch (Exception e) {
logger.error( channel + " publish fail " + e.getMessage(), e);
}
return result;
}
public static Set hkeys(final String key) {
Set result = null;
try {
result = jedis.hkeys(key);
} catch (Exception e) {
logger.error(key + " hkey fail " + e.getMessage(), e);
}
return result;
}
public static Long del(String key) {
Long result = null;
try {
result = jedis.del(key);
} catch (Exception e) {
logger.error(key + " del fail " + e.getMessage(), e);
}
return result;
}
/**
* 清除指定前缀的cache key
*/
public static void delPrefix(String key) {
try {
Set keySet = jedis.keys(key + "*");
for (String k : keySet) {
jedis.del(k);
}
} catch (Exception e) {
logger.error("del prefix key " + key + " fail " + e.getMessage(), e);
}
}
public static Long llen(final String key) {
Long result = null;
try {
result = jedis.llen(key);
} catch (Exception e) {
logger.error(key + " llen fail " + e.getMessage(), e);
}
return result;
}
public static String lindex(final String key, final long index) {
String result = null;
try {
result = jedis.lindex(key, index);
} catch (Exception e) {
logger.error(key + "_" + index + " lindex fail " + e.getMessage(), e);
}
return result;
}
public static boolean lock(String key, String uuid) {
try {
SetParams setParams = new SetParams();
setParams.nx();
setParams.px(REDIS_LOCK_TIMEOUT_MS);
String result = jedis.set(REDIS_LOCK_PRE + key, uuid, setParams);
if (LOCK_SUCCESS.equals(result)) {
return true;
}
return false;
} catch (Exception e) {
logger.error(key +"_" + uuid + " lock fail " + e.getMessage(), e);
}
return false;
}
public static void releaseLock(String key, String uuid) {
try {
jedis.eval(REDIS_RELEASE_LOCK_SCRIPT, Collections.singletonList(REDIS_LOCK_PRE + key),
Collections.singletonList(uuid));
} catch (Exception e) {
logger.error(key + "_" + uuid + " release lock fail " + e.getMessage(), e);
}
}
public static List lrange(final String key) {
try {
Long len = jedis.llen(key);
if (len > 0) {
return jedis.lrange(key, 0, len);
}
} catch (Exception e) {
logger.error(key + " lrange fail " + e.getMessage(), e);
}
return new ArrayList<>();
}
public static Long expire(final String key, final int seconds) {
Long result = null;
try {
result = jedis.expire(key, seconds);
} catch (Exception e) {
logger.error(key + "set expire time " + seconds + " fail " + e.getMessage(), e);
}
return result;
}
public static Long rpush(final String key, final String... strings) {
Long result = null;
try {
result = jedis.rpush(key, strings);
} catch (Exception e) {
logger.error(key + " rpush fail " + e.getMessage(), e);
}
return result;
}
public Tuple2> getSqlResultCache(String redisKey, int pageNum, int pageSize) {
long count = 0;
List result = null;
try {
if (jedis != null && exists(redisKey)) {
count = jedis.llen(redisKey);
result = jedis.lrange(redisKey, getOffset(pageNum, pageSize),
getOffset(pageNum, pageSize) + pageSize - 1);
}
} catch (Exception e) {
logger.error("get query cache result fail ! " + e.getMessage(), e);
} finally {
return new Tuple2<>(count, result);
}
}
public void setSqlResultCache(String redisKey, List data) {
setSqlResultCache(redisKey, data, 60 * 60);
}
public void setSqlResultCache(String redisKey, List data, int expireTimeSec) {
if (jedis != null && !jedis.exists(redisKey) && data != null && data.size() > 0) {
UUID uuid = UUID.randomUUID();
//lock first
try {
logger.info("Cache this key " + redisKey + " to redis and ttl is " + expireTimeSec);
if (lock(redisKey, uuid.toString()) && !jedis.exists(redisKey)) {
jedis.rpush(redisKey, data.toArray(new String[]{}));
jedis.expire(redisKey, expireTimeSec);
}
} finally {
releaseLock(redisKey, uuid.toString());
}
}
}
public void pushStrCache(String redisKey, String data, int expireTimeSec) {
if (jedis != null && !jedis.exists(redisKey) && StringUtils.isNotEmpty(data)) {
UUID uuid = UUID.randomUUID();
//lock first
try {
if (lock(redisKey, uuid.toString()) && !jedis.exists(redisKey)) {
jedis.setex(redisKey, expireTimeSec, data);
}
} finally {
releaseLock(redisKey, uuid.toString());
}
}
}
public int getOffset(int pageNum, int pageSize) {
return (pageNum - 1) * pageSize;
}
}
2、其他地方引用时,只需要执行如下代码即可
@Autowired
public RedisService redisService;
redisService.getJedis().get("is_cas_debug"))