郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.gkhy.safePlatform.account.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO;
import com.gkhy.safePlatform.account.entity.user.MenuInfoDO;
import com.gkhy.safePlatform.account.service.RedisService;
import com.gkhy.safePlatform.account.utils.RedisUtils;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Service("redisService")
public class RedisServiceImpl implements RedisService {
 
    @Autowired
    private RedisUtils redisUtils;
 
 
 
    /**
     * @Description: 设置用户缓存
     */
    @Override
    public void setCacheUserAndExpireTime(String key, ContextCacheUser user, Long expireTime) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        if (expireTime == null) {
            expireTime = 5 * 60L;
        }
        redisUtils.set(key, JSONObject.toJSONString(user), expireTime, TimeUnit.SECONDS);
    }
 
    @Override
    public void setCacheAuthorityAndExpireTime(String key, List<GrantedAuthority> authorities, Long expireTime) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        if (expireTime == null) {
            expireTime = 5 * 60L;
        }
        redisUtils.set(key, JSONObject.toJSONString(authorities), expireTime, TimeUnit.SECONDS);
    }
 
    @Override
    public void setCacheMenuAndExpireTime(String key, List<MenuInfoDO> menus, Long expireTime) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        if (expireTime == null) {
            expireTime = 5 * 60L;
        }
        redisUtils.set(key, JSONArray.toJSONString(menus), expireTime, TimeUnit.SECONDS);
    }
 
 
 
    @Override
    public void setCacheDepAndExpireTime(String key, List<DepartmentInfoDO> deps, Long expireTime) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        if (expireTime == null) {
            expireTime = 5 * 60L;
        }
        redisUtils.set(key, JSONArray.toJSONString(deps), expireTime, TimeUnit.SECONDS);
    }
 
 
    @Override
    public String getCacheUserByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        ;
        Object o = redisUtils.get(key);
        if (o != null) {
            return o.toString();
        }
        return null;
    }
 
    @Override
    public String getCacheMenuByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        Object o = redisUtils.get(key);
        if (o != null) {
            return o.toString();
        }
        return null;
    }
 
    @Override
    public String getCacheDepByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        Object o = redisUtils.get(key);
        if (o != null) {
            return o.toString();
        }
        return null;
    }
 
    @Override
    public Long getLeftSecondsByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        return redisUtils.getExpireTime(key);
    }
 
    @Override
    public void resetKeyExpireTime(String key, Long expireTime) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        redisUtils.resetKeyExpireTime(key,expireTime);
    }
 
    @Override
    public void cleanCacheUserByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        redisUtils.remove(key);
 
    }
 
    @Override
    public void cleanCacheAuthorityByKey(String key) {
        ;
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(ResultCodes.REDIS_KEY_NULL);
        }
        redisUtils.remove(key);
    }
}