当前位置: 首页 > news >正文

055 SpringCache

文章目录

  • 缓存一致性
  • Spring Cache
    • pom.xml
    • application.yml
    • CubemallProductApplication.java
    • SpringCache改造三级分类
    • MyCacheConfig.java
    • 缓存一致性

缓存一致性

双写模式

设置过期时间
失效模式
读写锁
设置过期时间

Spring Cache

1.读模式
缓存穿透:查询一个null数据,解决:开启缓存空数据,spring.cache.redis.cache-null-values=true
缓存击穿:大量并发进来查询一个正好过期的数据,解决:加锁
缓存雪崩:大量的key同时过期 解决:加过期时间,spring.cache.redis.time-to-live=3600000
2.写模式:缓存与数据库一致
读写加锁。
引入Canal,感知到MySQL的更新去更新数据库
读多写多,直接去数据库查询就行

pom.xml

        <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

application.yml

spring:
  cache:
    type: redis
    redis:
      time-to-live: 3600000
      cache-null-values: true

CubemallProductApplication.java

package com.xd.cubemall.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableCaching
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.xd.cubemall.product.feign")
public class CubemallProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(CubemallProductApplication.class, args);
    }

}

SpringCache改造三级分类

    /**
     * 使用SpringCache改造三级分类
     * @return
     */
    @Cacheable(value={"category"}, key = "#root.method.name")
    public List<CategoryVo> getLevel1Categorys() {


            System.out.println("缓存不命中,查询数据库。。。");
            //2.缓存中没有数据,查询数据库,从数据库查询分类数据
            List<CategoryVo> categoryJsonFromDb = getCategoryJsonFromWithRedissonLock();

            return categoryJsonFromDb;





    }

MyCacheConfig.java

package com.xd.cubemall.product.config;


import org.springframework.boot.autoconfigure.cache.CacheProperties;
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;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class MyCacheConfig {

    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        //修改key和value的序列号机制
        config = config.serializeKeysWith(
                RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
        );

        config = config.serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())

        );



        //将配置文件中的所有配置进行生效
        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();
        }

        return config;

    }
}

缓存一致性

    /**
     * 三级菜单的级联更新 (缓存一致性)
     * @param category
     */
    @CacheEvict(value = "category", key = "'getLevel1Categorys'")
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
    }

相关文章:

  • cs106x-lecture14(Autumn 2017)-SPL实现
  • 【Java进阶学习 第五篇】JDK8、9中的接口新特性
  • ARM Cortex-M3 技术解析:核寄存器R1-R15介绍及使用
  • 第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
  • kafka+spring cloud stream 发送接收消息
  • 华为OD机试真题-相对开音节-OD统一考试(E卷)
  • Meterpreter之getsystem命令提权原理详解
  • Zotero 快速参考文献导出(特定期刊引用)
  • 区块链相关方法-波士顿矩阵 (BCG Matrix)
  • Codes 开源免费研发项目管理平台 2025年第一个大版本3.0.0 版本发布及创新的轻IPD实现
  • 在LangFlow中集成OpenAI Compatible API类型的大语言模型
  • 不同类型的网站选择不同的服务器,那么应该怎么选择服务器呢?
  • STM32-心知天气项目
  • python包重要修改
  • 如何把windows机器作为SSH客户端免密登录
  • Markdown使用方法文字版解读
  • 数据表的存储过程和函数介绍
  • OpenBMC:BmcWeb app.run
  • tortoiseGit的使用和上传拉取
  • 使用docker开发镜像编译
  • 央视起底“字画竞拍”网络传销案:涉案44亿元,受害者众多
  • 梅花奖在上海|舞剧《朱鹮》,剧里剧外都是生命的赞歌
  • 中国—美国经贸合作对接交流会在华盛顿成功举行
  • 从《让·桑特伊》到《追忆》,假故事的胜利
  • 加拿大新政府宣誓就职
  • 西北大学副校长成陕西首富?旗下巨子生物去年净利超20亿,到底持股多少