You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/15 03:07:03 UTC

[incubator-shenyu] branch master updated: [type:refactor] refactor redis blocking get method (#3554)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 865f6ef13 [type:refactor] refactor redis blocking get method (#3554)
865f6ef13 is described below

commit 865f6ef133da4b4fca13c29168afbdffb66f4811
Author: moremind <he...@hotmail.com>
AuthorDate: Wed Jun 15 11:06:58 2022 +0800

    [type:refactor] refactor redis blocking get method (#3554)
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
    
    * [type:refactor] refactor blocking get method
---
 .../shenyu/plugin/cache/redis/RedisCache.java      | 33 ++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisCache.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisCache.java
index 0ffff35d4..86fe60cce 100644
--- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisCache.java
+++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisCache.java
@@ -19,18 +19,29 @@ package org.apache.shenyu.plugin.cache.redis;
 
 import org.apache.shenyu.plugin.cache.ICache;
 import org.apache.shenyu.plugin.cache.redis.serializer.ShenyuRedisSerializationContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.data.redis.connection.ReactiveRedisConnection;
 import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
 import org.springframework.data.redis.core.ReactiveRedisTemplate;
+import reactor.core.publisher.Mono;
 
 import java.time.Duration;
 import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 /**
  * RedisCache.
  */
 public final class RedisCache implements ICache {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisCache.class);
+
+    private static final long REDIS_DEFAULT_TIMEOUT = 3L;
+
     private final ReactiveRedisTemplate<String, byte[]> redisTemplate;
 
     public RedisCache(final RedisConfigProperties redisConfigProperties) {
@@ -58,7 +69,17 @@ public final class RedisCache implements ICache {
      */
     @Override
     public boolean isExist(final String key) {
-        return Boolean.TRUE.equals(this.redisTemplate.hasKey(key).block());
+        CompletableFuture<Boolean> f = CompletableFuture.supplyAsync(() -> Mono.from(this.redisTemplate.hasKey(key)).block());
+        Boolean result = null;
+        try {
+            result = f.get(REDIS_DEFAULT_TIMEOUT, TimeUnit.SECONDS);
+        } catch (ExecutionException | TimeoutException | InterruptedException e) {
+            LOGGER.error("isExist error: {}", e.getMessage());
+        }
+        if (Objects.isNull(result)) {
+            return Boolean.FALSE;
+        }
+        return Boolean.TRUE.equals(result);
     }
 
     /**
@@ -68,7 +89,15 @@ public final class RedisCache implements ICache {
      */
     @Override
     public byte[] getData(final String key) {
-        return this.redisTemplate.opsForValue().get(key).block();
+        CompletableFuture<byte[]> f = CompletableFuture.supplyAsync(() -> Mono.from(this.redisTemplate.opsForValue().get(key)).block());
+        byte[] result = null;
+        try {
+            // can't get result in 3 seconds, this handle will fail
+            result = f.get(REDIS_DEFAULT_TIMEOUT, TimeUnit.SECONDS);
+        } catch (ExecutionException | TimeoutException | InterruptedException e) {
+            LOGGER.error("getData error: {}", e.getMessage());
+        }
+        return result;
     }
 
     /**