From 4dd235124dae878fbeb9338fc88724b5fc536797 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 03 九月 2021 16:22:04 +0800
Subject: [PATCH] add 增加 发布订阅 演示案例

---
 ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisPubSubController.java |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisPubSubController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisPubSubController.java
new file mode 100644
index 0000000..a9144b4
--- /dev/null
+++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisPubSubController.java
@@ -0,0 +1,44 @@
+package com.ruoyi.demo.controller;
+
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.redis.RedisCache;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Redis 鍙戝竷璁㈤槄 婕旂ず妗堜緥
+ *
+ * @author Lion Li
+ */
+@Api(value = "Redis鍙戝竷璁㈤槄 婕旂ず妗堜緥", tags = {"Redis鍙戝竷璁㈤槄"})
+@RequiredArgsConstructor(onConstructor_ = @Autowired)
+@RestController
+@RequestMapping("/demo/redis/pubsub")
+public class RedisPubSubController {
+
+	private final RedisCache redisCache;
+
+	@ApiOperation("鍙戝竷娑堟伅")
+	@GetMapping("/pub")
+	public AjaxResult<Void> pub(String key, String value){
+		redisCache.publish(key, value, consumer -> {
+			System.out.println("鍙戝竷閫氶亾 => " + key + ", 鍙戦�佸�� => " + value);
+		});
+		return AjaxResult.success("鎿嶄綔鎴愬姛");
+	}
+
+	@ApiOperation("璁㈤槄娑堟伅")
+	@GetMapping("/sub")
+	public AjaxResult<Void> sub(String key){
+		redisCache.subscribe(key, String.class, msg -> {
+			System.out.println("璁㈤槄閫氶亾 => " + key + ", 鎺ユ敹鍊� => " + msg.toString());
+		});
+		return AjaxResult.success("鎿嶄綔鎴愬姛");
+	}
+
+}

--
Gitblit v1.9.3