|
@@ -1,103 +0,0 @@
|
|
|
-package com.llisoft.service.file.util;
|
|
|
-
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import com.llisoft.common.util.JsonUtil;
|
|
|
-
|
|
|
-/**
|
|
|
- * redis处理
|
|
|
- * @author YangJie [2018年12月26日]
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class RedisUtil {
|
|
|
-
|
|
|
- private static StringRedisTemplate redisTemplate;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- public RedisUtil(StringRedisTemplate redisTemplate) {
|
|
|
- RedisUtil.redisTemplate = redisTemplate;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String get(String key) {
|
|
|
- return Objects.isNull(key) ? null : redisTemplate.opsForValue().get(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取
|
|
|
- * @param key
|
|
|
- * @param clazz 目标对象类型
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> T get(String key, Class<T> clazz) {
|
|
|
- String value = get(key);
|
|
|
- return Objects.isNull(value) ? null : JsonUtil.toObject(value, clazz);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过正则获取列表
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Set<String> gets(String pattern) {
|
|
|
- return redisTemplate.keys(pattern);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取与 key 对应的对象
|
|
|
- * @param key
|
|
|
- * @param clazz 目标对象类型
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static long getExpire(String key) {
|
|
|
- return redisTemplate.getExpire(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 保存
|
|
|
- * @param key
|
|
|
- * @param obj
|
|
|
- */
|
|
|
- public static void set(String key, Object obj) {
|
|
|
- redisTemplate.opsForValue().set(key, JsonUtil.toJson(obj));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 保存
|
|
|
- * @param key
|
|
|
- * @param obj
|
|
|
- * @param timeout 超时时间
|
|
|
- * @param unit 时间单位
|
|
|
- */
|
|
|
- public static void set(String key, Object obj, long timeout, TimeUnit unit) {
|
|
|
- redisTemplate.opsForValue().set(key, JsonUtil.toJson(obj), timeout, unit);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除
|
|
|
- * @param key
|
|
|
- */
|
|
|
- public static void delete(String key) {
|
|
|
- redisTemplate.delete(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除
|
|
|
- * @param key
|
|
|
- */
|
|
|
- public static void deletes(String pattern) {
|
|
|
- redisTemplate.delete(gets(pattern));
|
|
|
- }
|
|
|
-
|
|
|
-}
|