Selaa lähdekoodia

api次数逻辑优化

dsx 2 vuotta sitten
vanhempi
commit
559a09aa23

+ 1 - 1
src/main/java/com/fdkankan/openApi/service/system/IUserAuthInfoService.java

@@ -33,7 +33,7 @@ public interface IUserAuthInfoService extends IService<UserAuthInfo> {
 
     void checkValidAppKey(String appKey);
 
-    void setReidsCount(String appKey, Integer count, boolean isInfinite);
+    void setReidsCount(String appKey, Integer type, Integer count);
 
     UserAuthInfo findByAccountId(long accountId);
 

+ 2 - 3
src/main/java/com/fdkankan/openApi/service/system/impl/AccountServiceImpl.java

@@ -154,7 +154,7 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
         userAuthService.save(userAuthInfo);
 
         //设置redis缓存
-        userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
+        userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
 
         return ResultData.ok();
     }
@@ -201,7 +201,6 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
             }else if(CountType.REDUCE.code() == param.getCountType()){
                 totalCount  -= count;
                 totalCount = totalCount < 0 ? 0 : totalCount;
-                count = -count;
             }else{
                 totalCount += count;
             }
@@ -210,7 +209,7 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
                 userAuthInfo.setCallCount(0);
             }
             //设置redis缓存
-            userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
+            userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
         }
 
         userAuthInfo.setUserId(account.getUserId());

+ 11 - 4
src/main/java/com/fdkankan/openApi/service/system/impl/UserAuthInfoInfoServiceImpl.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.common.constant.CommonStatus;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.exception.BusinessException;
+import com.fdkankan.openApi.constant.CountType;
 import com.fdkankan.openApi.constant.RedisKey;
 import com.fdkankan.openApi.entity.system.UserAuthInfo;
 import com.fdkankan.openApi.exception.ApiBusinessException;
@@ -58,7 +59,7 @@ public class UserAuthInfoInfoServiceImpl extends ServiceImpl<IUserAuthInfoMapper
         userAuthService.save(userAuthInfo);
 
         //设置redis缓存
-        this.setReidsCount(userAuthInfo.getAppKey(), null, true);
+        this.setReidsCount(userAuthInfo.getAppKey(), CountType.NO_LIMIT.code(), null);
 
         return userAuthInfo;
     }
@@ -146,14 +147,20 @@ public class UserAuthInfoInfoServiceImpl extends ServiceImpl<IUserAuthInfoMapper
     }
 
     @Override
-    public void setReidsCount(String appKey, Integer count, boolean isInfinite) {
-        if(isInfinite){
+    public void setReidsCount(String appKey, Integer type, Integer count) {
+        if(type == CountType.NO_LIMIT.code()){
             redisUtil.sSet(RedisKey.API_METHOD_COUNT_INFINITE, appKey);
             redisUtil.del(String.format(RedisKey.API_METHOD_COUNT, appKey));
             return;
         }
         redisUtil.setRemove(RedisKey.API_METHOD_COUNT_INFINITE, appKey);
-        redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
+        if(type == CountType.ADD.code()){
+            redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
+            return;
+        }
+        if(type == CountType.REDUCE.code()){
+            redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
+        }
     }
 
     @Override