SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
一、Cache缓存简介
从Spring3开始定义Cache和CacheManager接口来统一不同的缓存技术;
Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
Cache接口下Spring提供了各种缓存的实现;
如RedisCache,EhCacheCache ,ConcurrentMapCache等;
二、核心API
1、Cache缓存接口
定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等
2、CacheManager
缓存管理器,管理各种缓存(cache)组件
3、@Cacheable 主要针对方法配置,能够根据方法的请求参数对其进行缓存
Cacheable 执行流程 1)方法运行之前,按照cacheNames指定的名字先去查询Cache 缓存组件 2)第一次获取缓存如果没有Cache组件会自动创建 3)Cache中查找缓存的内容,使用一个key,默认就是方法的参数 4)key是按照某种策略生成的;默认是使用keyGenerator生成的,这里使用自定义配置 5)没有查到缓存就调用目标方法; 6)将目标方法返回的结果,放进缓存中 Cacheable 注解属性 cacheNames/value:指定方法返回结果使用的缓存组件的名字,可以指定多个缓存 key:缓存数据使用的key key/keyGenerator:key的生成器,可以自定义 cacheManager:指定缓存管理器 cacheResolver:指定缓存解析器 condition:指定符合条件的数据才缓存 unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存 sync:是否使用异步模式
4、@CacheEvict
清除缓存
CacheEvict:缓存清除 key:指定要清除的数据 allEntries = true:指定清除这个缓存中所有的数据 beforeInvocation = false:方法之前执行清除缓存,出现异常不执行 beforeInvocation = true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
5、@CachePut
保证方法被调用,又希望结果被缓存。
与@Cacheable区别在于是否每次都调用方法,常用于更新,写入
CachePut:执行方法且缓存方法执行的结果 修改了数据库的某个数据,同时更新缓存; 执行流程 1)先调用目标方法 2)然后将目标方法的结果缓存起来
6、@EnableCaching
开启基于注解的缓存
7、keyGenerator
缓存数据时key生成策略
8、@CacheConfig
统一配置本类的缓存注解的属性
三、与SpringBoot2.0整合
1、核心依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、Cache缓存配置
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Method; @Configuration public class CacheConfig { /** * 自定义 Cache 的 key 生成器 */ @Bean("oneKeyGenerator") public KeyGenerator getKeyGenerator (){ return new KeyGenerator() { @Override public Object generate(Object obj, Method method, Object... objects) { return "KeyGenerator:"+method.getName(); } } ; } }
3、启动类注解开启Cache
@EnableCaching // 开启Cache 缓存注解 @SpringBootApplication public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class,args) ; } }
4、Cache注解使用代码
1)封装增删改查接口
import com.boot.cache.entity.User; public interface UserService { // 增、改、查、删 User addUser (User user) ; User updateUser (Integer id) ; User selectUser (Integer id) ; void deleteUser (Integer id); }
2)Cache注解使用案例
import com.boot.cache.entity.User; import com.boot.cache.service.UserService; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { // 使用自定义的key生成策略 // 缓存结果key:addUser::KeyGenerator:addUser @CachePut(value = "addUser",keyGenerator="oneKeyGenerator") @Override public User addUser(User user) { return user ; } // 缓存结果key:updateUser::2 @CachePut(value = "updateUser",key = "#result.id") @Override public User updateUser(Integer id) { User user = new User() ; user.setId(id); user.setName("smile"); return user; } // 缓存结果key: selectUser::3 @Cacheable(cacheNames = "selectUser",key = "#id") @Override public User selectUser(Integer id) { User user = new User() ; user.setId(id); user.setName("cicadaSmile"); return user; } // 删除指定key: selectUser::3 @CacheEvict(value = "selectUser",key = "#id",beforeInvocation = true) @Override public void deleteUser(Integer id) { } }
5、测试代码块
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = CacheApplication.class) public class CacheTest { @Resource private UserService userService ; // 分别测试:增、改、查、删,四个方法 @Test public void testAdd (){ User user = new User() ; user.setId(1); user.setName("cicada"); userService.addUser(user) ; } @Test public void testUpdate (){ userService.updateUser(2) ; } @Test public void testSelect (){ userService.selectUser(3) ; } @Test public void testDelete (){ userService.deleteUser(3) ; } }
四、源代码地址
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 码云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置。所以 Spring Boot 最核心的 3 个注解就是:1、@Configurationorg.springframework.context.annotation.Configuration这是 Spring 3.0 添加的一个注解,用来代替 applicationContext.xml 配置文件,所有这个配置文件里面能做到的事情都可以通过这个注解所在类来进行注册。下面几个相关注解也是非常重要的!@Bean用来代替 XML 配置文件里面的 配置。@ImportResource如果有些通过类的注册方式配置不了的,可以通过这个注解引入额外的 XML 配置文件,有些老的配置文件无法通过 @Configuration 方式配置的非常管用。@Import用来引入额外的一个或者多个 @Configuration 修饰的配置文件类。@SpringBootConfiguration这个注解就是 @Configura...
- 下一篇
SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
一、文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上传,各类文档数据上传等。SpringBoot使用MultiPartFile接收来自表单的file文件,然后执行上传文件。该案例基于SpringBoot2.0中yml配置,管理文件上传的常见属性。该案例演示单文件上传和多文件上传。 二、搭建文件上传界面 1、引入页面模板Jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2、编写简单的上传页面 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <hr/> <h3>1、单文件上传</h3> <form method="POST" action...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Red5直播服务器,属于Java语言的直播服务器
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8安装Docker,最新的服务器搭配容器使用