SpringBoot @Cacheable缓存入门程序
导语
在之前的博客中分享了关于SpringBoot缓存的一些基本的概念,在这篇博客中提供一个小小的入门的实例,通过这个实例可以更好的了解关于SpringBoot缓存有关的知识点。
首先既然是缓存的使用就不得不提及关于缓存的使用场景,在实际的开发中很多的地方都是需要使用到缓存的技术。
- 1.场景1:和数据库中的数据结构保持一致,原样缓存
- 2.场景2:列表排序分页场景的缓存
- 3.场景3:较大的详情内容数据缓存
- 等等场景
在这些场景中都使用了缓存技术,当然这里并不是对于这些缓存场景进行分析,所以不做过多的解释。有兴趣的话可以了解。
在SpringBoot中对于缓存主要有两个步骤,第一开启基于注解缓存,第二标注注解。
入门实例
1.创建对应的MySql数据库和数据表。
配置对应的数据库的链接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
配置完成数据库连接之后就要建立对应的Mapper对象,当然这里使用的是基于注解的形式,当然也可以是基于配置文件的形式,这里由于功能比较简单所以说使用基于注解的方式进行配置
import com.example.cache.bean.Employee;
import org.apache.ibatis.annotations.*;@Mapper
public interface EmployeeMapper {@Select("select * from employee where id = #{id}")public Employee getEmpById(Integer id);@Update("update employee set lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} where id=#{id}")public void updateEmp(Employee employee);@Delete("delete from employee where id=#{id}")public void deleteEmpById(Integer id);@Insert("insert into employee (lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId})")public void insertEmployee(Employee employee);
}
在主类上标注扫描Mapper的注解@MapperScan通过这个注解,可以将Mapper注解扫描到Spring容器中。也就是说所有的在对应的mapper包下的标记了@Mapper注解的类都被检查。
//配置扫描包
@MapperScan("com.example.cache.mapper")
@SpringBootApplication
@EnableCaching
public class Springboot01CacheApplication {public static void main(String[] args) {SpringApplication.run(Springboot01CacheApplication.class, args);}
}
2.按照如下的目录结构创建对应的Service和Controller层。
Service层
import com.example.cache.bean.Employee;
import com.example.cache.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class EmployeeService {@AutowiredEmployeeMapper employeeMapper;/*** @Cacheable* 将方法的运行结果进行缓存,如果需要相同的数据,就可以直接从缓存中获取* 不用调用方法** CacheManager管理多个Cache组件,对缓存真正的CRUD操作在Cache组件中,每一个缓存组件* 有自己唯一一个名字* value/cacheNames:指定缓存组件的名字* key ;缓存数据时使用的key;可以用他来指定,默认使用方法参数的值 1-方法返回值* 编写SPEL表达式 #id:参数id的值, #a0,#p0,#root.args[0]* keyGenerator key的生成器,可以自己指定key的生成器的组件ID* key/keyGenerator 二选一** cacheManager 指定缓存管理器,或者指定缓存解析器cacheResolver** condition 指定复合条件的情况下才缓存** unless 否定缓存,当unless指定的条件为true,方法的返回值就不会缓存,可以获取到结果进行判断** sync 是否使用异步模式* @param id* @return*/@Cacheable(cacheNames = {"emp"})public Employee getEmp(Integer id){System.out.println("查询"+id+"号员工");return employeeMapper.getEmpById(id);}
}
注意
在这里介绍点新的东西@Cacheable注解标注这个方法的返回值是可以缓存的,也就是说可以将这个注解的标注的方法返回值作为缓存。而这个注解的源码如下
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {/*** 缓存管理器的名称.*/@AliasFor("cacheNames")String[] value() default {};=@AliasFor("value")String[] cacheNames() default {};//标注一个缓存的keyString key() default "";//key的生成器String keyGenerator() default "";//缓存管理器String cacheManager() default "";//缓存分析器String cacheResolver() default "";//当满足条件的饿时候开始缓存String condition() default "";//当条件不满足的时候开始缓存String unless() default "";//是否同步缓存boolean sync() default false;
controller层
import com.example.cache.bean.Employee;
import com.example.cache.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class EmployeeController {@AutowiredEmployeeService employeeService;@GetMapping("/emp/{id}")public Employee getEmployee(@PathVariable Integer id){Employee emp = employeeService.getEmp(id);return emp;}
}
完成以上操作之后就可以通过浏览器访问对应的localhost:8080端口执行缓存操作,当我们第一次请求两个员工的时候都会在后台打印出查询结果当然了这个需要打开对应的日志级别。
logging.level.com.example.cache.mapper=debug
如图所示。当第二次访问相同的数据的时候就不需要再通过插叙数据库所获取到。所以就不会再控制台输出查询语句。
总结
其实SpringBoot的缓存原理是很简单的,通过这个例子看到了关于缓存的基本使用方式,了解的关于@Cacheable注解的相关内容。
SpringBoot @Cacheable缓存入门程序相关推荐
- SpringBoot的Cacheable缓存入门
因为工作需要,最近开始学习SpringBoot.要使用缓存,就搜索了下SpringBoot的缓存方案,有自带的ConcurrentLinkedHashMap,还有主流的Guava.Caffeine.E ...
- SpringBoot @Cacheable缓存注解的使用
文章目录 1. 引入缓存依赖 2. application.properties 配置 3. 准备基本的Controller.Service代码 4. @Cacheable注解 4.1 cacheNa ...
- 玩转springboot:thymeleaf模板引擎入门程序
一.前言 常用的模板引擎有:JSP.Velocity.Freemarker.Thymeleaf 但是,Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.而且,语法更简单,功 ...
- 【Springboot 入门培训 】#18 SpringBoot Cache 缓存实现
目录 1 EhCache3 1 执行环境设置 2 Ehcache3 应用 3 Ehcache3 xml设置 2 Caffeine 3 Infinispan 1 SpringBoot嵌入式缓存 2 Ja ...
- 玩转springboot:入门程序
Spring Boot 入门 一.Spring Boot 简介 官网英文: Spring Boot makes it easy to create stand-alone, production-gr ...
- SpringBoot简介、SpringBoot 入门程序搭建、与JDBC、Druid、Mybatis和SpringData JPA的整合
一.SpringBoot 简介: spring boot并不是一个全新的框架,它不是spring解决方案的一个替代品,而是spring的一个封装.所以,你以前可以用spring做的事情,现在用spri ...
- SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut
SpringBoot Redis缓存 @Cacheable.@CacheEvict.@CachePut
- springboot的缓存技术
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 我门知道一个程序的瓶颈在于数据库,我门也知道内存的速度是大大快于硬盘的速度的.当我门需要重复的获取相 ...
- 详细SpringBoot教程之入门(一)
写在前面 鉴于有人留言说想要学习SpringBoot相关的知识,我这里打算写一个SpringBoot系列的相关博文,目标呢是想让看了这一系列博文的同学呢,能够对SpringBoot略窥门径,这一系列的 ...
最新文章
- 什么是产品Backlog(Product Backlog)?
- 如何实现Asp与Asp.Net共享Session
- DefaultSingletonBeanRegistry 的registerDependentBean()方法对属性注入
- 如何查看python安装路径
- Andoird自定义ViewGroup实现竖向引导界面
- 转:SQL的内连接与外连接
- Qt QString to char*
- 老显卡都涨价了,所以我把坏的显卡拿出来修
- 土壤类型空间分布数据介绍
- 推荐几款绿色无广告良心软件
- 理解FFT, STFT, 加窗的含义
- 如何查看电子元器件的丝印信息
- 加速R运算的简易方法-MRO
- k8s-污点和容忍度
- 手机移动开发大作业 -- 仿淘宝app
- 2020安洵杯——EasyCM WriteUP
- QQ也出网页版的了- WebQQ公测中
- Pycharm中,pyqt5.11.1的Qt assistant显示
- linux下查看gaussian结构,察看Gaussian全部IOp的方法
- matlab plotroc 画roc曲线