SpringBoot使用分布式缓存
如果对SpringBoot缓存不熟悉的建议先看第一片文章SpringBoot使用caffeine作为缓存,为什么使用分布式缓存?在实际开发场景中,往往单机应用无法满足当前的需求,需要对项目进行分布式部署,由此,每个项目中的缓存都是属于自己独立服务的,并不能共享,其次,当某个服务更新了缓存,其他服务并不知道,当用户请求到其他服务时,获取到的往往还是旧的数据,说到这,就有人会说使用Redis进行代替,但是Redis毕竟是借助于第三方,会存在网络消耗,如果所有都堆积到Redis,会造成Redis被大量并发访问,最坏会被宕机,所以我们可以使用本地缓存和Redis缓存结合进行使用,运用Redis的发布订阅功能进行通知其他服务更新缓存.
接下来简单介绍本人开源的一个分布式缓存使用方法
一. 引入依赖
<dependency> <groupId>cn.gjing</groupId> <artifactId>tools-redis</artifactId> <version>1.1.0</version> </dependency>
二. 启动类标上注解
/** * @author Gjing */ @SpringBootApplication @EnableSecondCache public class TestRedisApplication { public static void main(String[] args) { SpringApplication.run(TestRedisApplication.class, args); } }
三. 配置
1. 配置介绍
以下为所有配置信息, 使用时可自定义设置, 皆以second.cache
开头
配置项 | 描述 |
---|---|
cache-names | 缓存key名 |
cache-value-nullable | 是否存储控制,默认true ,防止缓存穿透 |
dynamic | 是否动态根据cacheName创建Cache实现, 默认true |
cache-prefix | 缓存key的前缀 |
caffeine.initial-capacity | 初始化大小 |
caffeine.expire-after-access | 访问后过期时间,单位毫秒 |
caffeine.expire-after-write | 写入后过期时间,单位毫秒 |
caffeine.maximum-size | 最大缓存对象个数,超过此数量时之前放入的缓存将失效 |
caffeine.refresh-after-write | 写入后刷新时间,单位毫秒 |
redis.every-cache-expire | 每个cacheName的过期时间,单位秒,优先级比expire 高 |
redis.expire | 全局过期时间,单位秒,默认不过期 |
redis.topic | 缓存更新时通知其他节点的topic名称 |
2. 配置示例
- yml方式
second: cache: cache-prefix: 锁的前缀 redis: expire: 10 caffeine: expire-after-write: 3000
- JavaBean方式
/** * @author Gjing **/ @Configuration public class CacheConfiguration { @Bean public SecondCache secondCache() { return SecondCache.builder() .cachePrefix("锁的前缀") .dynamic(true) .build(); } @Bean public RedisCache redisCache() { return RedisCache.builder() .expire(10) .build(); } @Bean public CaffeineCache caffeineCache() { return CaffeineCache.builder() .expireAfterWrite(3000) .build(); } }
三. 简单使用
/** * @author Gjing **/ @Service @Slf4j public class CustomService { @Resource private CustomRepository customRepository; /** * 获取一个用户 * @param customId 用户id * @return Custom */ @Cacheable(value = "user",key = "#customId") public Custom getCustom(Integer customId) { log.warn("查询数据库用户信息"); return customRepository.findById(customId).orElseThrow(() -> new NullPointerException("User is not exist")); } /** * 删除一个用户 * @param customId 用户id */ @CacheEvict(value = "user", key = "#customId") public void deleteUser(Integer customId) { Custom custom = customRepository.findById(customId).orElseThrow(() -> new NullPointerException("User is not exist")); customRepository.delete(custom); } }
四. 定义接口调用
/** * @author Gjing **/ @RestController public class CustomController { @Resource private CustomService customService; @GetMapping("/user/{custom-id}") @ApiOperation(value = "查询用户",httpMethod = "GET") public ResponseEntity getUser(@PathVariable("custom-id") Integer customId) { return ResponseEntity.ok(customService.getCustom(customId)); } @DeleteMapping("/user") @ApiOperation(value = "删除用户", httpMethod = "DELETE") @ApiImplicitParam(name = "customId", value = "用户Id", dataType = "int", required = true, paramType = "Query") @NotNull public ResponseEntity deleteUser(Integer customId) { customService.deleteUser(customId); return ResponseEntity.ok("Successfully delete"); } }
调用结果
使用中如果有任何问题,欢迎评论留言,我会及时回复以及更新,源代码地址:tools-redis
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Nacos 1.0.1发布,社区一起定义Nacos
Nacos 1.0.1是GA之后的第一个版本,除了修复1.0.0一些bug以外,还在社区反馈的基础上,吸收了大量的社区共建PR。这个版本中除了原有的贡献者外,新增了13名社区的贡献者,总计合入了43个PR。这一次,社区一起定义Nacos。或许你觉得Nacos很多槽点,或许你觉得Nacos应该支持某些牛逼的功能,此时请不要隐藏你的才华,贡献你在Nacos的第一PR吧。 Nacos 1.0.1主要新增特性介绍 服务发现SDK新增服务的运维接口 @chuntaojunNacos的服务模式是由服务-集群-实例组成的三层模型,和以往的Eureka、Consul等注册中心有所区别,这样虽然增加了对更多场景的支持,但是目前只能通过控制台去操作服务和集群的配置,包括创建、更新和删除服务。为了能让用户更加方便的使用Nacos的数据模型,我们在1.0.1版
- 下一篇
# Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。 输入为非空字符串且只包含数字 1 和 0。 Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" 解题思路(Java): Java:由于Java语言不像 C/C++ ,Java字符串 String 不可变,比较字符串不能用 “=” ,”=“ 会比较字符串是否为同一个对象,而不是比较字符串内容是否相同。StringBuilder 可操作性较好,可用来记录每一位数 相加后的最终值。 所以这道题输入字符串可利用...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS关闭SELinux安全模块
- CentOS8编译安装MySQL8.0.19
- CentOS7设置SWAP分区,小内存服务器的救世主
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题