baoshiwei
2025-04-19 9d960ed0058f9087f49e9741a9af06c3f9116eb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.zhitan.common.config.keycloak;
 
import com.zhitan.common.core.redis.RedisCache;
import lombok.AllArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.time.Duration;
import java.util.concurrent.TimeUnit;
 
/**
 * 授权状态缓存
 */
@AllArgsConstructor
@Component
public class AuthRedisStateCache implements AuthStateCache {
 
    @Autowired
    private final RedisCache redisCache;
 
    /**
     * 存入缓存
     *
     * @param key   缓存key
     * @param value 缓存内容
     */
    @Override
    public void cache(String key, String value) {
        // 授权超时时间 默认三分钟
        redisCache.setCacheObject("social_auth_codes:" + key, value, 3, TimeUnit.MINUTES);
    }
 
    @Override
    public void cache(String s, String s1, long l) {
 
    }
 
 
 
    /**
     * 获取缓存内容
     *
     * @param key 缓存key
     * @return 缓存内容
     */
    @Override
    public String get(String key) {
        return redisCache.getCacheObject("social_auth_codes:" + key);
    }
 
    /**
     * 是否存在key,如果对应key的value值已过期,也返回false
     *
     * @param key 缓存key
     * @return true:存在key,并且value没过期;false:key不存在或者已过期
     */
    @Override
    public boolean containsKey(String key) {
        return redisCache.hasKey("social_auth_codes:" + key);
    }
}