Skip to content

Commit a634799

Browse files
committed
1、升级J2cache为2.7.0版本,主要修复channel获取次数过多导致的错误问题,另个j2cache后期可能会移除对jedis的支持,所以还是提前升级了
2、调整二级缓存redis为lettuce,lettuce为spring推荐的redis操作方式,另个j2cache也推荐使用 3、配置文件application.yml和j2cache配置文件都增加了对lettuce的支持 4、修改com.geekcattle.core.redis包下的RedisCacheManage为RedisShiroCacheManage,因为此文件名和spring源码中的一个文件重名,为了区分特些改名 5、优化RedisConfiguration的JedisConnectionFactory为LettuceConnctionFoctory
1 parent f55c413 commit a634799

17 files changed

+540
-118
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
<dependency>
147147
<groupId>net.oschina.j2cache</groupId>
148148
<artifactId>j2cache-core</artifactId>
149-
<version>2.3.22-release</version>
149+
<version>2.7.0-release</version>
150150
<exclusions>
151151
<exclusion>
152152
<groupId>org.slf4j</groupId>

src/main/java/com/geekcattle/core/j2cache/autoconfigure/J2CacheAutoConfiguration.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.geekcattle.core.j2cache.autoconfigure;
1818

19+
import com.geekcattle.core.j2cache.cache.support.util.SpringJ2CacheConfigUtil;
1920
import com.geekcattle.core.j2cache.cache.support.util.SpringUtil;
2021
import net.oschina.j2cache.CacheChannel;
2122
import net.oschina.j2cache.J2Cache;
@@ -27,23 +28,31 @@
2728
import org.springframework.context.annotation.Bean;
2829
import org.springframework.context.annotation.Configuration;
2930
import org.springframework.context.annotation.DependsOn;
31+
import org.springframework.context.annotation.PropertySource;
32+
import org.springframework.core.env.StandardEnvironment;
3033

3134
import java.io.IOException;
3235

3336
@ConditionalOnClass(J2Cache.class)
3437
@EnableConfigurationProperties({J2CacheExtendConfig.class})
3538
@Configuration
39+
@PropertySource(value = "${j2cache.config-location}")
3640
public class J2CacheAutoConfiguration {
3741

3842
@Autowired
39-
private J2CacheExtendConfig cacheExtendConfig;
43+
private StandardEnvironment standardEnvironment;
4044

4145
@Bean
42-
@DependsOn("springUtil")
43-
public CacheChannel cacheChannel() throws IOException {
46+
public J2CacheConfig j2CacheConfig() throws IOException{
4447
J2CacheConfig cacheConfig = new J2CacheConfig();
45-
cacheConfig = cacheConfig.initFromConfig(cacheExtendConfig.getConfigLocation());
46-
J2CacheBuilder builder = J2CacheBuilder.init(cacheConfig);
48+
cacheConfig = SpringJ2CacheConfigUtil.initFromConfig(standardEnvironment);
49+
return cacheConfig;
50+
}
51+
52+
@Bean
53+
@DependsOn({"springUtil","j2CacheConfig"})
54+
public CacheChannel cacheChannel(J2CacheConfig j2CacheConfig) throws IOException {
55+
J2CacheBuilder builder = J2CacheBuilder.init(j2CacheConfig);
4756
return builder.getChannel();
4857
}
4958

Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
1-
/*
2-
* Copyright (c) 2017-2018. 放牛极客<l_iupeiyu@qq.com>
3-
* <p>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
* Unless required by applicable law or agreed to in writing, software
9-
* distributed under the License is distributed on an "AS IS" BASIS,
10-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
* See the License for the specific language governing permissions and
12-
* limitations under the License.
13-
* </p>
14-
*
15-
*/
16-
171
package com.geekcattle.core.j2cache.autoconfigure;
182

193
import org.springframework.boot.context.properties.ConfigurationProperties;
20-
4+
/**
5+
* 相关的配置信息
6+
* @author 小雨哥哥
7+
*
8+
*/
219
@ConfigurationProperties(prefix = "j2cache")
2210
public class J2CacheExtendConfig {
23-
2411
private String configLocation = "/j2cache.properties";
2512

2613
/**
27-
* 是否开启spring cache缓存,注意:开启后需要添加spring.cache.type=none,将缓存类型设置为none
14+
* 是否开启spring cache缓存,注意:开启后需要添加spring.cache.type=GENERIC,将缓存类型设置为GENERIC
2815
*/
2916
private Boolean openSpringCache = false;
3017

18+
/**
19+
* 缓存清除模式,
20+
* <ul>
21+
* <li>active:主动清除,二级缓存过期主动通知各节点清除,优点在于所有节点可以同时收到缓存清除</li>
22+
* <li>passive:被动清除,一级缓存过期进行通知各节点清除一二级缓存,</li>
23+
* <li> blend:两种模式一起运作,对于各个节点缓存准确以及及时性要求高的可以使用,正常用前两种模式中一个就可</li>
24+
* </ul>
25+
*/
26+
private String cacheCleanMode = "passive";
27+
28+
/**
29+
* 是否允许缓存空值,默认:true
30+
*/
31+
private boolean allowNullValues = true;
32+
33+
/**
34+
* 使用哪种redis客户端,默认:jedis
35+
* <ul>
36+
* <li><a href ='https://github.com/xetorthio/jedis'>jedis: https://github.com/xetorthio/jedis</a></li>
37+
* <li><a href ='https://github.com/lettuce-io/lettuce-core'>lettuce: https://github.com/lettuce-io/lettuce-core</a></li>
38+
* </ul>
39+
*/
40+
private String redisClient = "jedis";
41+
42+
3143
public String getConfigLocation() {
3244
return configLocation;
3345
}
@@ -43,4 +55,28 @@ public Boolean getOpenSpringCache() {
4355
public void setOpenSpringCache(Boolean openSpringCache) {
4456
this.openSpringCache = openSpringCache;
4557
}
58+
59+
public String getCacheCleanMode() {
60+
return cacheCleanMode;
61+
}
62+
63+
public void setCacheCleanMode(String cacheCleanMode) {
64+
this.cacheCleanMode = cacheCleanMode;
65+
}
66+
67+
public boolean isAllowNullValues() {
68+
return allowNullValues;
69+
}
70+
71+
public void setAllowNullValues(boolean allowNullValues) {
72+
this.allowNullValues = allowNullValues;
73+
}
74+
75+
public String getRedisClient() {
76+
return redisClient;
77+
}
78+
79+
public void setRedisClient(String redisClient) {
80+
this.redisClient = redisClient;
81+
}
4682
}

src/main/java/com/geekcattle/core/j2cache/autoconfigure/J2CacheSpringCacheAutoConfiguration.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ public class J2CacheSpringCacheAutoConfiguration {
4343

4444
private final CacheProperties cacheProperties;
4545

46-
J2CacheSpringCacheAutoConfiguration(CacheProperties cacheProperties) {
46+
private final J2CacheExtendConfig j2CacheExtendConfig;
47+
48+
J2CacheSpringCacheAutoConfiguration(CacheProperties cacheProperties, J2CacheExtendConfig j2CacheExtendConfig) {
4749
this.cacheProperties = cacheProperties;
50+
this.j2CacheExtendConfig = j2CacheExtendConfig;
4851
}
4952

5053
@Bean
5154
@ConditionalOnBean(CacheChannel.class)
5255
public J2CacheCacheManger cacheManager(CacheChannel cacheChannel) {
5356
List<String> cacheNames = cacheProperties.getCacheNames();
5457
J2CacheCacheManger cacheCacheManger = new J2CacheCacheManger(cacheChannel);
58+
cacheCacheManger.setAllowNullValues(j2CacheExtendConfig.isAllowNullValues());
5559
cacheCacheManger.setCacheNames(cacheNames);
5660
return cacheCacheManger;
5761
}

0 commit comments

Comments
 (0)