Skip to content

Commit 1ab0b58

Browse files
committed
Redis 配置测试
1 parent 5b61ce3 commit 1ab0b58

File tree

5 files changed

+136
-47
lines changed

5 files changed

+136
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codingmore;
2+
3+
import org.springframework.cache.annotation.Cacheable;
4+
import org.springframework.cache.annotation.Caching;
5+
6+
/**
7+
* 微信搜索「沉默王二」,回复 Java
8+
*
9+
* @author 沉默王二
10+
* @date 6/16/22
11+
*/
12+
public interface CacheDemoService {
13+
Object getFromDB(Integer id);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codingmore;
2+
3+
import org.springframework.cache.annotation.Cacheable;
4+
import org.springframework.cache.annotation.Caching;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* 微信搜索「沉默王二」,回复 Java
9+
*
10+
* @author 沉默王二
11+
* @date 6/16/22
12+
*/
13+
@Service
14+
public class CacheDemoServiceImpl implements CacheDemoService {
15+
16+
17+
@Caching(cacheable = {
18+
@Cacheable(cacheNames = "demoCache", key = "#id + 0"),
19+
})
20+
@Override
21+
public Object getFromDB(Integer id) {
22+
System.out.println("模拟去db查询~~~" + id);
23+
return "hello cache...";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.codingmore;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.cache.Cache;
7+
import org.springframework.data.redis.cache.RedisCacheManager;
8+
import org.springframework.data.redis.core.RedisTemplate;
9+
import org.springframework.data.redis.core.StringRedisTemplate;
10+
11+
import javax.annotation.Resource;
12+
13+
/**
14+
* 微信搜索「沉默王二」,回复 Java
15+
*
16+
* @author 沉默王二
17+
* @date 6/16/22
18+
*/
19+
@SpringBootTest
20+
class CodingmoreRedisApplicationTests {
21+
@Resource
22+
private RedisTemplate redisTemplate;
23+
24+
@Resource
25+
private StringRedisTemplate stringRedisTemplate;
26+
27+
@Autowired
28+
private CacheDemoService cacheDemoService;
29+
30+
@Autowired
31+
private RedisCacheManager cacheManager;
32+
33+
@Test
34+
public void testRedis() {
35+
cacheDemoService.getFromDB(3);
36+
37+
System.out.println("----------验证缓存是否生效----------");
38+
Cache cache = cacheManager.getCache("demoCache");
39+
System.out.println(cache.get(1, String.class));
40+
41+
}
42+
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Spring Boot 配置
12
spring:
23
profiles:
34
active: dev #默认为开发环境
@@ -9,9 +10,11 @@ spring:
910
date-format: yyyy-MM-dd HH:mm:ss
1011
time-zone: GMT+8
1112

13+
# 端口号
1214
server:
13-
port: 8080
15+
port: 9002
1416

17+
# MyBatis plus 的配置查询后写注释
1518
mybatis-plus:
1619
mapper-locations:
1720
- classpath:dao/*.xml
@@ -23,52 +26,35 @@ mybatis-plus:
2326
map-underscore-to-camel-case: true
2427
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2528

26-
ignored:
27-
urls: #安全路径白名单
28-
- /doc.html
29-
- /swagger-ui/**
30-
- /swagger/**
31-
- /swagger-resources/**
32-
- /**/v3/api-docs
33-
- /**/*.js
34-
- /**/*.css
35-
- /**/*.png
36-
- /**/*.ico
37-
- /webjars/springfox-swagger-ui/**
38-
- /actuator/**
39-
- /druid/**
40-
- /users/login
41-
- /users/register
42-
- /users/info
43-
- /users/logout
44-
- /minio/upload
29+
# 自定义类 ignoreConfig,URL 的白名单
30+
secure:
31+
ignored:
32+
urls: #安全路径白名单
33+
- /doc.html
34+
- /swagger-ui/**
35+
- /swagger/**
36+
- /swagger-resources/**
37+
- /**/v3/api-docs
38+
- /**/*.js
39+
- /**/*.css
40+
- /**/*.png
41+
- /**/*.ico
42+
- /webjars/springfox-swagger-ui/**
43+
- /actuator/**
44+
- /druid/**
45+
- /users/login
46+
- /users/register
47+
- /users/info
48+
- /users/logout
49+
- /minio/upload
4550

4651
# 登录认证 token filter
4752
jwt:
4853
tokenHeader: Authorization #JWT存储的请求头
49-
secret: learn-admin-secret #JWT加解密使用的密钥
54+
secret: codingmore-admin-secret #JWT加解密使用的密钥
5055
expiration: 604800 #JWT的超期限时间(60*60*24*7)
5156
tokenHead: 'Bearer ' #JWT负载中拿到开头
5257

53-
54-
# oss
55-
aliyun:
56-
oss:
57-
# oss对外服务的访问域名
58-
endpoint: oss-cn-beijing.aliyuncs.com
59-
# 访问身份验证中用到用户标识
60-
accessKeyId: LTAI5t924wYTNT9dQoAWGDKa
61-
# 用户用于加密签名字符串和oss用来验证签名字符串的密钥
62-
accessKeySecret: RYNmqo4yZdTXbPcuQVE4jK9ax0HRLM
63-
# oss的存储空间
64-
bucketName: itwanger-oss
65-
# 上传文件大小(M)
66-
maxSize: 3
67-
# 上传文件夹路径前缀
68-
dir:
69-
prefix: codingmore/images/
70-
71-
7258
#异步线程配置
7359
async:
7460
executor:
@@ -81,4 +67,9 @@ async:
8167
#设置线程活跃时间(秒)
8268
keepAliveSeconds: 360
8369
#设置默认线程名称
84-
threadNamePrefix: upload-image-thread-
70+
threadNamePrefix: sync-thread-
71+
72+
# 文章定时发布的最小间隔
73+
post:
74+
schedule:
75+
minInterval: 10

codingmore-mbg/src/main/java/com/codingmore/config/RedisConfig.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.fasterxml.jackson.annotation.PropertyAccessor;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.cache.CacheManager;
810
import org.springframework.cache.annotation.CachingConfigurerSupport;
911
import org.springframework.cache.annotation.EnableCaching;
1012
import org.springframework.context.annotation.Bean;
@@ -20,13 +22,16 @@
2022
import org.springframework.data.redis.serializer.StringRedisSerializer;
2123

2224
import java.time.Duration;
25+
import java.util.HashMap;
26+
import java.util.Map;
2327

2428
/**
2529
* Redis配置类
2630
* Created by zhanglei on 2020/3/2.
2731
*/
2832
@EnableCaching
2933
@Configuration
34+
@Slf4j
3035
public class RedisConfig extends CachingConfigurerSupport {
3136

3237
@Bean
@@ -67,12 +72,22 @@ public RedisSerializer<Object> redisSerializer() {
6772
}
6873

6974
@Bean
70-
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
71-
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
72-
//设置Redis缓存有效期为1天
73-
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
74-
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer())).entryTtl(Duration.ofDays(1));
75-
return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
75+
public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
76+
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
77+
.entryTtl(Duration.ofDays(1)) // 默认没有特殊指定的
78+
.computePrefixWith(cacheName -> "caching:" + cacheName);
79+
80+
// 针对不同cacheName,设置不同的过期时间
81+
Map<String, RedisCacheConfiguration> initialCacheConfiguration = new HashMap<String, RedisCacheConfiguration>() {{
82+
put("demoCache", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMillis(20*1000)));
83+
// ...
84+
}};
85+
86+
RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisConnectionFactory)
87+
.cacheDefaults(defaultCacheConfig) // 默认配置(强烈建议配置上)。 比如动态创建出来的都会走此默认配置
88+
.withInitialCacheConfigurations(initialCacheConfiguration) // 不同cache的个性化配置
89+
.build();
90+
return redisCacheManager;
7691
}
7792

7893
}

0 commit comments

Comments
 (0)