功能介绍
Redis公共工具类 可操作任意类型 将操作缓存与数据库繁琐的代码步骤聚合封装起来 使用简单简洁 且redis数据为空时可取数据库并同步添加缓存(配合daos)
引入依赖
com.alibaba
fastjson
1.2.74
org.springframework.boot
spring-boot-starter-data-redis
创建Daos
/**
* @author ZS
* @date 2021/8/12 9:55
* Detail: 此工具类配合redisClient工具类使用 redis有值则此方法不执行 redis查询数据为空时 此工具类则作为第二选择执行dao方法
*/
@Component
public class Daos {
@Autowired
private TraderUserDao traderUserDao;
@Autowired
private TraderDao traderDao;
@Autowired
private CouponDao couponDao;
@Autowired
private CouponParkingDao couponParkingDao;
@Autowired
private RoadsideParkNumberDao roadsideParkNumberDao;
@Autowired
private RoadsideCityDao roadsideCityDao;
@Autowired
private CarparkDao carparkDao;
@Autowired
private TraderCouponParkingDao traderCouponParkingDao;
@Autowired
private TraderCouponBuyDao traderCouponBuyDao;
@Autowired
private TraderCouponUsedDao traderCouponUsedDao;
public Object query(String key) {
//redis的key以下划线分割 需要满足格式为:前缀_id 例:"user_1"
String[] s = key.split("_");
//分割之后id需要确保没有其他_下划线
int id = Integer.parseInt(s[1]);
try {
//通过前缀匹配对应的dao查询结果 此时返回Object 之后RedisClient会转为目标类型
switch (s[0]) {
case "Coupon":
return couponDao.findById(id).get();
case "TraderCouponBuy":
return traderCouponBuyDao.findById(id).get();
case "TraderCouponUsed":
return traderCouponUsedDao.findById(id).get();
case "RoadsideParknumber":
return roadsideParkNumberDao.findById(id).get();
case "CouponParkingBySaas":
return couponParkingDao.findBySaasId(id);
default:
return null;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
创建RedisClient
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author ZS
* @date 2021/7/16 9:30
* Detail: Redis缓存工具类 可操作任意类型 且redis数据为空时可查询数据库并添加缓存(配合Daos)
*/
@Component
public class RedisClient {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private Daos daos;
/**
* 使用方法: 指定clazz返回类型 如果redis为空 则执行daos
*
* @param key RedisKey
* @param clazz 实体类t
* @param
泛型: 任意类型 * @return 实体类
*/
public
T getData(String key, Class clazz, int time, TimeUnit timeUnit) { //先从redis查询
String s = stringRedisTemplate.opsForValue().get(key);
T t = null;
if (!StringUtils.isEmpty(s)) {
t = JSON.parseObject(s, clazz);
} else {
try {//redis为空 查询数据库
t = (T) daos.query(key);
if (t != null) {
stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(t), time, timeUnit);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return t;
}
//返回集合
public
List getDataList(String key, Class clazz, int time, TimeUnit timeUnit) { //先从redis查询
String s = stringRedisTemplate.opsForValue().get(key);
List
t = null; if (!StringUtils.isEmpty(s)) {
t = JSON.parseArray(s, clazz);
} else {
try {//redis为空 查询数据库
t = (List
) daos.query(key); if (!CollectionUtils.isEmpty(t)) {
stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(t));
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return t;
}
//返回json字符串
public String getData(String key) {
String s = stringRedisTemplate.opsForValue().get(key);
return s;
}
public void setData(String key, String value, int time, TimeUnit timeUnit) {
stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
}
public void deleteData(String key) {
if (exists(key))
stringRedisTemplate.delete(key);
}
public boolean exists(String key) {
Boolean flag = stringRedisTemplate.hasKey(key);
return flag;
}
}
使用代码示例
//单个对象
String orderKey = "roadsideOrder_" + roadsideParknumber.getOrderId();
RoadsideOrder order = redisClient.getData(orderKey, RoadsideOrder.class, 1, TimeUnit.DAYS);
//集合对象
List
//json字符串
String s = redisClient.getData(key);
//设置缓存
String orderKey = "roadsideOrder_" + roadsideParknumber.getOrderId();
redisClient.setData(orderKey , JSON.toJSONString(order));
//设置缓存并添加过期时间
String orderKey = "roadsideOrder_" + roadsideParknumber.getOrderId();
redisClient.setData(orderKey , JSON.toJSONString(order), 1, TimeUnit.DAYS);
//删除缓存
redisClient.deleteData(key)
//是否存在
redisClient.exists(key)