# hotkey **Repository Path**: liuweig/hotkey ## Basic Information - **Project Name**: hotkey - **Description**: 京东hotkey,nodejs版实现 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-27 - **Last Updated**: 2025-06-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 原文中“2、client端jar包” 描述了该 jar 包的功能 京东 client 端 jar 包,不仅包含了查询是不是热 key,还包含了设置缓存 比如在“4、client端” 提到 “如果是redis的热key,框架(jar包)告诉了我哪个是热key,并没有给我value啊。是的,框架(jar包)提供了是否是热key的方法,如果是 redis 热 key,就需要程序员自己去 redis 获取 value,然后调用框架(jar包)的set方法,将 value 也 set 进去就好。如果不是热key,那么就走原来的逻辑即可” 商品详情页伪代码 ```java public ProductDetail getProduct(String productId) { // 1. 只检查一次是否为热key boolean isHotKey = hotKeyClient.isHotKey(productId); // 2. 如果是热key,先查JVM本地缓存 if (isHotKey) { ProductDetail product = hotKeyClient.getValue(productId); if (product != null) { return product; // 直接返回,避免Redis压力 } } // 3. 查Redis ProductDetail product = redisTemplate.get(productId); if (product != null) { // 如果是热key,顺便缓存到JVM if (isHotKey) { hotKeyClient.setValue(productId, product); } return product; } // 4. 最后查MySQL,设置过期时间并缓存到Redis product = productMapper.selectById(productId); redisTemplate.set(productId, product, 3600); // 如果是热key,也缓存到JVM if (isHotKey) { hotKeyClient.setValue(productId, product); } return product; } ``` 更新商品详情伪代码 ```java @Service public class ProductService { // 更新商品时 public void updateProduct(String productId, ProductDetail newProduct) { // 1. 更新数据库 productMapper.updateById(productId, newProduct); // 2. 更新Redis redisTemplate.set("product_" + productId, newProduct); // 3. 清除本应用的JVM缓存 String hotKey = "productId_" + productId; JdHotKeyStore.delete(hotKey); // 4. 通知其他应用实例清除缓存 messageQueue.broadcast("CLEAR_CACHE", hotKey); // 4. 或者删除hotKey hotKeyClient.remove("productId_123"); } @EventListener("CLEAR_CACHE") public void handleClearCache(String hotKey) { JdHotKeyStore.delete(hotKey); // 清除本实例的缓存 } } ``` ┌─────────────────────────────────────────────────────────┐ │ Client Jar Package │ │ │ │ ┌──────────────┐ ┌─────────────────┐ ┌─────────────┐ │ │ │ 本地缓存 │ │ 网络通信 │ │ 配置监听 │ │ │ │ │ │ │ │ │ │ │ │ ┌──────────┐ │ │ ┌─────────────┐ │ │ ┌─────────┐ │ │ │ │ │Caffeine │ │ │ │Netty Client │ │ │ │ Etcd │ │ │ │ │ │Cache │ │ │ │Connection │ │ │ │Listener │ │ │ │ │ └──────────┘ │ │ └─────────────┘ │ │ └─────────┘ │ │ │ │ │ │ │ │ │ │ │ │ • get() │ │ • push() │ │ • 规则变化 │ │ │ │ • smartSet() │ │ • 长连接管理 │ │ • 热键通知 │ │ │ │ • delete() │ │ • 重连机制 │ │ • Worker变化 │ │ │ └──────────────┘ └─────────────────┘ └─────────────┘ │ │ │ │ ┌─────────────────────────────────────────────────────┐ │ │ │ 统一API接口层 │ │ │ │ JdHotKeyStore.isHotKey() / get() / smartSet() │ │ │ └─────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────┘