【7.3.2】【cache】更新租户缓存代理

pull/41/head
fengshuonan 2022-11-08 01:39:21 +08:00
parent 000fb3fb89
commit 5541550ce5
3 changed files with 49 additions and 5 deletions

View File

@ -54,6 +54,7 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoLoginCodeRequest;
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoProperties;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.api.tenant.TenantCacheProxyFactory;
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
import cn.stylefeng.roses.kernel.jwt.JwtTokenOperator;
import cn.stylefeng.roses.kernel.jwt.api.context.JwtContext;
@ -302,7 +303,8 @@ public class AuthServiceImpl implements AuthServiceApi {
}
// 1.2 判断账号是否密码重试次数过多被冻结
Integer loginErrorCount = loginErrorCountCacheApi.get(loginRequest.getAccount());
CacheOperatorApi<Integer> tenantCacheProxy = TenantCacheProxyFactory.createTenantCacheProxy(loginRequest.getTenantCode(), loginErrorCountCacheApi);
Integer loginErrorCount = tenantCacheProxy.get(loginRequest.getAccount());
if (loginErrorCount != null && loginErrorCount >= LoginCacheConstants.MAX_ERROR_LOGIN_COUNT) {
throw new AuthException(AuthExceptionEnum.LOGIN_LOCKED);
}

View File

@ -13,13 +13,12 @@ import java.lang.reflect.Proxy;
* @author fengshuonan
* @date 2022/11/8 0:45
*/
@SuppressWarnings("all")
public class TenantCacheProxy<T> implements InvocationHandler {
public class TenantCacheProxy implements InvocationHandler {
/**
*
*/
private CacheOperatorApi<T> targetCacheObject = null;
private CacheOperatorApi<?> targetCacheObject = null;
/**
*
@ -34,7 +33,7 @@ public class TenantCacheProxy<T> implements InvocationHandler {
* @author fengshuonan
* @date 2022/11/8 1:05
*/
public CacheOperatorApi<T> bindCacheObject(String tenantCode, CacheOperatorApi<T> target) {
public <T> CacheOperatorApi<T> bindCacheObject(String tenantCode, CacheOperatorApi<T> target) {
this.targetCacheObject = target;
this.tenantCode = tenantCode;
return (CacheOperatorApi<T>) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);

View File

@ -0,0 +1,43 @@
package cn.stylefeng.roses.kernel.cache.api.tenant;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import java.util.HashMap;
import java.util.Map;
/**
*
*
* @author fengshuonan
* @date 2022/11/8 0:45
*/
public class TenantCacheProxyFactory {
/**
*
*/
private static final Map<String, CacheOperatorApi<?>> proxyCacheMap = new HashMap<>();
/**
*
*
* @author fengshuonan
* @date 2022/11/8 1:18
*/
public static <T> CacheOperatorApi<T> createTenantCacheProxy(String tenantCode, CacheOperatorApi<?> cacheOperatorApi) {
// 计算缓存的key
String key = tenantCode + cacheOperatorApi.getCommonKeyPrefix();
// 直接获取缓存中的代理
if (proxyCacheMap.containsKey(key)) {
return (CacheOperatorApi<T>) proxyCacheMap.get(key);
}
TenantCacheProxy tenantCacheProxy = new TenantCacheProxy();
CacheOperatorApi<?> resultCacheOperator = tenantCacheProxy.bindCacheObject(tenantCode, cacheOperatorApi);
proxyCacheMap.put(key, resultCacheOperator);
return (CacheOperatorApi<T>) resultCacheOperator;
}
}