package com.alatus.mall.product.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
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;
@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class RedisCacheConfig {
@Autowired
private CacheProperties cacheProperties;
// 开发流程:引入依赖,写配置,使用注解
// 写配置部分,CacheAutoConfiguration会导入RedisCacheConfiguration
// 需要我们在yml或者properties指定我们使用的是spring.cache.redis
// @Cacheable触发将数据库保存到缓存的操作
// @CacheEvict触发将数据从缓存删除
// @CachePut不影响方法执行更新缓存
// @Caching组合以上多个操作
// @CacheConfig在类级别分享缓存的相同配置
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
// CacheAutoConfiguration会自动导入RedisCacheConfiguration(通过IOC容器)
// RedisCacheManager初始化所有的缓存,每个缓存具体使用什么
// 想改缓存的配置,只需要给容器放一个RedisCacheManager即可
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
// 将配置文件中的所有配置都生效
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return config;
}
}
package com.alatus.mall.product.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
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;
@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class RedisCacheConfig {
@Autowired
private CacheProperties cacheProperties;
// 开发流程:引入依赖,写配置,使用注解
// 写配置部分,CacheAutoConfiguration会导入RedisCacheConfiguration
// 需要我们在yml或者properties指定我们使用的是spring.cache.redis
// @Cacheable触发将数据库保存到缓存的操作
// @CacheEvict触发将数据从缓存删除
// @CachePut不影响方法执行更新缓存
// @Caching组合以上多个操作
// @CacheConfig在类级别分享缓存的相同配置
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
// CacheAutoConfiguration会自动导入RedisCacheConfiguration(通过IOC容器)
// RedisCacheManager初始化所有的缓存,每个缓存具体使用什么
// 想改缓存的配置,只需要给容器放一个RedisCacheManager即可
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
// 将配置文件中的所有配置都生效
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return config;
}
}
spring:
cache:
type: redis
redis:
time-to-live: 3600000
# 前缀名字
key-prefix: CACHE_
# 是否使用前缀,如果我们指定前缀就使用指定前缀
use-key-prefix: true
# 是否缓存空值,这样可以解决缓存穿透的问题
cache-null-values: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
datasource:
username: root
password: root
url: jdbc:mysql://192.168.56.10:3306/Mall-pms
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: true
mvc:
static-path-pattern: /static/**
redis:
host: 192.168.56.10
port: 6379
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
logic-delete-value: 1
logic-not-delete-value: 0
server:
port: 10000
logging:
level:
com.alatus.mall.product: error
spring:
cache:
type: redis
redis:
time-to-live: 3600000
# 前缀名字
key-prefix: CACHE_
# 是否使用前缀,如果我们指定前缀就使用指定前缀
use-key-prefix: true
# 是否缓存空值,这样可以解决缓存穿透的问题
cache-null-values: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
datasource:
username: root
password: root
url: jdbc:mysql://192.168.56.10:3306/Mall-pms
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: true
mvc:
static-path-pattern: /static/**
redis:
host: 192.168.56.10
port: 6379
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
logic-delete-value: 1
logic-not-delete-value: 0
server:
port: 10000
logging:
level:
com.alatus.mall.product: error