Skip to content

Commit

Permalink
feat: 缓存设置过期时间
Browse files Browse the repository at this point in the history
  • Loading branch information
oddfar committed Apr 12, 2024
1 parent 7a45518 commit ee385e2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oddfar.campus.business.domain;

/**
* i茅台缓存常量
*
* @author oddfar
* @date 2024/4/12
*/
public interface IMTCacheConstants {
String MT_VERSION = "mt_version";

String MT_SESSION_ID = "mt_session_id";

String MT_SHOP_LIST = "mt_shop_list";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public interface IShopService extends IService<IShop> {
*/
void refreshItem();

/**
* 根据 iShopId 查询门店信息
* <p>
* 预约选择门店时,根据城市、经纬度选择门店
* 之前这些参数是在账号配置里手动填写
* 现在是查询门店的信息,把门店的这些信息填到账号配置里
* 主要是这样避免用户填错信息
*
* @param iShopId
* @return 门店信息
*/
IShop selectByIShopId(String iShopId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.oddfar.campus.business.domain.IMTCacheConstants;
import com.oddfar.campus.business.entity.IUser;
import com.oddfar.campus.business.mapper.IUserMapper;
import com.oddfar.campus.business.service.IMTLogFactory;
Expand Down Expand Up @@ -80,7 +81,7 @@ public void run() {

@Override
public String getMTVersion() {
String mtVersion = Convert.toStr(redisCache.getCacheObject("mt_version"));
String mtVersion = Convert.toStr(redisCache.getCacheObject(IMTCacheConstants.MT_VERSION));
if (StringUtils.isNotEmpty(mtVersion)) {
return mtVersion;
}
Expand All @@ -92,15 +93,15 @@ public String getMTVersion() {
mtVersion = matcher.group(1);
mtVersion = mtVersion.replace("版本 ", "");
}
redisCache.setCacheObject("mt_version", mtVersion);
redisCache.setCacheObject(IMTCacheConstants.MT_VERSION, mtVersion);

return mtVersion;

}

@Override
public void refreshMTVersion() {
redisCache.deleteObject("mt_version");
redisCache.deleteObject(IMTCacheConstants.MT_VERSION);
getMTVersion();
}

Expand Down Expand Up @@ -241,8 +242,9 @@ public void run() {
new Thread(runnable).start();

}

// 领取小茅运
public void receiveReward(IUser iUser){
public void receiveReward(IUser iUser) {
String url = "https://h5.moutai519.com.cn/game/xmTravel/receiveReward";
HttpRequest request = HttpUtil.createRequest(Method.POST, url);

Expand All @@ -256,14 +258,14 @@ public void receiveReward(IUser iUser){
HttpResponse execute = request.execute();
JSONObject body = JSONObject.parseObject(execute.body());

if(body.getInteger("code") != 2000){
if (body.getInteger("code") != 2000) {
String message = "领取小茅运失败";
throw new ServiceException(message);
}
}

public void shareReward(IUser iUser){
logger.info("「领取每日首次分享获取耐力」:"+iUser.getMobile());
public void shareReward(IUser iUser) {
logger.info("「领取每日首次分享获取耐力」:" + iUser.getMobile());
String url = "https://h5.moutai519.com.cn/game/xmTravel/shareReward";
HttpRequest request = HttpUtil.createRequest(Method.POST, url);

Expand All @@ -277,7 +279,7 @@ public void shareReward(IUser iUser){
HttpResponse execute = request.execute();
JSONObject body = JSONObject.parseObject(execute.body());

if(body.getInteger("code") != 2000){
if (body.getInteger("code") != 2000) {
String message = "领取每日首次分享获取耐力失败";
throw new ServiceException(message);
}
Expand Down Expand Up @@ -561,7 +563,7 @@ public void appointmentResults() {
throw new ServiceException(message);
}
JSONArray itemVOs = jsonObject.getJSONObject("data").getJSONArray("reservationItemVOS");
if(Objects.isNull(itemVOs) || itemVOs.isEmpty()){
if (Objects.isNull(itemVOs) || itemVOs.isEmpty()) {
logger.info("申购记录为空: user->{}", iUser.getMobile());
continue;
}
Expand All @@ -574,7 +576,7 @@ public void appointmentResults() {
}
}
} catch (Exception e) {
logger.error("查询申购结果失败:失败原因->{}", e.getMessage(),e);
logger.error("查询申购结果失败:失败原因->{}", e.getMessage(), e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oddfar.campus.business.domain.IMTCacheConstants;
import com.oddfar.campus.business.domain.IMTItemInfo;
import com.oddfar.campus.business.domain.MapPoint;
import com.oddfar.campus.business.entity.IItem;
Expand All @@ -18,8 +19,7 @@
import com.oddfar.campus.common.core.RedisCache;
import com.oddfar.campus.common.exception.ServiceException;
import com.oddfar.campus.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -33,9 +33,9 @@
import java.util.stream.Collectors;

@Service
@Slf4j
public class IShopServiceImpl extends ServiceImpl<IShopMapper, IShop> implements IShopService {

private static final Logger logger = LoggerFactory.getLogger(IShopServiceImpl.class);

@Autowired
IShopMapper iShopMapper;
Expand All @@ -48,7 +48,7 @@ public class IShopServiceImpl extends ServiceImpl<IShopMapper, IShop> implements
@Override
public List<IShop> selectShopList() {

List<IShop> shopList = redisCache.getCacheList("mt_shop_list");
List<IShop> shopList = redisCache.getCacheList(IMTCacheConstants.MT_SHOP_LIST);

if (shopList != null && shopList.size() > 0) {
return shopList;
Expand All @@ -74,7 +74,7 @@ public void refreshShop() {
String shopUrl = body.getJSONObject("data").getJSONObject("mtshops_pc").getString("url");
//清空数据库
iShopMapper.truncateShop();
redisCache.deleteObject("mt_shop_list");
redisCache.deleteObject(IMTCacheConstants.MT_SHOP_LIST);

String s = HttpUtil.get(shopUrl);

Expand All @@ -88,12 +88,13 @@ public void refreshShop() {
list.add(iShop);
}
this.saveBatch(list);
redisCache.setCacheList("mt_shop_list", list);
redisCache.setCacheList(IMTCacheConstants.MT_SHOP_LIST, list);
redisCache.expire(IMTCacheConstants.MT_SHOP_LIST, 2, TimeUnit.HOURS);
}

@Override
public String getCurrentSessionId() {
String mtSessionId = Convert.toStr(redisCache.getCacheObject("mt_session_id"));
String mtSessionId = Convert.toStr(redisCache.getCacheObject(IMTCacheConstants.MT_SESSION_ID));

long dayTime = LocalDate.now().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
if (StringUtils.isNotEmpty(mtSessionId)) {
Expand All @@ -107,7 +108,7 @@ public String getCurrentSessionId() {
if (jsonObject.getString("code").equals("2000")) {
JSONObject data = jsonObject.getJSONObject("data");
mtSessionId = data.getString("sessionId");
redisCache.setCacheObject("mt_session_id", mtSessionId);
redisCache.setCacheObject(IMTCacheConstants.MT_SESSION_ID, mtSessionId, 2, TimeUnit.HOURS);

iItemMapper.truncateItem();
//item插入数据库
Expand All @@ -126,7 +127,7 @@ public String getCurrentSessionId() {

@Override
public void refreshItem() {
redisCache.deleteObject("mt_session_id");
redisCache.deleteObject(IMTCacheConstants.MT_SESSION_ID);
getCurrentSessionId();
}

Expand All @@ -138,7 +139,6 @@ public IShop selectByIShopId(String iShopId) {
} else {
return null;
}
// return iShopMapper.selectOne(IShop::getIShopId, iShopId);
}

@Override
Expand Down Expand Up @@ -167,14 +167,14 @@ public List<IMTItemInfo> reGetShopsByProvince(String province, String itemId) {
res = JSONObject.parseObject(urlRes);
} catch (JSONException jsonException) {
String message = StringUtils.format("查询所在省市的投放产品和数量error: %s", url);
logger.error(message);
log.error(message);
throw new ServiceException(message);
}

// JSONObject res = JSONObject.parseObject(HttpUtil.get(url));
if (!res.containsKey("code") || !res.getString("code").equals("2000")) {
String message = StringUtils.format("查询所在省市的投放产品和数量error: %s", url);
logger.error(message);
log.error(message);
throw new ServiceException(message);
}
//组合信息
Expand Down

0 comments on commit ee385e2

Please sign in to comment.