多级缓存架构(二)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缓存。

相关推荐
企鹅侠客13 分钟前
实践篇:14-构建 Node.js 应用程序镜像
docker·node.js·dockerfile
zhuyasen17 分钟前
定义即代码!这个框架解决了90%的Go开发者还在低效开发项目的问题
架构·go·gin
LCG元20 分钟前
云原生微服务间的异步消息通信:最终一致性与系统容错的架构实战
微服务·云原生·架构
做一个AC梦1 小时前
Docker安装失败:Docker Desktop installation failed
运维·docker·容器
Shan12051 小时前
浅谈Docker Kicks in的应用
运维·docker·容器
Li&&Tao1 小时前
docker 常用命令
docker·容器·eureka
骑着王八撵玉兔1 小时前
【性能优化与架构调优(二)】高性能数据库设计与优化
数据库·性能优化·架构
Edingbrugh.南空1 小时前
Flink MySQL CDC 环境配置与验证
mysql·adb·flink
BD_Marathon2 小时前
Ubuntu:Mysql服务器
服务器·mysql·ubuntu
Jiude2 小时前
MinIO 社区版被故意阉割,Web管理功能全面移除。我来试试国产RustFS
后端·docker·架构