商城项目分布式缓存使用Spring Cache自定义配置信息并解决内存穿透问题-----商城项目

#程序员如何平衡日常编码工作与提升式学习?#
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
  • 39
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值