更新到cache
This commit is contained in:
parent
3195f26714
commit
df914631a5
@ -15,7 +15,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@ -50,7 +50,6 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.1</version>
|
||||
<configuration>
|
||||
<!--<proc>none</proc>-->
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
|
@ -15,7 +15,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
|
@ -2,9 +2,15 @@
|
||||
|
||||
基于注解的声明式缓存
|
||||
|
||||
SpringBoot 2.0的写法有些改变,参考:
|
||||
|
||||
https://3dot141.com/blogs/20329.html
|
||||
|
||||
https://my.oschina.net/u/3773384/blog/1795296
|
||||
|
||||
## 运行
|
||||
|
||||
初始化sql文件在`resources/sql/t_user.sql`中
|
||||
本地安装好MySQL 5.7,并执行初始化sql脚本:`resources/sql/t_user.sql`
|
||||
|
||||
另外还需要安装Redis,配置好`application.yml`文件中的redis地址
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@ -34,6 +34,16 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -3,14 +3,30 @@ package com.xncoding.trans.config;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* RedisCacheConfig
|
||||
@ -21,17 +37,73 @@ import java.util.Arrays;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class RedisCacheConfig {
|
||||
public class RedisCacheConfig extends CachingConfigurerSupport {
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
/**
|
||||
* 重新配置RedisCacheManager
|
||||
*/
|
||||
@Autowired
|
||||
public void configRedisCacheManger(RedisCacheManager rd) {
|
||||
rd.setDefaultExpiration(100L);
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.redis.port}")
|
||||
private String port;
|
||||
|
||||
@Bean
|
||||
public RedisStandaloneConfiguration getRedisClient() {
|
||||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, Integer.parseInt(port));
|
||||
return redisStandaloneConfiguration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration RedisStandaloneConfiguration) {
|
||||
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(RedisStandaloneConfiguration);
|
||||
return jedisConnectionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
|
||||
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
|
||||
redisTemplate.setConnectionFactory(cf);
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisCacheConfiguration redisCacheConfiguration() {
|
||||
return RedisCacheConfiguration
|
||||
.defaultCacheConfig()
|
||||
.serializeKeysWith(
|
||||
RedisSerializationContext
|
||||
.SerializationPair
|
||||
.fromSerializer(new StringRedisSerializer()))
|
||||
.serializeValuesWith(
|
||||
RedisSerializationContext
|
||||
.SerializationPair
|
||||
.fromSerializer(new GenericJackson2JsonRedisSerializer()))
|
||||
.entryTtl(Duration.ofSeconds(600L));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager(RedisConnectionFactory cf) {
|
||||
//RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(cf);
|
||||
//RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, RedisCacheConfiguration.defaultCacheConfig());
|
||||
RedisCacheManager cm = RedisCacheManager.builder(cf).cacheDefaults(redisCacheConfiguration()).build();
|
||||
return cm;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public KeyGenerator keyGenerator() {
|
||||
// return new KeyGenerator() {
|
||||
// @Override
|
||||
// public Object generate(Object o, Method method, Object... objects) {
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// sb.append(o.getClass().getName());
|
||||
// sb.append(method.getName());
|
||||
// for (Object obj : objects) {
|
||||
// sb.append(obj.toString());
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
/**
|
||||
* 自定义缓存key的生成类实现
|
||||
*/
|
||||
|
@ -26,7 +26,7 @@ public class UserService {
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(cacheNames = "user1", key = "#id")
|
||||
@Cacheable(key = "#id")
|
||||
public User getById(int id) {
|
||||
logger.info("获取用户start...");
|
||||
return userMapper.selectById(id);
|
||||
@ -39,7 +39,7 @@ public class UserService {
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(cacheNames = "user1", key = "#id", sync = true)
|
||||
@Cacheable(key = "#id", sync = true)
|
||||
public User getById2(int id) {
|
||||
logger.info("获取用户start...");
|
||||
return userMapper.selectById(id);
|
||||
@ -55,7 +55,7 @@ public class UserService {
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(cacheNames = "user1", keyGenerator = "myKeyGenerator")
|
||||
@Cacheable(keyGenerator = "myKeyGenerator")
|
||||
public User queryUserById(int id) {
|
||||
logger.info("queryUserById,id={}", id);
|
||||
return userMapper.selectById(id);
|
||||
@ -66,9 +66,9 @@ public class UserService {
|
||||
*
|
||||
* @param user
|
||||
*/
|
||||
@CachePut(cacheNames = "user1", key = "#user.id")
|
||||
@CachePut(key = "#user.id")
|
||||
public void createUser(User user) {
|
||||
logger.info("创建用户start...");
|
||||
logger.info("创建用户start..., user.id=" + user.getId());
|
||||
userMapper.insert(user);
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ public class UserService {
|
||||
*
|
||||
* @param user
|
||||
*/
|
||||
@CachePut(cacheNames = "user1", key = "#user.id")
|
||||
@CachePut(key = "#user.id")
|
||||
public void updateUser(User user) {
|
||||
logger.info("更新用户start...");
|
||||
userMapper.updateById(user);
|
||||
@ -86,7 +86,7 @@ public class UserService {
|
||||
/**
|
||||
* 对符合key条件的记录从缓存中book1移除
|
||||
*/
|
||||
@CacheEvict(cacheNames = "user1", key = "#id")
|
||||
@CacheEvict(key = "#id")
|
||||
public void deleteById(int id) {
|
||||
logger.info("删除用户start...");
|
||||
userMapper.deleteById(id);
|
||||
@ -95,7 +95,7 @@ public class UserService {
|
||||
/**
|
||||
* allEntries = true: 清空user1里的所有缓存
|
||||
*/
|
||||
@CacheEvict(cacheNames="user1", allEntries=true)
|
||||
@CacheEvict(allEntries=true)
|
||||
public void clearUser1All(){
|
||||
logger.info("clearAll");
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ spring:
|
||||
profiles:
|
||||
active: dev
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
|
||||
url: jdbc:mysql://123.207.66.156:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
|
||||
username: root
|
||||
password: 123456
|
||||
password: _EnZhi123
|
||||
|
||||
################### mybatis-plus配置 ###################
|
||||
mybatis-plus:
|
||||
@ -43,20 +43,14 @@ spring:
|
||||
cache:
|
||||
type: REDIS
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
host: 123.207.66.156
|
||||
port: 6379
|
||||
timeout: 0
|
||||
database: 0
|
||||
pool:
|
||||
max-active: 100
|
||||
max-wait: -1
|
||||
max-idle: 8
|
||||
min-idle: 0
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: INFO
|
||||
com:
|
||||
xncoding: DEBUG
|
||||
file: E:/logs/trans.log
|
||||
file: D:/logs/springboot-cache.log
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
|
@ -30,4 +30,4 @@ logging:
|
||||
ROOT: INFO
|
||||
com:
|
||||
xncoding: DEBUG
|
||||
file: E:/logs/app.log
|
||||
file: D:/logs/app.log
|
||||
|
@ -9,8 +9,8 @@ import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
@ -98,15 +98,14 @@ public class ApplicationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 方式4. 客户端代码生成方式
|
||||
*/
|
||||
@Test
|
||||
public void cl4() {
|
||||
CommonService_Service c = new CommonService_Service();
|
||||
com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom");
|
||||
assertThat(user.getName(), is("Tom"));
|
||||
}
|
||||
// /**
|
||||
// * 方式4. 客户端代码生成方式
|
||||
// */
|
||||
// @Test
|
||||
// public void cl4() {
|
||||
// CommonService_Service c = new CommonService_Service();
|
||||
// com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom");
|
||||
// assertThat(user.getName(), is("Tom"));
|
||||
// }
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user