Skip to content

Commit 9726a69

Browse files
committed
style(admin): cache key
1 parent 671dc72 commit 9726a69

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hackyle.blog.admin.infrastructure.redis;
2+
3+
/**
4+
* 定义本模块下所有的缓存key前缀
5+
*/
6+
public class CacheKey {
7+
//统一前缀:blog admin
8+
private static final String UNI_PREFIX = "ba:";
9+
10+
11+
/**
12+
* 登录认证
13+
*/
14+
public static class Auth {
15+
private static final String PREFIX = UNI_PREFIX + "auth:";
16+
17+
//登录者用户id
18+
public static final String LOGIN_UID = PREFIX + "uid";
19+
20+
}
21+
22+
/**
23+
* 系统管理
24+
*/
25+
public static class Sys {
26+
private static final String PREFIX = UNI_PREFIX + "sys:";
27+
28+
//系统配置
29+
public static final String CONFIG_KEY = PREFIX + "config:";
30+
//字典
31+
public static final String DICT_KEY = PREFIX + "dict:";
32+
}
33+
34+
35+
36+
}

blog-admin/src/main/java/com/hackyle/blog/admin/module/auth/service/impl/LoginUserCacheServiceImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.alibaba.fastjson2.JSON;
44
import com.alibaba.fastjson2.JSONObject;
5+
import com.hackyle.blog.admin.infrastructure.redis.CacheKey;
56
import com.hackyle.blog.admin.module.auth.model.dto.UserDetailsDto;
67
import com.hackyle.blog.admin.module.auth.service.LoginUserCacheService;
7-
import com.hackyle.blog.common.constant.CacheKeyConstants;
88
import lombok.extern.slf4j.Slf4j;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.beans.factory.annotation.Value;
@@ -36,13 +36,13 @@ public class LoginUserCacheServiceImpl implements LoginUserCacheService {
3636

3737
@Override
3838
public void putCache(UserDetailsDto userDetails) {
39-
String key = CacheKeyConstants.System.LOGIN_UID + userDetails.getUserId();
39+
String key = CacheKey.Auth.LOGIN_UID + userDetails.getUserId();
4040
redisTemplate.opsForValue().set(key, userDetails, expireMinutes, TimeUnit.MINUTES);
4141
}
4242

4343
@Override
4444
public void refreshCache(UserDetailsDto userDetailsDto) {
45-
String key = CacheKeyConstants.System.LOGIN_UID + userDetailsDto.getUserId();
45+
String key = CacheKey.Auth.LOGIN_UID + userDetailsDto.getUserId();
4646

4747
Long expire = redisTemplate.getExpire(key, TimeUnit.MINUTES);
4848
if(expire <= 20) { //登录态有效期小于5分钟,刷新缓存
@@ -53,7 +53,7 @@ public void refreshCache(UserDetailsDto userDetailsDto) {
5353

5454
@Override
5555
public UserDetailsDto getCache(Long userId) {
56-
String key = CacheKeyConstants.System.LOGIN_UID + userId;
56+
String key = CacheKey.Auth.LOGIN_UID + userId;
5757

5858
Object object = redisTemplate.opsForValue().get(key);
5959
if (object == null) {
@@ -65,13 +65,13 @@ public UserDetailsDto getCache(Long userId) {
6565

6666
@Override
6767
public boolean removeCache(Long userId) {
68-
String key = CacheKeyConstants.System.LOGIN_UID + userId;
68+
String key = CacheKey.Auth.LOGIN_UID + userId;
6969
return redisTemplate.delete(key);
7070
}
7171

7272
@Override
7373
public List<UserDetailsDto> getAll() {
74-
String keyPattern = CacheKeyConstants.System.LOGIN_UID + "*";
74+
String keyPattern = CacheKey.Auth.LOGIN_UID + "*";
7575
Set<String> keys = redisTemplate.keys(keyPattern);
7676

7777
List<UserDetailsDto> res = new ArrayList<>();

0 commit comments

Comments
 (0)