You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tet/snailmall-user-service/src/main/java/com/njupt/swg/cache/CommonCacheUtil.java

128 lines
7.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.njupt.swg.cache;
import com.njupt.swg.common.exception.SnailmallException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* 该类CommonCacheUtil是一个工具类用于与Redis缓存进行交互操作提供了如缓存数据、获取缓存数据、设置带过期时间的缓存以及删除缓存等常用功能。
* 它依赖于JedisPoolWrapper来获取JedisPool对象进而操作Redis缓存并且在操作过程中如果出现异常会抛出SnailmallException异常同时记录相关的日志信息用于排查问题。
* 此类被标记为Spring的@Component组件方便在Spring容器中进行管理和注入使用。
* @Author swg.
* @Date 2019/1/1 15:03
* @CONTACT 317758022@qq.com
* @DESC
*/
@Component
// 该注解表明这个类是Spring框架中的一个组件Spring会自动扫描并将其纳入到容器管理中使其可以被其他需要使用的类进行依赖注入。
@Slf4j
// 使用lombok的@Slf4j注解自动生成一个名为log的日志记录对象方便在类中记录日志信息便于后续调试和查看与Redis交互过程中出现的问题情况。
public class CommonCacheUtil {
@Autowired
// 使用Spring的依赖注入功能自动注入JedisPoolWrapper的实例通过它来获取JedisPool从而能够获取Jedis客户端连接到Redis服务器进行操作。
private JedisPoolWrapper jedisPoolWrapper;
/**
* 缓存永久key的方法用于将指定的键值对存储到Redis缓存中且该缓存数据没有设置过期时间永久有效除非手动删除
*
* @param key 要存储到Redis中的键用于唯一标识缓存数据后续可以通过该键来获取对应的缓存值。
* @param value 要存储到Redis中的值对应键所关联的数据内容可以是各种序列化后的字符串形式的数据如JSON字符串等
*/
public void cache(String key, String value) {
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
// 使用Jedis客户端将指定的键值对存储到Redis中默认存储到Redis的第0个数据库Redis可以有多个数据库通过select方法选择
jedis.set(key, value);
}
}
} catch (Exception e) {
log.error("redis存值失败", e);
// 如果在存储过程中出现异常记录错误日志并抛出SnailmallException异常提示“redis报错”由全局异常处理机制进行处理。
throw new SnailmallException("redis报错");
}
}
/**
* 获取缓存key对应的缓存值的方法用于从Redis缓存中根据指定的键获取对应的值。
*
* @param key 要从Redis中获取值的键通过该键来查找之前存储在Redis中的对应缓存数据。
* @return 返回从Redis中获取到的对应键的值如果键不存在则返回null若获取过程中出现异常会抛出相应异常。
*/
public String getCacheValue(String key) {
String value = null;
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
// 使用Jedis客户端根据指定的键从Redis中获取对应的值默认从Redis的第0个数据库中查找。
value = jedis.get(key);
}
}
} catch (Exception e) {
log.error("redis获取指失败", e);
// 如果在获取过程中出现异常记录错误日志并抛出SnailmallException异常提示“redis报错”由全局异常处理机制进行处理。
throw new SnailmallException("redis报错");
}
return value;
}
/**
* 过期key的方法用于将指定的键值对存储到Redis缓存中并同时设置该缓存数据的过期时间且只有在键不存在时才设置成功原子操作
*
* @param key 要存储到Redis中的键用于唯一标识缓存数据后续可以通过该键来获取对应的缓存值以及判断其是否过期等情况。
* @param value 要存储到Redis中的值对应键所关联的数据内容可以是各种序列化后的字符串形式的数据如JSON字符串等
* @param expire 要设置的缓存数据的过期时间单位为秒表示从存储该缓存数据开始经过指定的秒数后该缓存数据将自动从Redis中删除。
* @return 返回一个长整型结果表示设置操作是否成功若返回1表示设置成功键不存在成功设置了键值对并设置了过期时间若返回0表示设置失败键已存在
*/
public long cacheNxExpire(String key, String value, int expire) {
long result = 0;
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
// 先使用Jedis的setnx方法尝试设置键值对只有在键不存在时才会设置成功返回1若键已存在则设置失败返回0。这是一个原子操作。
result = jedis.setnx(key, value);
// 如果设置键值对成功即setnx返回1再使用expire方法设置该键对应的缓存数据的过期时间单位为秒。
jedis.expire(key, expire);
}
}
} catch (Exception e) {
log.error("redis塞值和设置缓存时间失败", e);
// 如果在设置过程中出现异常记录错误日志并抛出SnailmallException异常提示“redis报错”由全局异常处理机制进行处理。
throw new SnailmallException("redis报错");
}
return result;
}
/**
* 删除缓存key的方法用于从Redis缓存中删除指定的键及其对应的数据。
*
* @param key 要从Redis中删除的键通过该键来定位并删除对应的缓存数据。
*/
public void delKey(String key) {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
try {
// 使用Jedis客户端根据指定的键从Redis中删除对应的缓存数据默认从Redis的第0个数据库中删除。
jedis.del(key);
} catch (Exception e) {
log.error("从redis中删除失败", e);
// 如果在删除过程中出现异常记录错误日志并抛出SnailmallException异常提示“redis报错”由全局异常处理机制进行处理。
throw new SnailmallException("redis报错");
}
}
}
}
}