Spring-Cache 缓存

1.简介

 2.SpringCache 整合

简化缓存开发

1.导入依赖

 <!-- spring cache        --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>

2.redis 作为缓存场景

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

3. 配置类

1.自动配置

自动配置了哪些

CacheAutoConfiguration 自动导入 RedisCacheConfiguration

自动配好了缓存管理器  RedisCacheManager

2.手动需要得yaml配置

spring:
#缓存cache:type: redis

3.测试 使用缓存

1.开启缓存
 1.使用redis作为缓存
spring:
#缓存cache:type: redis
@EnableCaching //放在主启动类上
2.使用注解就可以完成缓存
 //每一个需要缓存的数据我们都来指定要放到哪个名字的缓存 [缓存的分区(按照业务类型分区)]@Cacheable({"category"}) //代表这个数据是可以缓存的 当前方法的结果需要缓存,如果缓存中有,方法不用调用。//如果缓存中没有,调用方法,将方法结果放入缓存@Overridepublic List<CategoryEntity> getLevel1Category() {System.out.println("调用了数据库getLevel1Category");List<CategoryEntity> parentCid = this.list(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));return parentCid;}

    //默认行为//1.如果缓存中有,方法不用调用//2.key默认自动生成 : 缓存的名字::SimpleKey [](自主生产的key的值)//3.缓存的value的值 默认使用JDK序列化机制 将序列化后的数据存入Redis//4.默认ttl时间 -1;

 

//自定义//1. 指定生成的缓存使用的key key 属性指定,接受一个SPEL 表达式  ${}  #{}//2. 指定缓存的过期时间 yml 配置文件中修改ttl//3. 将数据存入JSON标准格式

 

 

    public  String mycategorykey="我自定义的key";@Override
//    @Cacheable(value = {"category"}, key = "'level1Category'")
//    @Cacheable(value = {"category"}, key = "#root.method.name")//root是当前上下文的意思 method 是方法 可以有参数 各种东西@Cacheable(value = {"category"}, key = "#root.target.mycategorykey")//root 可以拿到当前对象 当前方法 当前参数public List<CategoryEntity> getLevel1Category() {System.out.println("调用了数据库getLevel1Category");List<CategoryEntity> parentCid = this.list(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));return parentCid;}

 2.自定义缓存设置

保存的数据为json格式

1.讲一下 缓存redis配置类的 原理 

CacheAutoConfiguration->RedisCacheConfiguration->自动配置了缓存管理器 RedisCacheManager->初始化所有的缓存->每个缓存觉得使用什么配置->如果RedisCacheConfiguration有在 容器中自己 配置,就要用自己的配置,否则就用默认的配置

所以,我们只需要给容器中放入一个RedisCacheConfiguration即可

就会应用到当前缓存管理器的所有缓存中

2.配置类
package com.jmj.gulimall.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 {/*** 给了默认配置文件就不生效了* 因为 条件判断了 if config !=null  就返回* 也不需要加@EnableConfigurationProperties(CacheProperties.class)* 因为默认自动装配类已经加入这个* @return*/@Beanpublic RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();/*** public RedisCacheConfiguration entryTtl(Duration ttl) { 是new新对象 得要覆盖上* 		return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair,* 				valueSerializationPair, conversionService);*        }*/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;}}
#配置数据源
spring:
#缓存cache:redis:time-to-live: 36000000  #单位 ms 先设定一个小时过期时间use-key-prefix: false #不使用,原来的前缀就没有了 key是什么 就是什么key-prefix: CACHE_ #所有Key都设置一个前缀来区分  如果指定了 前缀就用指定的,没有就用默认的 name::[]cache-null-values: true #是否缓存入空值 可以解决缓存穿透type: redis
3.实现 
  //每一个需要缓存的数据我们都来指定要放到哪个名字的缓存 [缓存的分区(按照业务类型分区)]//代表这个数据是可以缓存的 当前方法的结果需要缓存,如果缓存中有,方法不用调用。//如果缓存中没有,调用方法,将方法结果放入缓存//默认行为//1.如果缓存中有,方法不用调用//2.key默认自动生成 : 缓存的名字::SimpleKey [](自主生产的key的值)//3.缓存的value的值 默认使用JDK序列化机制 将序列化后的数据存入Redis//4.默认ttl时间 -1;//自定义//1. 指定生成的缓存使用的key key 属性指定,接受一个SPEL 表达式   #{}//2. 指定缓存的过期时间 yml 配置文件中修改ttl//3. 将数据存入JSON标准格式public  String mycategorykey="我自定义的key";@Override
//    @Cacheable(value = {"category"}, key = "'level1Category'")
//    @Cacheable(value = {"category"}, key = "#root.method.name")//root是当前上下文的意思 method 是方法 可以有参数 各种东西@Cacheable(value = {"category"}, key = "#root.target.mycategorykey")//root 可以拿到当前对象 当前方法 当前参数public List<CategoryEntity> getLevel1Category() {System.out.println("调用了数据库getLevel1Category");//线程不安全,需要加分布式锁和读写锁List<CategoryEntity> parentCid = this.list(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));return parentCid;}
1.删除缓存
    /*** 级联更新所有关联数据* @param category* @throws Exception*/@CacheEvict(value = "category", key = "'category::tree'")//删除缓存@Override@Transactional(rollbackFor = Exception.class)public void updateDetails(List<CategoryEntity> category) throws Exception {category.stream().filter(c -> StringUtils.isNotBlank(c.getName())).forEach(c -> {List<CategoryBrandRelationEntity> updateList = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", c.getCatId())).stream().peek(r -> r.setCatelogName(c.getName())).collect(Collectors.toList());categoryBrandRelationService.updateBatchById(updateList);});this.updateBatchById(category);}
2.存入 
  @Override@Cacheable(value = {"category"}, key = "'category::tree'")public List<CategoryEntity> listWithTree() {//1.查出所有分类System.out.println("三级分类查询数据库");List<CategoryEntity> all = this.list();//2.组装成父子的属性结构List<CategoryEntity> level1Menus = all.stream().filter(c -> c.getParentCid().equals(0L)).map(categoryEntity -> categoryEntity.setChildren(getChildrenCategory(all, categoryEntity.getCatId())))//大于放后面 升序.sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());return level1Menus;}
3.要操作多个 
//    @CacheEvict(value = "category", key = "'category::tree'")//删除缓存//如果多操作的话@Caching(evict = {@CacheEvict(value = "category", key = "'category::tree'"),@CacheEvict(value = "category", key = "#root.target.mycategorykey")})@Override@Transactional(rollbackFor = Exception.class)public void updateDetails(List<CategoryEntity> category) throws Exception {category.stream().filter(c -> StringUtils.isNotBlank(c.getName())).forEach(c -> {List<CategoryBrandRelationEntity> updateList = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", c.getCatId())).stream().peek(r -> r.setCatelogName(c.getName())).collect(Collectors.toList());categoryBrandRelationService.updateBatchById(updateList);});this.updateBatchById(category);}
4.删除这个分区下所有数据 (失效模式)

存储同一类型的数据,都可以指定一个分区

    @CacheEvict(value = "category", allEntries = true)// 设定了 use-key-prefix: false 这个如果没有这个分类,将全部缓存删除
use-key-prefix必须要为true  和  key-prefix: 不设置 这个才有用
5.双写模式

@CachePut//双写模式

3.SpringCache的不足

基本都能解决,唯独缓存击穿特别一点

 这样就是加了个本地锁,本地也就是放一个过来

  @Override@Cacheable(value = {"category"}, key = "'tree'",sync = true)public List<CategoryEntity> listWithTree() {//1.查出所有分类System.out.println("三级分类查询数据库");List<CategoryEntity> all = this.list();//2.组装成父子的属性结构List<CategoryEntity> level1Menus = all.stream().filter(c -> c.getParentCid().equals(0L)).map(categoryEntity -> categoryEntity.setChildren(getChildrenCategory(all, categoryEntity.getCatId())))//大于放后面 升序.sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());return level1Menus;}

只有cacheable 查询注解的时候 才能够加锁

虽然加的是本地锁,但是一台服务器只能一个访问,也是够用了

4.总结

常规数据(读多写少,即时性,一致性要求不高的数据 ):完全可以使用springcache(写模式:只有数据有过期时间 就完全足够了 这样可以保证数据的最终一致性)

特殊数据 (特殊设计)例如 canal 感知数据库去更新 缓存

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/872766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二分法binary search

欢迎来到一夜看尽长安花 博客&#xff0c;您的点赞和收藏是我持续发文的动力 对于文章中出现的任何错误请大家批评指出&#xff0c;一定及时修改。有任何想要讨论的问题可联系我&#xff1a;3329759426qq.com 。发布文章的风格因专栏而异&#xff0c;均自成体系&#xff0c;不足…

解决一下git clone失败的问题

1&#xff09;.不开梯子&#xff0c;我们用https克隆 git clone https://github.com 报错&#xff1a; Failed to connect to github.com port 443 after 2091 ms: Couldnt connect to server 解决办法&#xff1a; 开梯子&#xff0c;然后# 注意修改成自己的IP和端口号 gi…

Java时间练习(7) (2024.7.17)

LocalDate、LocalTime、LocalDateTime类 package LocalExercise20240717; import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;public class LocalExercise {public static void main(Stri…

跟ChatGPT学习go语言-利用Go语言封装一个adb命令函数

问&#xff1a;在go语言中&#xff0c;根据不同的操作系统&#xff0c;选择adb文件路径来进行执行&#xff0c;封装成一个函数 package mainimport ("fmt""os/exec""runtime" )// GetADBPath 根据操作系统获取 ADB 可执行文件的路径 func GetAD…

docker搭建普罗米修斯监控gpu

ip8的服务器监控ip110和ip111的服务器 被监控的服务器110和111只需要安装node-export和nvidia-container-toolkit 下载镜像包 docker pull prom/node-exporter docker pull prom/prometheus docker pull grafana/grafana新建目录 mkdir /opt/prometheus cd /opt/prometheus/…

生信软件27 - 基于python的基因注释数据查询/检索库mygene

1. mygene库简介 MyGene.info提供简单易用的REST Web服务来查询/检索基因注释数据&#xff0c;具有以下特点&#xff1a; mygene技术文档&#xff1a; https://docs.mygene.info/en/latest/ 多物种支持: 包括人、小鼠、大鼠、斑马鱼等多个模式生物&#xff1b; 多数据源聚合…

卷积神经网络图像识别车辆类型

卷积神经网络图像识别车辆类型 1、图像 自行车: 汽车: 摩托车: 2、数据集目录 3、流程 1、获取数据,把图像转成矩阵,并随机划分训练集、测试集 2、把标签转为数值,将标签向量转换为二值矩阵 3、图像数据归一化,0-1之间的值 4、构造卷积神经网络 5、设置图像输入…

记录些MySQL题集(8)

ACID原则、事务隔离级别及事务机制原理 一、事务的ACID原则 什么是事务呢&#xff1f;事务通常是由一个或一组SQL组成的&#xff0c;组成一个事务的SQL一般都是一个业务操作&#xff0c;例如聊到的下单&#xff1a;「扣库存数量、增加订单详情记录、插入物流信息」&#xff0…

Qt5.12.2安装教程

文章目录 文章介绍下载连接安装教程 文章介绍 安装Qt5.12.2 下载连接 点击官网下载 安装包下载完毕 安装教程 点开设置&#xff0c;添加临时储存库&#xff0c;复制连接“https://download.qt.io/online/qtsdkrepository/windows_x86/root/qt/” 点击测试&#xff0…

谷歌搜索引擎:知识殿堂与全球网络连通的桥梁

谷歌搜索引擎&#xff0c;其标志性的名称已成为互联网的象征。每次启动电脑&#xff0c;我总会习惯性地在洁白的搜索框内输入疑惑。谷歌如同一位博学多才的朋友&#xff0c;总能迅捷地提供满意解答。其界面设计简约明快&#xff0c;仅有标志性的彩色"Google"字样与搜…

HDFS和ES

HDFS&#xff08;Hadoop Distributed File System&#xff09;和 Elasticsearch&#xff08;ES&#xff09;是两种不同类型的分布式系统&#xff0c;分别用于存储和搜索数据。它们在设计目标、架构、使用场景等方面有显著的区别。以下是对 HDFS 和 ES 的详细比较&#xff1a; …

孟德尔随机化——混杂SNP剔除之LDlink(1)

1、注册&#xff1a;LDlink | An Interactive Web Tool for Exploring Linkage Disequilibrium in Population Groups 邮箱会收到12个字符串的token&#xff0c;使用时需要提供token 2、包内方法 LDexpress Determine if genomic variants are associated with gene express…

352_C++_管理用户登录计数器的类,其中有定时器m_timer操作,定时修改【登录锁定时间】和【错误次数】

~~ 头文件 #pragma once#include "commonfunction.h" #include <boost/asio/steady_timer.hpp> #include <set> #include

第13章 更多的结构化命令《Linux命令行与Shell脚本编程大全笔记》

13.1 For命令 格式&#xff1a;for var in list;dofor命令默认按照空格、制表符、换行符作为字段分隔符区分单个值&#xff0c;如果某个值含有空格要使用双引号从命令中读取值列表for state in $(cat $file)更改字段分隔符IFS(internal field separator)IFS$\n可能的需求&…

set类和map类介绍和简单使用

目录 set类介绍与简单使用 set类 multiset类 map类介绍与简单使用 map类 multimap类 set类介绍与简单使用 set类是一种关联式容器&#xff0c;在数据检索时比序列式容器效率更高。本质是一个常规的二叉搜索树&#xff0c;但是为了防止出现单支树导致效率下降进行了相关优…

【Linux】将IDEA项目部署到云服务器上,让其成为后台进程(保姆级教学,满满的干货~~)

目录 部署项目到云服务器什么是部署一、 创建MySQL数据库二、 修改idea配置项三、 数据打包四、 部署云服务器五、开放端口号六 、 验证程序 部署项目到云服务器 什么是部署 ⼯作中涉及到的"环境" 开发环境:开发⼈员写代码⽤的机器.测试环境:测试⼈员测试程序使⽤…

SQL面试题-留存率计算

表定义&#xff1a; create table if not exists liuliang_detail (user_id string comment ,record_time string comment yyyymmdd hh:mi:ss ) comment 流量明细表 ; 方法一&#xff1a; 计算的是整段时间范围内&#xff0c;每一天为基准的所有的留存1、2、7天的用户数。 …

WEB前端05-JavaScrip基本对象

JavaScript对象 1.Function对象 函数的创建 //方法一&#xff1a;自定义函数 function 函数名([参数]) {函数体[return 表达式] }//方法二&#xff1a;匿名函数 (function([参数]) {函数体[return 表达式] }); **使用场景一&#xff1a;定义后直接调用使用(只使用一次) (fun…

GitHub每日最火火火项目(7.17)

项目名称&#xff1a;aider 项目介绍&#xff1a;aider 是一个在终端中实现 AI 结对编程的项目。它能够为开发者提供智能的编程辅助&#xff0c;帮助开发者更高效地完成编程任务。通过与 AI 的协作&#xff0c;开发者可以获得实时的代码建议、错误修复提示等&#xff0c;从而提…

成为git砖家(1): author 和 committer 的区别

大家好&#xff0c;我是白鱼。一直对 git author 和 committer 不太了解&#xff0c; 今天通过 cherry-pick 的例子搞清楚了区别。 原理 例如我克隆了著名开源项目 spdlog 的源码&#xff0c; 根据某个历史 commit A 创建了分支&#xff0c; 然后 cherry-pick 了这个 commit …