import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @Description Redis工具类
* @Author WangKun
* @Date 2023/2/24 15:43
* @Version
*/
@Component
public class RedisTemplateUtil {
// @Autowired
private static RedisTemplate<String, Object> redisTemplate;
// 采用set方法注入
@Autowired
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate){
RedisTemplateUtil.redisTemplate = redisTemplate;
}
/**
* @param key
* @param time 时间(秒)
* @Description key指定失效时间
* @Throws
* @Return boolean
* @Date 2023-02-24 15:46:15
* @Author WangKun
*/
public static boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @param key
* @Description 根据key 获取过期时间
* @Throws
* @Return long 时间(秒) 返回0代表为永久有效
* @Date 2023-02-24 15:47:54
* @Author WangKun
*/
public static long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* @param key
* @Description 判断key是否存在
* @Throws
* @Return boolean true:存在,false:不存在
* @Date 2023-02-24 15:48:43
* @Author WangKun
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @param key 多个使用逗号隔开
* @Description 删除缓存
* @Throws
* @Return void
* @Date 2023-02-24 15:50:24
* @Author WangKun
*/
@SuppressWarnings("unchecked")
public static void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
}
}
}
/**
* @param key
* @Description 获取缓存值
* @Throws
* @Return java.lang.Object
* @Date 2023-02-24 15:51:21
* @Author WangKun
*/
public static Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* @param key
* @param clazz
* @Description 获取缓存值, 指定类型
* @Throws
* @Return T
* @Date 2023-02-24 15:53:45
* @Author WangKun
*/
public static <T> T get(String key, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
Object v = get(key);
if (v == null) return null;
return mapper.convertValue(v, clazz);
}
/**
* @param key
* @param clazz 实体类型
* @Description 获取缓存值, 指定类型
* @Throws
* @Return java.util.List<T>
* @Date 2023-02-27 17:57:13
* @Author WangKun
*/
public static <T> List<T> get2(String key, Class<T> clazz) {
List<T> result = new ArrayList<T>();
ObjectMapper objectMapper = new ObjectMapper();
Object obj = get(key);
if (obj instanceof ArrayList<?>) {
for (Object o : (List<?>) obj) {
result.add(objectMapper.convertValue(o, clazz));
}
}
return result;
}
/**
* @param key 键 value 值
* @Description 缓存放入
* @Throws
* @Return true成功 false失败
* @Date 2023-02-24 15:52:11
* @Author WangKun
*/
public static boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @param key 键 value 值 time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @Description 缓存放入并设置时间
* @Throws
* @Return
* @Date 2023-02-24 15:54:58
* @Author WangKun
*/
public static boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Redis常用工具封装(RedisTemplate)
最新推荐文章于 2024-03-28 13:04:20 发布