{"id":1225,"date":"2026-01-04T19:29:52","date_gmt":"2026-01-04T11:29:52","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1225"},"modified":"2026-01-04T19:29:52","modified_gmt":"2026-01-04T11:29:52","slug":"springboot%e6%95%b4%e5%90%88redis%e5%ae%9e%e7%8e%b0%e8%ae%a2%e5%8d%95%e8%b6%85%e6%97%b6%e8%87%aa%e5%8a%a8%e5%88%a0%e9%99%a4%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/04\/springboot%e6%95%b4%e5%90%88redis%e5%ae%9e%e7%8e%b0%e8%ae%a2%e5%8d%95%e8%b6%85%e6%97%b6%e8%87%aa%e5%8a%a8%e5%88%a0%e9%99%a4%e5%8a%9f%e8%83%bd\/","title":{"rendered":"SpringBoot\u6574\u5408Redis\u5b9e\u73b0\u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664\u529f\u80fd"},"content":{"rendered":"\n<p>\u660e\u767d\u4e86\uff0c\u4f60\u60f3\u7528 <strong>Spring Boot + Redis<\/strong> \u6765\u5b9e\u73b0 <strong>\u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664<\/strong>\u7684\u529f\u80fd\uff0c\u8fd9\u5728\u7535\u5546\u7cfb\u7edf\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4e0b\u9762\u6211\u7ed9\u4f60\u4e00\u4e2a <strong>\u5b8c\u6574\u53ef\u7528\u65b9\u6848<\/strong>\uff0c\u5305\u542b\u601d\u8def\u3001\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u70b9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Spring Boot + Redis \u5b9e\u73b0\u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6848\u601d\u8def<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8ba2\u5355\u8d85\u65f6\u5b9a\u4e49<\/strong>\uff1a\u7528\u6237\u4e0b\u5355\u540e\uff0c\u5982\u679c\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u672a\u652f\u4ed8\uff0c\u8ba2\u5355\u81ea\u52a8\u53d6\u6d88\/\u5220\u9664\u3002<\/li>\n\n\n\n<li><strong>Redis \u7684\u4f18\u52bf<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>Redis \u652f\u6301 <strong>\u952e\u8fc7\u671f<\/strong>\uff08TTL, Time-To-Live\uff09<\/li>\n\n\n\n<li>Redis \u652f\u6301 <strong>\u4e8b\u4ef6\u901a\u77e5<\/strong>\uff08Keyspace Notification\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>\u65b9\u6848 1\uff08\u7b80\u5355\uff09<\/strong>\uff1a\u7528 <code>RedisTemplate<\/code> \u8bbe\u7f6e\u8ba2\u5355 key \u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5229\u7528 <code>RedisListener<\/code> \u76d1\u542c\u8fc7\u671f\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u65b9\u6848 2\uff08\u5b9a\u65f6\u8f6e\u8be2\uff09<\/strong>\uff1a\u628a\u8ba2\u5355\u5b58\u5230 Redis \u6216\u6570\u636e\u5e93\u4e2d\uff0c\u5b9a\u65f6\u626b\u63cf\u5224\u65ad\u8d85\u65f6\uff08\u7b80\u5355\u4f46\u6548\u7387\u4f4e\uff09\u3002<\/li>\n\n\n\n<li><strong>\u65b9\u6848 3\uff08\u6d88\u606f\u961f\u5217 + \u5ef6\u8fdf\u961f\u5217\uff09<\/strong>\uff1a\u53ef\u4ee5\u7528 Redis \u7684 <code>ZSet<\/code> \u6216 <code>Stream<\/code> \u505a\u5ef6\u8fdf\u961f\u5217\uff0c\u4f46 Redis key TTL \u8db3\u591f\u7b80\u5355\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u63a8\u8350\u65b9\u6848\uff1a<strong>\u4f7f\u7528 Redis TTL + \u8fc7\u671f\u4e8b\u4ef6\u76d1\u542c<\/strong>\uff0c\u6700\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5177\u4f53\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u6dfb\u52a0\u4f9d\u8d56\uff08Maven\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;dependencies&gt;\n    &amp;lt;!-- Spring Boot Web --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- Spring Data Redis --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;spring-boot-starter-data-redis&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- Lettuce \u5ba2\u6237\u7aef --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;io.lettuce.core&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;lettuce-core&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- \u53ef\u9009\uff1aJSON \u5e8f\u5217\u5316 --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;com.fasterxml.jackson.core&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;jackson-databind&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n&amp;lt;\/dependencies&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 Redis \u914d\u7f6e<\/h3>\n\n\n\n<p>\u5728 <code>application.yml<\/code> \u6216 <code>application.properties<\/code> \u4e2d\u914d\u7f6e Redis\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nspring:\n  redis:\n    host: localhost\n    port: 6379\n    timeout: 5000\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f <strong>\u6ce8\u610f<\/strong>\uff1a\u8981\u5f00\u542f Redis key \u8fc7\u671f\u4e8b\u4ef6\u901a\u77e5\u3002\u4fee\u6539 <code>redis.conf<\/code> \u6587\u4ef6\uff0c\u6dfb\u52a0\u6216\u786e\u4fdd\u6709\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnotify-keyspace-events Ex\n\n<\/pre><\/div>\n\n\n<p><code>Ex<\/code> \u8868\u793a <strong>Key \u8fc7\u671f\u4e8b\u4ef6<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 \u8ba2\u5355\u670d\u52a1\uff1a\u4e0b\u5355 + \u8d85\u65f6\u81ea\u52a8\u5220\u9664<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u8ba2\u5355\u5bf9\u8c61<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@Data\n@AllArgsConstructor\n@NoArgsConstructor\npublic class Order {\n    private String orderId;\n    private String userId;\n    private double amount;\n    private long createTime;\n}\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">3.2 Redis \u5b58\u8ba2\u5355\u5e76\u8bbe\u7f6e TTL<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@Service\npublic class OrderService {\n\n    private final RedisTemplate&amp;lt;String, Object&gt; redisTemplate;\n\n    @Autowired\n    public OrderService(RedisTemplate&amp;lt;String, Object&gt; redisTemplate) {\n        this.redisTemplate = redisTemplate;\n    }\n\n    \/**\n     * \u4fdd\u5b58\u8ba2\u5355\uff0c\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u79d2\uff09\n     *\/\n    public void saveOrder(Order order, long expireSeconds) {\n        String key = &quot;order:&quot; + order.getOrderId();\n        redisTemplate.opsForValue().set(key, order, expireSeconds, TimeUnit.SECONDS);\n        System.out.println(&quot;\u8ba2\u5355\u5df2\u4fdd\u5b58\uff0ckey=&quot; + key + &quot;\uff0c\u8d85\u65f6\u79d2\u6570=&quot; + expireSeconds);\n    }\n\n    \/**\n     * \u83b7\u53d6\u8ba2\u5355\n     *\/\n    public Order getOrder(String orderId) {\n        return (Order) redisTemplate.opsForValue().get(&quot;order:&quot; + orderId);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u8fd9\u91cc\u4f7f\u7528 <code>opsForValue().set(key, value, expireTime, TimeUnit)<\/code> \u5c31\u53ef\u4ee5\u81ea\u52a8\u8fc7\u671f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4\ufe0f\u20e3 \u76d1\u542c\u8ba2\u5355\u8fc7\u671f\u4e8b\u4ef6<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@Component\npublic class RedisKeyExpiredListener {\n\n    @EventListener\n    public void onExpiredEvent(RedisKeyExpiredEvent event) {\n        String expiredKey = event.getKey();\n        if (expiredKey.startsWith(&quot;order:&quot;)) {\n            String orderId = expiredKey.split(&quot;:&quot;)&#x5B;1];\n            \/\/ \u5904\u7406\u8ba2\u5355\u8d85\u65f6\u903b\u8f91\n            System.out.println(&quot;\u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664: &quot; + orderId);\n            \/\/ TODO: \u540c\u6b65\u66f4\u65b0\u6570\u636e\u5e93\u72b6\u6001\u4e3a\u201c\u5df2\u53d6\u6d88\u201d\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f <strong>\u6ce8\u610f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76d1\u542c\u5668\u4f1a\u63a5\u6536\u5230 <strong>Redis \u8fc7\u671f\u4e8b\u4ef6<\/strong>\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u6267\u884c\u53d6\u6d88\u8ba2\u5355\u903b\u8f91\uff0c\u6bd4\u5982\u66f4\u65b0\u6570\u636e\u5e93\u72b6\u6001\u3002<\/li>\n\n\n\n<li>Redis TTL \u53ea\u4fdd\u8bc1 key \u6d88\u5931\uff0c\u4e0d\u4f1a\u81ea\u52a8\u5220\u9664\u6570\u636e\u5e93\u91cc\u7684\u8ba2\u5355\uff0c\u9700\u8981\u81ea\u5df1\u540c\u6b65\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5\ufe0f\u20e3 \u6d4b\u8bd5 Controller<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@RestController\n@RequestMapping(&quot;\/orders&quot;)\npublic class OrderController {\n\n    @Autowired\n    private OrderService orderService;\n\n    @PostMapping(&quot;\/create&quot;)\n    public String createOrder(@RequestParam String userId, @RequestParam double amount) {\n        String orderId = UUID.randomUUID().toString();\n        Order order = new Order(orderId, userId, amount, System.currentTimeMillis());\n        \/\/ \u8bbe\u7f6e\u8ba2\u5355\u8d85\u65f6\u65f6\u95f4\uff0c\u4f8b\u5982 60 \u79d2\n        orderService.saveOrder(order, 60);\n        return &quot;\u8ba2\u5355\u521b\u5efa\u6210\u529f, orderId=&quot; + orderId;\n    }\n\n    @GetMapping(&quot;\/{orderId}&quot;)\n    public Order getOrder(@PathVariable String orderId) {\n        return orderService.getOrder(orderId);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8c03\u7528 <code>\/orders\/create?userId=123&amp;amount=100<\/code><\/li>\n\n\n\n<li>60 \u79d2\u540e\uff0cRedis key \u81ea\u52a8\u8fc7\u671f<\/li>\n\n\n\n<li><code>RedisKeyExpiredListener<\/code> \u6536\u5230\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u6267\u884c\u6570\u636e\u5e93\u53d6\u6d88\u903b\u8f91<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6\ufe0f\u20e3 \u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u540c\u6b65<\/strong>\uff1aRedis key \u8fc7\u671f\u53ea\u80fd\u5220\u9664\u7f13\u5b58\uff0c\u771f\u6b63\u8ba2\u5355\u72b6\u6001\u5fc5\u987b\u66f4\u65b0\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>Redis \u914d\u7f6e<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u786e\u4fdd <code>notify-keyspace-events<\/code> \u8bbe\u7f6e\u4e3a <code>Ex<\/code><\/li>\n\n\n\n<li>\u4e0d\u7136\u8fc7\u671f\u4e8b\u4ef6\u65e0\u6cd5\u89e6\u53d1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5206\u5e03\u5f0f\u73af\u5883<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u6709\u591a\u53f0 Redis \u6216\u5fae\u670d\u52a1\uff0c\u786e\u4fdd\u4e8b\u4ef6\u76d1\u542c\u548c\u6570\u636e\u5e93\u66f4\u65b0\u4e0d\u4f1a\u91cd\u590d\u6267\u884c\u3002\u53ef\u4ee5\u7528 <strong>\u6d88\u606f\u961f\u5217<\/strong> \u6216 <strong>Redisson \u5206\u5e03\u5f0f\u9501<\/strong> \u4fdd\u8bc1\u5e42\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8ba2\u5355\u65f6\u95f4\u53ef\u7075\u6d3b\u914d\u7f6e<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u79d2\u7ea7\u3001\u5206\u949f\u7ea7\u90fd\u53ef\u4ee5<\/li>\n\n\n\n<li>\u5b9e\u9645\u7535\u5546\u7cfb\u7edf\u5e38\u7528 15 \u5206\u949f\u652f\u4ed8\u8d85\u65f6<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5229\u7528 <strong>Redis TTL<\/strong> \u81ea\u52a8\u8fc7\u671f key<\/li>\n\n\n\n<li>\u5229\u7528 <strong>Keyspace Notification<\/strong> \u76d1\u542c key \u8fc7\u671f<\/li>\n\n\n\n<li>\u5728\u8fc7\u671f\u4e8b\u4ef6\u4e2d\u6267\u884c <strong>\u53d6\u6d88\u8ba2\u5355 \/ \u540c\u6b65\u6570\u636e\u5e93<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6848 <strong>\u6027\u80fd\u9ad8\u3001\u5b9e\u73b0\u7b80\u5355\u3001\u65e0\u9700\u989d\u5916\u5b9a\u65f6\u4efb\u52a1<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u660e\u767d\u4e86\uff0c\u6211\u7ed9\u4f60\u5199\u4e00\u4e2a <strong>\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 Spring Boot + Redis + MySQL demo<\/strong>\uff0c\u5b9e\u73b0 <strong>\u8ba2\u5355\u4e0b\u5355 + Redis \u8d85\u65f6\u81ea\u52a8\u5220\u9664 + \u540c\u6b65\u6570\u636e\u5e93\u53d6\u6d88\u8ba2\u5355<\/strong>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Spring Boot + Redis + MySQL \u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664\u5b8c\u6574 Demo<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 Maven \u4f9d\u8d56\uff08pom.xml\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;dependencies&gt;\n    &amp;lt;!-- Spring Boot Web --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- Spring Data Redis --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;spring-boot-starter-data-redis&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- Lettuce \u5ba2\u6237\u7aef --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;io.lettuce.core&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;lettuce-core&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- MySQL \u6570\u636e\u5e93 --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;spring-boot-starter-data-jpa&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;mysql&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;mysql-connector-java&amp;lt;\/artifactId&gt;\n    &amp;lt;\/dependency&gt;\n\n    &amp;lt;!-- Lombok --&gt;\n    &amp;lt;dependency&gt;\n        &amp;lt;groupId&gt;org.projectlombok&amp;lt;\/groupId&gt;\n        &amp;lt;artifactId&gt;lombok&amp;lt;\/artifactId&gt;\n        &amp;lt;optional&gt;true&amp;lt;\/optional&gt;\n    &amp;lt;\/dependency&gt;\n&amp;lt;\/dependencies&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 application.yml \u914d\u7f6e<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nspring:\n  datasource:\n    url: jdbc:mysql:\/\/localhost:3306\/testdb?useSSL=false&amp;amp;serverTimezone=UTC\n    username: root\n    password: 123456\n    driver-class-name: com.mysql.cj.jdbc.Driver\n\n  jpa:\n    hibernate:\n      ddl-auto: update\n    show-sql: true\n\n  redis:\n    host: localhost\n    port: 6379\n    timeout: 5000\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f Redis \u914d\u7f6e\u6587\u4ef6 <code>redis.conf<\/code> \u4e2d\u9700\u5f00\u542f\u8fc7\u671f\u4e8b\u4ef6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnotify-keyspace-events Ex\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u8ba2\u5355\u5b9e\u4f53\u7c7b<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport jakarta.persistence.*;\nimport lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\n@Data\n@AllArgsConstructor\n@NoArgsConstructor\n@Entity\n@Table(name = &quot;orders&quot;)\npublic class Order {\n\n    @Id\n    private String orderId;\n\n    private String userId;\n\n    private double amount;\n\n    private String status; \/\/ NEW, PAID, CANCELLED\n\n    private long createTime;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 \u8ba2\u5355 JPA \u4ed3\u5e93<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport org.springframework.data.jpa.repository.JpaRepository;\n\npublic interface OrderRepository extends JpaRepository&amp;lt;Order, String&gt; {\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 Redis \u914d\u7f6e\uff08\u5f00\u542f Keyspace Notification\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.redis.connection.RedisConnectionFactory;\nimport org.springframework.data.redis.listener.PatternTopic;\nimport org.springframework.data.redis.listener.RedisMessageListenerContainer;\nimport org.springframework.data.redis.listener.adapter.MessageListenerAdapter;\n\n@Configuration\npublic class RedisConfig {\n\n    @Bean\n    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,\n                                            MessageListenerAdapter listenerAdapter) {\n        RedisMessageListenerContainer container = new RedisMessageListenerContainer();\n        container.setConnectionFactory(connectionFactory);\n        \/\/ \u76d1\u542c\u8fc7\u671f\u4e8b\u4ef6\n        container.addMessageListener(listenerAdapter, new PatternTopic(&quot;__keyevent@0__:expired&quot;));\n        return container;\n    }\n\n    @Bean\n    MessageListenerAdapter listenerAdapter(OrderExpiredListener listener) {\n        return new MessageListenerAdapter(listener, &quot;onMessage&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6\ufe0f\u20e3 Redis \u8ba2\u5355\u670d\u52a1<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.stereotype.Service;\n\nimport java.util.concurrent.TimeUnit;\n\n@Service\npublic class OrderService {\n\n    @Autowired\n    private RedisTemplate&amp;lt;String, Object&gt; redisTemplate;\n\n    @Autowired\n    private OrderRepository orderRepository;\n\n    \/**\n     * \u4fdd\u5b58\u8ba2\u5355\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\n     *\/\n    public void saveOrder(Order order, long expireSeconds) {\n        order.setStatus(&quot;NEW&quot;);\n        order.setCreateTime(System.currentTimeMillis());\n        \/\/ \u4fdd\u5b58\u6570\u636e\u5e93\n        orderRepository.save(order);\n        \/\/ \u4fdd\u5b58 Redis \u5e76\u8bbe\u7f6e TTL\n        String key = &quot;order:&quot; + order.getOrderId();\n        redisTemplate.opsForValue().set(key, order, expireSeconds, TimeUnit.SECONDS);\n        System.out.println(&quot;\u8ba2\u5355\u4fdd\u5b58\uff0cRedis key=&quot; + key + &quot;\uff0c\u8d85\u65f6\u79d2\u6570=&quot; + expireSeconds);\n    }\n\n    public Order getOrder(String orderId) {\n        return orderRepository.findById(orderId).orElse(null);\n    }\n\n    \/**\n     * \u53d6\u6d88\u8ba2\u5355\n     *\/\n    public void cancelOrder(String orderId) {\n        orderRepository.findById(orderId).ifPresent(order -&gt; {\n            order.setStatus(&quot;CANCELLED&quot;);\n            orderRepository.save(order);\n            System.out.println(&quot;\u8ba2\u5355\u5df2\u53d6\u6d88\uff1a&quot; + orderId);\n        });\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7\ufe0f\u20e3 Redis \u8fc7\u671f\u76d1\u542c\u5668<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.connection.Message;\nimport org.springframework.data.redis.connection.MessageListener;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class OrderExpiredListener implements MessageListener {\n\n    @Autowired\n    private OrderService orderService;\n\n    @Override\n    public void onMessage(Message message, byte&#x5B;] pattern) {\n        String expiredKey = message.toString();\n        if (expiredKey.startsWith(&quot;order:&quot;)) {\n            String orderId = expiredKey.split(&quot;:&quot;)&#x5B;1];\n            \/\/ \u81ea\u52a8\u53d6\u6d88\u8ba2\u5355\n            orderService.cancelOrder(orderId);\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8\ufe0f\u20e3 Controller \u6d4b\u8bd5\u4e0b\u5355<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.*;\n\nimport java.util.UUID;\n\n@RestController\n@RequestMapping(&quot;\/orders&quot;)\npublic class OrderController {\n\n    @Autowired\n    private OrderService orderService;\n\n    @PostMapping(&quot;\/create&quot;)\n    public String createOrder(@RequestParam String userId, @RequestParam double amount) {\n        String orderId = UUID.randomUUID().toString();\n        Order order = new Order(orderId, userId, amount, &quot;NEW&quot;, System.currentTimeMillis());\n        \/\/ \u8bbe\u7f6e\u8ba2\u5355\u8d85\u65f660\u79d2\n        orderService.saveOrder(order, 60);\n        return &quot;\u8ba2\u5355\u521b\u5efa\u6210\u529f, orderId=&quot; + orderId;\n    }\n\n    @GetMapping(&quot;\/{orderId}&quot;)\n    public Order getOrder(@PathVariable String orderId) {\n        return orderService.getOrder(orderId);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9\ufe0f\u20e3 \u6d4b\u8bd5\u6d41\u7a0b<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u542f\u52a8 Spring Boot \u9879\u76ee<\/li>\n\n\n\n<li>\u8c03\u7528\u63a5\u53e3\u521b\u5efa\u8ba2\u5355\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nPOST http:\/\/localhost:8080\/orders\/create?userId=123&amp;amp;amount=100\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Redis \u81ea\u52a8\u751f\u6210 key\uff0c\u4f8b\u5982 <code>order:UUID<\/code><\/li>\n\n\n\n<li>\u8bbe\u7f6e TTL\uff08\u4f8b\u5982 60 \u79d2\uff09\uff0c\u8fc7\u671f\u540e\u89e6\u53d1\u76d1\u542c\u5668<\/li>\n\n\n\n<li>Listener \u8c03\u7528 <code>cancelOrder()<\/code> \u540c\u6b65\u6570\u636e\u5e93\uff0c\u5c06\u8ba2\u5355\u72b6\u6001\u6539\u4e3a <code>CANCELLED<\/code><\/li>\n\n\n\n<li>\u67e5\u8be2\u63a5\u53e3\u8fd4\u56de\u8ba2\u5355\u72b6\u6001\u5373\u53ef\u9a8c\u8bc1\u81ea\u52a8\u53d6\u6d88\u529f\u80fd<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10\ufe0f\u20e3 \u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Redis \u914d\u7f6e<\/strong>\uff1a<code>notify-keyspace-events Ex<\/code> \u5fc5\u987b\u5f00\u542f<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u540c\u6b65<\/strong>\uff1a\u8fc7\u671f\u4e8b\u4ef6\u53ea\u80fd\u5220\u9664 Redis \u7f13\u5b58\uff0c\u6570\u636e\u5e93\u5fc5\u987b\u66f4\u65b0<\/li>\n\n\n\n<li><strong>\u5206\u5e03\u5f0f\u73af\u5883<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u591a\u4e2a\u5fae\u670d\u52a1\u76d1\u542c\u65f6\u53ef\u80fd\u4f1a\u91cd\u590d\u6267\u884c\uff0c\u53ef\u52a0 <strong>Redisson \u5206\u5e03\u5f0f\u9501<\/strong> \u4fdd\u8bc1\u5e42\u7b49<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8ba2\u5355\u65f6\u95f4<\/strong>\uff1a\u53ef\u6309\u4e1a\u52a1\u9700\u6c42\u8c03\u6574 TTL \u79d2\u6570<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u660e\u767d\u4e86\uff0c\u4f60\u60f3\u7528 Spring Boot + Redis \u6765\u5b9e\u73b0 \u8ba2\u5355\u8d85\u65f6\u81ea\u52a8\u5220\u9664&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/04\/springboot%e6%95%b4%e5%90%88redis%e5%ae%9e%e7%8e%b0%e8%ae%a2%e5%8d%95%e8%b6%85%e6%97%b6%e8%87%aa%e5%8a%a8%e5%88%a0%e9%99%a4%e5%8a%9f%e8%83%bd\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1225","post","type-post","status-publish","format-standard","hentry","category-java"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1225"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1225\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1225\/revisions\/1226"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}