提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.dal.redis.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.core.date.LocalDateTimeUtil; |
|
4 |
import com.iailab.framework.common.util.collection.CollectionUtils; |
|
5 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
6 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
|
7 |
import org.springframework.data.redis.core.StringRedisTemplate; |
|
8 |
import org.springframework.stereotype.Repository; |
|
9 |
|
|
10 |
import javax.annotation.Resource; |
|
11 |
import java.time.LocalDateTime; |
|
12 |
import java.time.temporal.ChronoUnit; |
|
13 |
import java.util.Collection; |
|
14 |
import java.util.List; |
|
15 |
import java.util.concurrent.TimeUnit; |
|
16 |
|
|
17 |
import static com.iailab.module.system.dal.redis.RedisKeyConstants.OAUTH2_ACCESS_TOKEN; |
|
18 |
|
|
19 |
/** |
|
20 |
* {@link OAuth2AccessTokenDO} 的 RedisDAO |
|
21 |
* |
|
22 |
* @author iailab |
|
23 |
*/ |
|
24 |
@Repository |
|
25 |
public class OAuth2AccessTokenRedisDAO { |
|
26 |
|
|
27 |
@Resource |
|
28 |
private StringRedisTemplate stringRedisTemplate; |
|
29 |
|
|
30 |
public OAuth2AccessTokenDO get(String accessToken) { |
|
31 |
String redisKey = formatKey(accessToken); |
|
32 |
return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), OAuth2AccessTokenDO.class); |
|
33 |
} |
|
34 |
|
|
35 |
public void set(OAuth2AccessTokenDO accessTokenDO) { |
|
36 |
String redisKey = formatKey(accessTokenDO.getAccessToken()); |
|
37 |
// 清理多余字段,避免缓存 |
|
38 |
accessTokenDO.setUpdater(null).setUpdateTime(null).setCreateTime(null).setCreator(null).setDeleted(null); |
|
39 |
long time = LocalDateTimeUtil.between(LocalDateTime.now(), accessTokenDO.getExpiresTime(), ChronoUnit.SECONDS); |
|
40 |
if (time > 0) { |
|
41 |
stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(accessTokenDO), time, TimeUnit.SECONDS); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
public void delete(String accessToken) { |
|
46 |
String redisKey = formatKey(accessToken); |
|
47 |
stringRedisTemplate.delete(redisKey); |
|
48 |
} |
|
49 |
|
|
50 |
public void deleteList(Collection<String> accessTokens) { |
|
51 |
List<String> redisKeys = CollectionUtils.convertList(accessTokens, OAuth2AccessTokenRedisDAO::formatKey); |
|
52 |
stringRedisTemplate.delete(redisKeys); |
|
53 |
} |
|
54 |
|
|
55 |
private static String formatKey(String accessToken) { |
|
56 |
return String.format(OAUTH2_ACCESS_TOKEN, accessToken); |
|
57 |
} |
|
58 |
|
|
59 |
} |