|
@@ -1,14 +1,19 @@
|
|
|
package com.atguigu.tingshu.album.api;
|
|
|
|
|
|
+import com.atguigu.tingshu.album.service.CacheService;
|
|
|
import com.atguigu.tingshu.album.service.TestService;
|
|
|
+import com.jd.platform.hotkey.client.callback.JdHotKeyStore;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.redisson.api.RBlockingQueue;
|
|
|
+import org.redisson.api.RDelayedQueue;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author: atguigu
|
|
@@ -60,4 +65,88 @@ public class TestController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+
|
|
|
+ @GetMapping("/sendDelayMessage/{msg}/{ttl}")
|
|
|
+ public String sendDelayMessage(@PathVariable String msg, @PathVariable Long ttl) {
|
|
|
+ //1.创建分布式阻塞队列
|
|
|
+ RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("delayQueue");
|
|
|
+ //2.创建延迟队列
|
|
|
+ RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
|
|
|
+ //3.发送延迟消息
|
|
|
+ delayedQueue.offer(msg, ttl, TimeUnit.SECONDS);
|
|
|
+ return "发送延时消息成功";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 监听阻塞队列消息
|
|
|
+ */
|
|
|
+ //@PostConstruct
|
|
|
+ //public void init() {
|
|
|
+ // //1.创建分布式阻塞队列
|
|
|
+ // RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("delayQueue");
|
|
|
+ // //开启单独线程 专门拉取延迟消息
|
|
|
+ // ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
+ // executor.submit(() -> {
|
|
|
+ // while (true) {
|
|
|
+ // try {
|
|
|
+ // String result = blockingQueue.poll(15, TimeUnit.SECONDS);
|
|
|
+ // if (StringUtil.isNotBlank(result)) {
|
|
|
+ // log.info("监听到延时队列消息:{}", result);
|
|
|
+ // }
|
|
|
+ // } catch (Exception e) {
|
|
|
+ // log.error("监听延时队列异常:{}", e.getMessage());
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CacheService cacheService;
|
|
|
+
|
|
|
+ @GetMapping("addKey")
|
|
|
+ public Object add(Integer count) {
|
|
|
+ for (int i = 0; i < 20; i++) {
|
|
|
+ cacheService.set("key" + i, "我是一个用来做测试的value:" + i);
|
|
|
+ }
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用热key查询,从redis查询key
|
|
|
+ */
|
|
|
+ @GetMapping("findHot/{key}")
|
|
|
+ public Object findWithHotKey(@PathVariable String key) {
|
|
|
+ return cacheService.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否为热key
|
|
|
+ * isHotKey(key)该方法会返回该key是否是热key,如果是返回true,如果不是返回false.
|
|
|
+ * 并且会将key上报到探测集群进行数量计算。该方法通常用于判断只需要判断key是否热、不需要缓存value的场景,如刷子用户、接口访问频率等。
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/hotKey/{key}")
|
|
|
+ public Object hotKey(@PathVariable String key) {
|
|
|
+ if (StringUtils.isNotBlank(key) && JdHotKeyStore.isHotKey(key)) {
|
|
|
+ return "isHot";
|
|
|
+ } else {
|
|
|
+ return "noHot";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @DeleteMapping("")
|
|
|
+ public Object aDelete(String key) {
|
|
|
+ JdHotKeyStore.remove(key);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
}
|