版本:
Springboot 2.0
spring 5.0
jdk 1.8.0_121
redis Windows版3.2.100
Windows版redis下载地址
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis配置
配置文件
spring.redis.database=1
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=root
spring.redis.timeout=20
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=5
spring.redis.jedis.pool.min-idle=0
Redis 配置类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Author: YiHong
* @Description: Redis 配置类
* @Date: Created in 14:29 2018/11/27.
* @Modified By:
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration
.defaultCacheConfig()
.serializeKeysWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer()));
}
@Override
@Bean
public CacheErrorHandler errorHandler() {
// 异常处理,当Redis发生异常时,打印日志,但是程序正常走
logger.info("初始化 -> [{}]", "Redis CacheErrorHandler");
CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
@Override
public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
logger.error("Redis occur handleCacheGetError:key -> [{}]", key, e);
}
@Override
public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
logger.error("Redis occur handleCachePutError:key -> [{}];value -> [{}]", key, value, e);
}
@Override
public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
logger.error("Redis occur handleCacheEvictError:key -> [{}]", key, e);
}
@Override
public void handleCacheClearError(RuntimeException e, Cache cache) {
logger.error("Redis occur handleCacheClearError:", e);
}
};
return cacheErrorHandler;
}
}
具体使用--->基于注解
在serviceImpl使用注解
/**
* 根据id查询
*
* @param id
* @return
*/
@Cacheable(value = "configu",key="#id")
@Override
public CarseriesConfiguration selectById(String id) {
return carseriesConfigurationMapper.selectById(id);
}
至此,就全部ok了。
下面进行测试:
第一次执行该方法,redis数据库存入了该数据,并且控制台打印了sql语句。
第二次执行该方法,控制台没有输出sql语句,说明是从redis拿出数据。
以上就是springboot2.0整合redis基于注解的使用。在下一篇,会详细讲解几种注解的使用。