一、配置Pom文件
在使用spring boot 2.0整合redis时遇到了好多问题,网上很多例子都是1.x版本的。故2.0没有折腾好所以将2.0降到了1.5。降级后由于thymeleaf版本也会从默认匹配的3.0降到2.0,所以这里调整了thymeleaf版本,仍指定为3.0 。另mysql也遇到了写问题也在pom中做了调整。调整部分如下:
前:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath />
</parent>
后:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath />
</parent>
指定thymeleaf版本否则会随springboot版本发生变化
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
指定mysql 版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
最重要的添加 redis starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
二、在配置文件application.properties 中调整版本差异以及添加redis配置
server.context-path=/WebDemo
#server.servlet.context-path=/WebDemo 2.x 配置
redis部分
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
三、redis场景使用
3.1 编写直接使用redis增删查改工具类操作缓存
在项目中新建package com.demo.redis。添加redisConfig 类 ,以及redis工具类RedisServiceImpl类 如下:
@Configuration
@EnableCaching
@EnableAutoConfiguration
public class RedisConfig extends CachingConfigurerSupport{
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间(秒)
rcm.setDefaultExpiration(3600);
return rcm;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
@Bean
public RedisTemplate<?, ?> getRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
RedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
return template;
}
}
com.demo.redis.RedisServiceImpl
@Service("redisService")
public class RedisServiceImpl{
@Resource
private RedisTemplate<String, ?> redisTemplate;
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.del(key.getBytes());
return true;
}
});
return result;
}
}
在项目中测试使用redis工具设置获取缓存
测试代码:
@Controller
@RequestMapping(value="/course")
public class CourseController {
@Autowired
private RedisServiceImpl redisService;
@RequestMapping(value="/setCourVal",method=RequestMethod.GET)
public void setCourVal(String val){
redisService.set("testkey", val);
}
@ResponseBody
@RequestMapping(value="/getCourVal",method=RequestMethod.GET)
public String getCourVal(String val){
return redisService.get("testkey");
}
}
测试过程:
首先方式连接进行设置 http://127.0.0.1:8080/WebDemo/course/setCourVal?val=testValue
在进行访问 http://127.0.0.1:8080/WebDemo/course/getCourVal
我么页面也可看到 打印了我们的缓存值 testValue
3.2 如何在项目中对访问的数据接口数据进行缓存呢
在项目中我们主要使用了Spring的缓存注解 @Cacheable、@CachePut、@CacheEvict 来实现缓存功能,具体可以参考下一篇博客进行查看 Spring boot - 整合 Redis缓存(下) https://blog.csdn.net/liuhenghui5201/article/details/90545317