多级缓存架构(二)Caffeine进程缓存

文章目录

通过本文章,可以完成多级缓存架构中的进程缓存。

一、引入依赖

item-service中引入caffeine依赖

xml 复制代码
		<dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

二、实现进程缓存

这是Caffeine官方文档地址

1. 配置Config类

创建config.CaffeineConfig

java 复制代码
@Configuration
public class CaffeineConfig {
    @Bean
    public Cache<Long, Item> itemCache(){
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(10_000)
                .build();
    }

    @Bean
    public Cache<Long, ItemStock> stockCache(){
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(10_000)
                .build();
    }
}

2. 修改controller

ItemController中注入两个Cache对象,并修改业务逻辑

java 复制代码
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemStockService stockService;
    @Autowired
    private Cache<Long, Item> itemCache;
    @Autowired
    private Cache<Long, ItemStock> stockCache;

    @GetMapping("/{id}")
    public Item findById(@PathVariable("id") Long id){
        return itemCache.get(id, key->
            itemService.query()
                    .ne("status", 3).eq("id", id)
                    .one()
        );
//        return itemService.query()
//                .ne("status", 3).eq("id", id)
//                .one();
    }

    @GetMapping("/stock/{id}")
    public ItemStock findStockById(@PathVariable("id") Long id){
        return stockCache.get(id, key->
                stockService.getById(id)
        );
//        return stockService.getById(id);
    }
}

三、运行

Idea结合Docker将springboot放入docker容器中运行,并指定使用multi-cache_multi-cache网络,以及固定172.30.3.4地址。

详细参考如下文章

启动好后,可以看到springboot容器和mysql容器处于同一网络下。(Docker Desktop for Windows插件PortNavigator)

四、测试

访问http://localhost:8081/item/10001可以看到springboot日志输出如下

txt 复制代码
02:45:58:841 DEBUG 1 --- [nio-8081-exec-1] c.h.item.mapper.ItemMapper.selectOne     : ==>  Preparing: SELECT id,name,title,price,image,category,brand,spec,status,create_time,update_time FROM tb_item WHERE (status <> ? AND id = ?)
02:45:58:889 DEBUG 1 --- [nio-8081-exec-1] c.h.item.mapper.ItemMapper.selectOne     : ==> Parameters: 3(Integer), 10001(Long)
02:45:58:951 DEBUG 1 --- [nio-8081-exec-1] c.h.item.mapper.ItemMapper.selectOne     : <==      Total: 1

当我们二次访问此网址,强制刷新+禁用浏览器缓存+更换浏览器,springboot日志都没有新的查询记录,说明使用了Caffeine缓存。

相关推荐
怣5013 分钟前
MySQL子查询零基础入门教程:从小白到上手(零基础入门版)
数据库·mysql
江湖有缘17 分钟前
零基础入门:使用 Docker 快速部署 Organizr 个人主页
java·服务器·docker
代码改善世界34 分钟前
CANN深度解构:中国AI系统软件的原创性突破与架构创新
大数据·人工智能·架构
jiunian_cn1 小时前
【Redis】数据库管理操作
数据库·redis·缓存
晚霞的不甘1 小时前
Flutter for OpenHarmony 实现计算几何:Graham Scan 凸包算法的可视化演示
人工智能·算法·flutter·架构·开源·音视频
猫头虎1 小时前
基于信创openEuler系统安装部署OpenTeleDB开源数据库的实战教程
数据库·redis·sql·mysql·开源·nosql·database
Tadas-Gao1 小时前
TCP粘包现象的深度解析:从协议本质到工程实践
网络·网络协议·云原生·架构·tcp
Nandeska2 小时前
17、MySQL InnoDB ReplicaSet
数据库·mysql
難釋懷2 小时前
秒杀优化-基于阻塞队列实现秒杀优化
redis·缓存
hlABgYML2 小时前
基于NGSIM数据的Wiedemann99跟驰模型标定
mysql