默认的缓存实现 @author yadong.zhang (yadong.zhang0415(a)gmail.com) @since 1.9.3
| 17 | * @since 1.9.3 |
| 18 | */ |
| 19 | public class AuthDefaultCache implements AuthCache { |
| 20 | |
| 21 | /** |
| 22 | * state cache |
| 23 | */ |
| 24 | private static Map<String, CacheState> stateCache = new ConcurrentHashMap<>(); |
| 25 | private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock(true); |
| 26 | private final Lock writeLock = cacheLock.writeLock(); |
| 27 | private final Lock readLock = cacheLock.readLock(); |
| 28 | |
| 29 | public AuthDefaultCache() { |
| 30 | if (AuthCacheConfig.schedulePrune) { |
| 31 | this.schedulePrune(AuthCacheConfig.timeout); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * 设置缓存 |
| 37 | * |
| 38 | * @param key 缓存KEY |
| 39 | * @param value 缓存内容 |
| 40 | */ |
| 41 | @Override |
| 42 | public void set(String key, String value) { |
| 43 | set(key, value, AuthCacheConfig.timeout); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 设置缓存 |
| 48 | * |
| 49 | * @param key 缓存KEY |
| 50 | * @param value 缓存内容 |
| 51 | * @param timeout 指定缓存过期时间(毫秒) |
| 52 | */ |
| 53 | @Override |
| 54 | public void set(String key, String value, long timeout) { |
| 55 | writeLock.lock(); |
| 56 | try { |
| 57 | stateCache.put(key, new CacheState(value, timeout)); |
| 58 | } finally { |
| 59 | writeLock.unlock(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * 获取缓存 |
| 65 | * |
| 66 | * @param key 缓存KEY |
| 67 | * @return 缓存内容 |
| 68 | */ |
| 69 | @Override |
| 70 | public String get(String key) { |
| 71 | readLock.lock(); |
| 72 | try { |
| 73 | CacheState cacheState = stateCache.get(key); |
| 74 | if (null == cacheState || cacheState.isExpired()) { |
| 75 | return null; |
| 76 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…