redis 缓存问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.sqx.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.sqx.modules.redisService.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -9,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -43,7 +45,30 @@ public class RedisUtils {
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
*/
|
||||
public <T> T getDate(String key, Class<T> clazz,String method) {
|
||||
public <T> List<T> getListData(String key, Class<T> clazz,String method) {
|
||||
String jsonStr = getDate(key, method);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
// 将JSON字符串转换为List<T>
|
||||
return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存里的数据 如果不存在 则插入 并返回
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
*/
|
||||
public <T> T getObjectDate(String key, Class<T> clazz,String method) {
|
||||
String jsonStr = getDate(key, method);
|
||||
return this.fromJson(jsonStr, clazz);
|
||||
}
|
||||
|
||||
public String getDate(String key, String method) {
|
||||
if (!this.hasKey(key)) {
|
||||
try {
|
||||
// 获取Lookup对象
|
||||
@@ -61,7 +86,7 @@ public class RedisUtils {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return this.get(key, clazz);
|
||||
return this.get(key);
|
||||
}
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
|
||||
@@ -54,10 +54,10 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
||||
|
||||
@Autowired
|
||||
public DiscSpinningServiceImpl(CommonInfoService commonRepository,
|
||||
UserMoneyDetailsService userMoneyDetailsService, CashOutService cashOutService,
|
||||
OrdersService ordersService, DiscSpinningRecordService recordService,
|
||||
UserMoneyService userMoneyService, UserService userService,
|
||||
DiscSpinningAmountService amountService,RedisUtils redisUtils
|
||||
UserMoneyDetailsService userMoneyDetailsService, CashOutService cashOutService,
|
||||
OrdersService ordersService, DiscSpinningRecordService recordService,
|
||||
UserMoneyService userMoneyService, UserService userService,
|
||||
DiscSpinningAmountService amountService, RedisUtils redisUtils
|
||||
) {
|
||||
this.commonRepository = commonRepository;
|
||||
this.amountService = amountService;
|
||||
@@ -146,7 +146,7 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
||||
} while (randomDouble == 0);
|
||||
BigDecimal randomNum = new BigDecimal(randomDouble).multiply(new BigDecimal(10000)).divide(new BigDecimal(100));
|
||||
|
||||
List<DiscSpinningAmount> amounts = amountService.list(new QueryWrapper<DiscSpinningAmount>().eq("status",1).orderByAsc("max_amount"));
|
||||
List<DiscSpinningAmount> amounts = redisUtils.getListData(RedisKeys.getDateKey("spinning:amount"), DiscSpinningAmount.class, "setDiscSpinningAmounts");
|
||||
for (DiscSpinning prize : prizes) {
|
||||
if (randomNum.compareTo(prize.getNumber()) < 0) {
|
||||
if (prize.getType() == 2) {
|
||||
@@ -184,6 +184,5 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
||||
recordService.save(record);
|
||||
return record;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user