pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
bootstrap.yml配置文件
spring:
redis:
host: 192.168.50.146
port: 6379
#超时时间,单位毫秒
timeout: 60000
#0-15 16个库 默认0
database: 0
cache:
redis:
cache-names:
cache-null-values: false
key-prefix: spring:cache
time-to-live: 3600000
use-key-prefix: true
type: # Cache type. By default, auto-detected according to the environment.
spring boot启动类加上注解
@EnableCaching
serviceimpl方法redis缓存的使用
@Override
@Cacheable(cacheNames = "channelConfig")
public ChannelConfigDO query(Long id) {
ChannelConfigDO channelConfigDO=channelConfigMapper.selectById(id);
return channelConfigDO;
}
redis配置文件公共配置:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
public class RedisConfig {
@Autowired
private RedisTemplate redisTemplate;
@Bean
public RedisTemplate redisTemplateInit() {
//设置序列化Key的实例化对象
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置序列化Value的实例化对象
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
@Primary
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
//缓存配置对象
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟
.disableCachingNullValues() //如果是空值,不缓存
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) //设置key序列化器
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); //设置value序列化器
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
/**
* 定义缓存数据 key 生成策略的bean
* 包名+类名+方法名+所有参数
* @return
*/
@Bean
public KeyGenerator wiselyKeyGenerator(){
return (target, method, params) -> {
StringBuilder buff = new StringBuilder();
buff.append(target.getClass().getName());
buff.append(method.getName());
for (Object obj : params) {
buff.append(obj.toString());
}
return buff.toString();
};
}
}