This commit is contained in:
guohb
2024-06-04 16:28:08 +08:00
parent 148ce575e8
commit d114275c7e
6 changed files with 20 additions and 7 deletions

View File

@@ -25,8 +25,8 @@ public class CommonConstants {
public static final int NORMAL_LOCK_TIMES = 60 * 1000; public static final int NORMAL_LOCK_TIMES = 60 * 1000;
public static final int AN_HOUR_SECONDS = 3600; public static final int AN_HOUR_SECONDS = 3600;
//十秒
public static final int TEN_SECONDS = 10; public static final int TEN_SECONDS = 10000;
/** /**
* 跟进任务通知缓存时间 1小时 * 跟进任务通知缓存时间 1小时

View File

@@ -321,6 +321,16 @@ public class RedisUtilPool {
public boolean setNxExpire(final String key, final String value, final int expire) { public boolean setNxExpire(final String key, final String value, final int expire) {
String res = new Executor<String>(shardedJedisPool) { String res = new Executor<String>(shardedJedisPool) {
@Override @Override
/**
* 参数定义详解,避免用错:
* set(String key, String value, String nxxx, String expx, int time)
* @key key
* @value value
* @nxxx 1.nx not exists, 只有key 不存在时才把key value set 到redis
* 2.xx is exists ,只有 key 存在是才把key value set 到redis
* @expx 1.ex seconds 秒; 2.px : milliseconds 毫秒
* 我们当前选用的是 nx,px,即key不存在时才set且过期时间为毫秒
*/
String execute() { String execute() {
return jedis.set(key, value, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expire); return jedis.set(key, value, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expire);
} }

View File

@@ -2,6 +2,7 @@ package com.cool.store.service.impl;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.LoginUserInfo; import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.EnterpriseUserDAO; import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.RegionDao; import com.cool.store.dao.RegionDao;
@@ -96,7 +97,7 @@ public class ApplyLicenseServiceImpl implements ApplyLicenseService {
String lockKey = "submitLicense:" + request.getShopId(); String lockKey = "submitLicense:" + request.getShopId();
String lockValue = UUID.randomUUID().toString(); String lockValue = UUID.randomUUID().toString();
boolean acquired = Boolean.FALSE; boolean acquired = Boolean.FALSE;
acquired = redisUtilPool.setNxExpire(lockKey, lockValue, 10); acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
if (!acquired) { if (!acquired) {
throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION); throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
} }

View File

@@ -1,6 +1,7 @@
package com.cool.store.service.impl; package com.cool.store.service.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.LoginUserInfo; import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.LineInfoDAO; import com.cool.store.dao.LineInfoDAO;
import com.cool.store.entity.*; import com.cool.store.entity.*;
@@ -203,7 +204,7 @@ public class IntentAgreementServiceImpl extends LineFlowService implements Inten
String lockValue = UUID.randomUUID().toString(); String lockValue = UUID.randomUUID().toString();
boolean acquired = false; boolean acquired = false;
try { try {
acquired = redisUtilPool.setNxExpire(lockKey, lockValue, 10); acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
if (Boolean.TRUE.equals(acquired)) { if (Boolean.TRUE.equals(acquired)) {
Map<String, Object> requestMap = new HashMap<>(); Map<String, Object> requestMap = new HashMap<>();
fillSignatureInfo(requestMap); fillSignatureInfo(requestMap);
@@ -269,7 +270,7 @@ public class IntentAgreementServiceImpl extends LineFlowService implements Inten
String lockValue = UUID.randomUUID().toString(); String lockValue = UUID.randomUUID().toString();
boolean acquired = false; boolean acquired = false;
try { try {
acquired = redisUtilPool.setNxExpire(lockKey, lockValue, 10); acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
if (Boolean.TRUE.equals(acquired)) { if (Boolean.TRUE.equals(acquired)) {
Map<String, Object> requestMap = new HashMap<>(); Map<String, Object> requestMap = new HashMap<>();
fillSignatureInfo(requestMap); fillSignatureInfo(requestMap);

View File

@@ -759,7 +759,7 @@ public class PointServiceImpl implements PointService {
public Integer lineSelectPoint(SelectPointRequest request) { public Integer lineSelectPoint(SelectPointRequest request) {
Long pointId = request.getPointId(), shopId = request.getShopId(), lineId = request.getLineId(); Long pointId = request.getPointId(), shopId = request.getShopId(), lineId = request.getLineId();
String lockKey = MessageFormat.format(POINT_SELECT_KEY, enterpriseId, pointId); String lockKey = MessageFormat.format(POINT_SELECT_KEY, enterpriseId, pointId);
if(!redisUtilPool.setNxExpire(lockKey, JSONObject.toJSONString(request), 30)){ if(!redisUtilPool.setNxExpire(lockKey, JSONObject.toJSONString(request), 30*1000)){
throw new ServiceException(ErrorCodeEnum.POINT_IS_LOCK); throw new ServiceException(ErrorCodeEnum.POINT_IS_LOCK);
} }
try { try {

View File

@@ -2,6 +2,7 @@ package com.cool.store.service.impl;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.LoginUserInfo; import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.ShopStageInfoDAO; import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.entity.*; import com.cool.store.entity.*;
@@ -97,7 +98,7 @@ public class SysStoreAppServiceImpl implements SysStoreAppService, AuditResultSe
String lockValue = UUID.randomUUID().toString(); String lockValue = UUID.randomUUID().toString();
boolean acquired = false; boolean acquired = false;
try { try {
acquired = redisUtilPool.setNxExpire(lockKey, lockValue, 10); acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
if (Boolean.TRUE.equals(acquired)) { if (Boolean.TRUE.equals(acquired)) {
//1.操作数据库 //1.操作数据库
SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.convertToSystemBuildingShopDO(request); SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.convertToSystemBuildingShopDO(request);