redis 方法封装
抽奖金额概率 配置 banner 跳转类型
This commit is contained in:
@@ -9,4 +9,8 @@ public class RedisKeys {
|
||||
public static String getSysConfigKey(String key){
|
||||
return "sys:config:" + key;
|
||||
}
|
||||
|
||||
public static String getDateKey(String key){
|
||||
return "date:" + key;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.sqx.common.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.sqx.modules.redisService.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -13,6 +17,8 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtils {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
@@ -31,6 +37,37 @@ public class RedisUtils {
|
||||
public final static long NOT_EXPIRE = -1;
|
||||
private final static Gson Gson = new Gson();
|
||||
|
||||
/**
|
||||
* 获取缓存里的数据 如果不存在 则插入 并返回
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
*/
|
||||
public <T> T getDate(String key, Class<T> clazz,String method) {
|
||||
if (!this.hasKey(key)) {
|
||||
try {
|
||||
// 获取Lookup对象
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
// 构建方法类型(这里假设方法无参数,返回类型为void)
|
||||
MethodType methodType = MethodType.methodType(void.class , String.class);
|
||||
// 获取方法句柄
|
||||
MethodHandle methodHandle = lookup.findVirtual(redisService.getClass(), method, methodType);
|
||||
// 调用方法句柄
|
||||
methodHandle.invoke(redisService,key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return this.get(key, clazz);
|
||||
}
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
public void set(String key, Object value, long expire){
|
||||
valueOperations.set(key, toJson(value));
|
||||
if(expire != NOT_EXPIRE){
|
||||
|
||||
Reference in New Issue
Block a user