1
0
mirror of synced 2026-04-23 01:48:49 +08:00

抽取 cache 接口,方便用户自行集成 cache

This commit is contained in:
Yangkai.Shen
2019-08-02 11:15:37 +08:00
parent 92bc4ab34a
commit 57cb7fb0d1
4 changed files with 87 additions and 19 deletions

View File

@@ -13,6 +13,9 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public enum AuthCacheScheduler {
/**
* 当前实例
*/
INSTANCE;
private AtomicInteger cacheTaskNumber = new AtomicInteger(1);

View File

@@ -0,0 +1,65 @@
package me.zhyd.oauth.cache;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public enum AuthDefaultStateCache implements AuthStateCache {
/**
* 当前实例
*/
INSTANCE;
private AuthCache authCache;
AuthDefaultStateCache() {
authCache = new AuthDefaultCache();
}
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
*/
@Override
public void cache(String key, String value) {
authCache.set(key, value);
}
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
* @param timeout 指定缓存过期时间(毫秒)
*/
@Override
public void cache(String key, String value, long timeout) {
authCache.set(key, value, timeout);
}
/**
* 获取缓存内容
*
* @param key 缓存key
* @return 缓存内容
*/
@Override
public String get(String key) {
return authCache.get(key);
}
/**
* 是否存在key如果对应key的value值已过期也返回false
*
* @param key 缓存key
* @return true存在key并且value没过期falsekey不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
return authCache.containsKey(key);
}
}

View File

@@ -1,22 +1,21 @@
package me.zhyd.oauth.cache;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
* <p>
* State缓存接口方便用户扩展
* </p>
*
* @author yangkai.shen
* @date Created in 2019-08-02 10:55
*/
public class AuthStateCache {
private static AuthCache authCache = new AuthDefaultCache();
public interface AuthStateCache {
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
*/
public static void cache(String key, String value) {
authCache.set(key, value);
}
void cache(String key, String value);
/**
* 存入缓存
@@ -25,9 +24,7 @@ public class AuthStateCache {
* @param value 缓存内容
* @param timeout 指定缓存过期时间(毫秒)
*/
public static void cache(String key, String value, long timeout) {
authCache.set(key, value, timeout);
}
void cache(String key, String value, long timeout);
/**
* 获取缓存内容
@@ -35,9 +32,7 @@ public class AuthStateCache {
* @param key 缓存key
* @return 缓存内容
*/
public static String get(String key) {
return authCache.get(key);
}
String get(String key);
/**
* 是否存在key如果对应key的value值已过期也返回false
@@ -45,7 +40,5 @@ public class AuthStateCache {
* @param key 缓存key
* @return true存在key并且value没过期falsekey不存在或者已过期
*/
public static boolean containsKey(String key) {
return authCache.containsKey(key);
}
boolean containsKey(String key);
}

View File

@@ -3,6 +3,7 @@ package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
@@ -28,10 +29,16 @@ import me.zhyd.oauth.utils.UuidUtils;
public abstract class AuthDefaultRequest implements AuthRequest {
protected AuthConfig config;
protected AuthSource source;
protected AuthStateCache authStateCache;
public AuthDefaultRequest(AuthConfig config, AuthSource source) {
this(config, source, AuthDefaultStateCache.INSTANCE);
}
public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) {
this.config = config;
this.source = source;
this.authStateCache = authStateCache;
if (!AuthChecker.isSupportedAuth(config, source)) {
throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE);
}
@@ -189,7 +196,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
state = UuidUtils.getUUID();
}
// 缓存state
AuthStateCache.cache(state, state);
authStateCache.cache(state, state);
return state;
}