2019独角兽企业重金招聘Python工程师标准>>>

这里新增加一个例子:

`

package com.atguigu.cache.service;import com.atguigu.cache.bean.Employee;
import com.atguigu.cache.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;@CacheConfig(cacheNames = "emp")
[@Service](https://my.oschina.net/service)
public class EmployeeService {@AutowiredEmployeeMapper employeeMapper;/*** 将方法的运行结果进行缓存,以后在要相同的数据,直接从缓存中获取,不用调用方法** CacheManager管理多个Cache组件的,对缓存的真正CRUD操作在Cache组件中,每一个缓存* 组件有自己唯一一个名字** 几个属性:*   cacheName/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,是数组的方式,可以指定多个缓存;*   key:缓存数据使用的key,可以用它来指定,默认是使用方法参数的值 1-方法的返回值*     编写SqEL; #id,参数id的值  #a0  #p0  #root.args[0]*   keyGenerator:key的生成器;可以自己指定key的生成器的组件*     key/keyGenerator:二选一使用*     cacheManager:缓存管理器;或者cacheResolver指定获取解析器*     condition:指定符合条件的情况下才缓存;*       condition="#id>0"*       condition="#a0>1"l;第一个参数的值>1 的时候才进行缓存*       (a0等于arg[0])*     unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;可以*     获取到结果进行判断unless="#a0==2":如果第一参数的值是2,结果不缓存*     sync:是否使用异步模式:异步模式不支持unless** 原理:*   1.缓存的自动配置类;CacheAutoConfiguration*   2.缓存的配置类*   org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration*   org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration*   org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration*   org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration*   org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration*   org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration*   org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration*   org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration*   org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默认】*   org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration*   3.哪个配置类生效; SimpleCacheConfiguration**   4.给容器中注册了一个CacheManager、ConcurrentMapCacheManager*   5.可以获取和创建ConcurrentMapCache类型的缓存组件;他的作用将数据保存在ConcurrentMap中;**   运行流程:*   @Cacheable:*   1.方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;*     (CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建,*   2.去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;*     key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用*       SimpleKeyGenerator生成key的默认策略;*         如果没有参数;key=new SimpleKey();*         如果有一个参数,key=参数的值*         如果有多个参数:key=new SimpleKey(params);*   3.没有查到缓存就调用目标方法;*   4.将目标方法的缓存结果,放进缓存中**   @Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,*   如果没有就运行方法并将结果放入缓存,以后再来调用就可以直接使用缓存中的数据**   核心:*     1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件*     2)、key使用keyGenerator生成的,默认是SimpleKeyGenerator* @param id* @return*/@Cacheable(value = {"emp"}/*,keyGenerator = "myKeyGenerator",condition = "#a0>1",unless = "#a0==2"*/)public Employee getEmp(Integer id){System.out.println("查询"+id+"号员工");Employee emp=employeeMapper.getEmpById(id);return emp;}/*** @CachePut:即调用方法,又更新缓存数据:同步更新缓存 注意key要相同* 修改了数据库的某个数据,同时更新缓存;* 运行时机:*   1、先调用目标方法*   2、将目标方法的结果缓存起来** 测试步骤:*   1、查询1号员工;查到的结果会放在缓存中*          key:1  value: lastName:张三*   2、以后查询还是之前的结果*   3、更新1号员工;【LastName:zhangsan:gender:0】*          将方法的返回值也放进缓存了;*          key: 传入的employee对象 值:返回employee对象;*   4、查询1号员工?*     应该是更新后的员工;*       key = "#employee.id":使用传入的参数的员工的id;*       key = "#result.id";使用返回后的id*              @Cacheable的key是不能用#result:*              原因:因为@Cacheable运行时机有两个,一个是在运行方法之前*                   一个是在运行方法之后,我们要在方法一开始就得到key,Put*                   是先执行方法的,而able是先执行注解的*     为什么是没更新前的?【1号员工更新了,只是用的key不一样】*/@CachePut(/*value = "emp",*/key = "#result.id")public Employee updateEmp(Employee employee){System.out.println("updateEmp:"+employee);employeeMapper.updateEmp(employee);return employee;}/*** @CacheEvict:缓存清除*   key:指定要清除的数据*   allEntries=true:指定清除这个缓存中所有数据*   beforeInvocation=false:缓存的清除是否在方法之前执行*     默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就*     不会清除**   beforeInvocation = true:*     代表清除缓存操作是在方法运行之前执行,无论是否出现异常,缓存都清除*/@CacheEvict(/*value = "emp",*/beforeInvocation = true/*,key = "#id"*/)public void deleteEmp(Integer id){System.out.println("deleteEmp:"+id);/*employeeMapper.deleteEmpById(id);*/}/*** @Caching 定义复杂的缓存规则* 这里意味着当你再用id或email调用时,则不需要再执行SQL语句* @param lastName* @return*/@Caching(cacheable = {@Cacheable(/*value = "emp",*/key = "#lastName")},put = {@CachePut(/*value = "emp",*/key = "#result.id"),@CachePut(/*value = "emp",*/key = "#result.email")//有@CachePut出现,下面的方法就一定会执行})  //你依照id,邮箱,名字都可以获得该valuepublic Employee getEmpByLastName(String lastName){System.out.println("复杂缓存");return employeeMapper.getEmpByLastName(lastName);}
}

` 这里主要说了SpringCache注解的功能,并且更加详细

但是我在另一项目中使用这些注解却出现了一些意外主要是因为key原本是String类型,但是我没有将负值key为id的属性转成String,这里则说明有些需要转型,有些则不需要:

`

package cn.enilu.subor.service.system.impl;import cn.enilu.subor.bean.entity.system.Video;
import cn.enilu.subor.dao.system.VideoRepository;
import cn.enilu.subor.service.system.VideoService;
import cn.enilu.subor.utils.factory.MutiStrFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;import java.util.List;@CacheConfig(cacheNames = "video")
@Service
public class VideoServiceImpl implements VideoService {@Autowiredprivate VideoRepository videoRepository;@Overridepublic Page<Video> findAll(Pageable var1) {return videoRepository.findAll(var1);}@Override//@Cacheable(value = {"video"})public List<Video> findAll() {return videoRepository.findAll();}@Override@CachePut(key = "#video.id.toString()")public Video save(Video video) {return videoRepository.save(video);}@Override@CacheEvict(key = "#id.toString()")public void delete(Integer id) {System.out.println("dddddddddddddddddddd"+id);videoRepository.delete(id);}@CachePut(key = "#video.id.toString()")public void update(Video video){System.out.println("uuuuuuu");System.out.println("38218401779274902148901249012"+video);Integer videoId=video.getId();//String title=video.getTitle();String video_introduce=video.getVideoIntroduce();String video_type=video.getVideoType();Integer video_class=video.getVideoClass();//String video_url=video.getVideoUrl();//String img_title=video.getImgTitle();// String img_url=video.getImgUrl();videoRepository.updateById2(video_class,video_introduce,video_type,videoId);}}

`

这里在附上注解的源码,key的类型为String

`

package org.springframework.cache.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CachePut {@AliasFor("cacheNames")String[] value() default {};@AliasFor("value")String[] cacheNames() default {};String key() default "";String keyGenerator() default "";String cacheManager() default "";String cacheResolver() default "";String condition() default "";String unless() default "";
}

`

转载于:https://my.oschina.net/u/3843989/blog/1831469

SpringBoot+SpringCache+Redis补充相关推荐

  1. [Springboot]SpringCache + Redis实现数据缓存

    前言 本文实现了SpringCache + Redis的集中式缓存,方便大家对学习了解缓存的使用. 本文实现: SpringCache + Redis的组合 通过配置文件实现了自定义key过期时间:k ...

  2. 一篇掌握SpringBoot+SpringCache+Redis超详细实例

    1.创建maven项目 2.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  3. springboot redis 刷新时间_「SpringBoot实战」SpringCache + Redis实现数据缓存

    关注我的微信公众号:后端技术漫谈 不定期推送关于后端开发.爬虫.算法题.数据结构方面的原创技术文章,以及生活中的逸闻趣事. 我目前是一名后端开发工程师.主要关注后端开发,数据安全,网络爬虫,物联网,边 ...

  4. Springboot整合redis(lettuce)

    springboot 整合redis(lettuce) 首先确保电脑上装了redis.最好能用redisDesktop查看一下数据情况 redis是一款非常流行的Nosql数据库.redis的功能非常 ...

  5. SpringBoot整合Redis缓存

    SpringBoot整合Redis缓存 一.缓存概念知识 1.是什么缓存 2.缓存的优缺点 3.为什么使用缓存 二.Redis概念知识 1.Redis简介 2.为什么用Redis作为缓存 3.Redi ...

  6. springboot连接redis集群

    开启redis服务和客户端 查看下当前redis的进程 [root@localhost ~]# ps -ef | grep redis 启动redis服务 [root@localhost ~]# cd ...

  7. SpringBoot整合Redis+Redis缓存应用+Redis实现Session共享+...

    一.SpringBoot整合Redis 1.导入依赖 <!--存在Redis依赖--> <dependency><groupId>org.springframewo ...

  8. Java项目:实现个人博客系统(java+springboot+mybatis+redis+vue+elementui+Mysql)

    源码获取:博客首页 "资源" 里下载! springboot+mybatis+前端vue,使用前后端分离架构实现的个人博客系统,共7个模块,首页,写博客,博客详情页,评论管理,文章 ...

  9. SpringBoot第九篇: springboot整合Redis

    这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...

最新文章

  1. 2022-2028年中国金属薄膜行业市场深度监测及投资潜力研究报告
  2. [WCF REST] 解决资源并发修改的一个有效的手段:条件更新(Conditional Update)
  3. CentOS 6.2下log4cplus的使用
  4. ASN.1 Editor
  5. 博士申请 | 上海交通大学叶南阳助理教授招收机器学习方向博士生
  6. python内存管理可以使用del_Python深入学习之内存管理
  7. JFinal针对ORACLE的timestamp字段解决办法
  8. javaScript的常见document对象
  9. 获取 python import模块的路径
  10. (8)Linux(客户端)和Windows(服务端)下socket通信实例
  11. 程序员的自我进化:共享经济新模式——共享员工
  12. 前端CSS样式去除body默认边距和a标签下划线去除和高亮显示问题
  13. 新的网络架构按下“快进键” 快步走入互联网下半场
  14. 为什么需要API接口开发?
  15. PS教程-photoshop入门基础应用技术服务支持
  16. Oracle命名规范
  17. CSS 布局 问题 及 解答
  18. 中国人保驰茂肥业承保产品责任险,为消费者保驾护航!
  19. C++ Release版软件 程序运行丢失MSVCR120D.dll的解决方法
  20. jupyter notebook 中的 markdown 语法

热门文章

  1. 和csm_CSM媒介研究首发短视频用户价值研究报告
  2. mysql基础表和修理表_MySQL基础知识——创建数据库和表
  3. jz2440-uboot-201204版本移植【学习笔记】【原创】
  4. Spring的两种动态代理:Jdk和Cglib 的区别和实现
  5. 第4课 - 深入浅出处理器(续)
  6. python : HTML+CSS (定时器轮寻)
  7. C#中5步完成word文档打印的方法
  8. c#多维数组的建立及操作 总结
  9. dart初始化项目的分析
  10. IOS的Alamofire5.4高版本上传图片