You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by ha...@apache.org on 2022/04/01 06:54:21 UTC
[incubator-shenyu] branch master updated: [type: refactor] optimize cache-plugin config logic. (#3164)
This is an automated email from the ASF dual-hosted git repository.
haiboduan 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 e083bc9 [type: refactor] optimize cache-plugin config logic. (#3164)
e083bc9 is described below
commit e083bc9c1fe33726ec64c7e63bc5e7dc5d33f47e
Author: Qicz <qi...@gmail.com>
AuthorDate: Fri Apr 1 14:54:14 2022 +0800
[type: refactor] optimize cache-plugin config logic. (#3164)
---
.../shenyu/plugin/cache/config/CacheConfig.java | 28 ++++++++++++++++++----
.../cache/handler/CachePluginDataHandler.java | 22 ++++++++++++-----
.../shenyu/plugin/cache/utils/CacheUtils.java | 6 +----
.../shenyu/plugin/cache/redis/RedisCache.java | 2 +-
.../org/apache/shenyu/plugin/cache/ICache.java | 2 +-
5 files changed, 42 insertions(+), 18 deletions(-)
diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/config/CacheConfig.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/config/CacheConfig.java
index 8080a2f..2f632e9 100644
--- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/config/CacheConfig.java
+++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/config/CacheConfig.java
@@ -30,6 +30,11 @@ public class CacheConfig {
private String cacheType = "memory";
/**
+ * the origin config.
+ */
+ private String config;
+
+ /**
* Get cache type.
* @return the cache type
*/
@@ -45,6 +50,22 @@ public class CacheConfig {
this.cacheType = cacheType;
}
+ /**
+ * Get config.
+ * @return the config
+ */
+ public String getConfig() {
+ return config;
+ }
+
+ /**
+ * Set the config.
+ * @param config the config
+ */
+ public void setConfig(final String config) {
+ this.config = config;
+ }
+
@Override
public boolean equals(final Object o) {
if (this == o) {
@@ -53,15 +74,12 @@ public class CacheConfig {
if (o == null || getClass() != o.getClass()) {
return false;
}
- if (!super.equals(o)) {
- return false;
- }
final CacheConfig that = (CacheConfig) o;
- return Objects.equals(cacheType, that.cacheType);
+ return cacheType.equals(that.cacheType) && config.equals(that.config);
}
@Override
public int hashCode() {
- return Objects.hash(super.hashCode(), cacheType);
+ return Objects.hash(cacheType, config);
}
}
diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/handler/CachePluginDataHandler.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/handler/CachePluginDataHandler.java
index 89b48f1..757a8d6 100644
--- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/handler/CachePluginDataHandler.java
+++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/handler/CachePluginDataHandler.java
@@ -55,6 +55,7 @@ public class CachePluginDataHandler implements PluginDataHandler {
public void handlerPlugin(final PluginData pluginData) {
if (Objects.isNull(pluginData) || Boolean.FALSE.equals(pluginData.getEnabled())) {
LOG.info("the plugin {} is disabled", this.pluginNamed());
+ this.closeCacheIfNeed();
return;
}
final String config = pluginData.getConfig();
@@ -64,18 +65,15 @@ public class CachePluginDataHandler implements PluginDataHandler {
return;
}
LOG.info("use the {} cache.", cacheConfig.getCacheType());
+ // set the config to compare with lastConfig.
+ cacheConfig.setConfig(config);
final CacheConfig lastCacheConfig = Singleton.INST.get(CacheConfig.class);
if (cacheConfig.equals(lastCacheConfig)) {
LOG.info("cache plugin initialized.");
return;
}
Singleton.INST.single(CacheConfig.class, cacheConfig);
- ICache lastCache = CacheUtils.getCache();
- if (Objects.nonNull(lastCache)) {
- // close last cache.
- LOG.info("close the last cache {}", lastCache);
- lastCache.close();
- }
+ this.closeCacheIfNeed();
final ICacheBuilder cacheBuilder = ExtensionLoader.getExtensionLoader(ICacheBuilder.class).getJoin(cacheConfig.getCacheType());
Singleton.INST.single(ICache.class, cacheBuilder.builderCache(config));
}
@@ -97,4 +95,16 @@ public class CachePluginDataHandler implements PluginDataHandler {
public String pluginNamed() {
return PluginEnum.CACHE.getName();
}
+
+ /**
+ * close the cache if you need.
+ */
+ private void closeCacheIfNeed() {
+ ICache lastCache = CacheUtils.getCache();
+ if (Objects.nonNull(lastCache)) {
+ // close last cache.
+ LOG.info("close the last cache {}", lastCache);
+ lastCache.close();
+ }
+ }
}
diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/utils/CacheUtils.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/utils/CacheUtils.java
index c01a34d..96aab48 100644
--- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/utils/CacheUtils.java
+++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler/src/main/java/org/apache/shenyu/plugin/cache/utils/CacheUtils.java
@@ -20,12 +20,9 @@ package org.apache.shenyu.plugin.cache.utils;
import org.apache.shenyu.common.utils.Md5Utils;
import org.apache.shenyu.common.utils.Singleton;
import org.apache.shenyu.plugin.cache.ICache;
-import org.apache.shenyu.plugin.cache.config.CacheConfig;
-import org.apache.shenyu.plugin.cache.memory.MemoryCache;
import org.springframework.web.server.ServerWebExchange;
import java.net.URI;
-import java.util.Optional;
/**
* CacheUtils.
@@ -67,7 +64,6 @@ public final class CacheUtils {
* @return cache
*/
public static ICache getCache() {
- final CacheConfig cacheConfig = Singleton.INST.get(CacheConfig.class);
- return Optional.ofNullable(cacheConfig).map(config -> Singleton.INST.get(ICache.class)).orElse(new MemoryCache());
+ return Singleton.INST.get(ICache.class);
}
}
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 dadf85d..0ffff35 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
@@ -52,7 +52,7 @@ public final class RedisCache implements ICache {
}
/**
- * Check the cache is exist or not.
+ * Check the cache is existed or not.
* @param key the cache key
* @return true exist
*/
diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-spi/src/main/java/org/apache/shenyu/plugin/cache/ICache.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-spi/src/main/java/org/apache/shenyu/plugin/cache/ICache.java
index c4e5c79..a5e9baf 100644
--- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-spi/src/main/java/org/apache/shenyu/plugin/cache/ICache.java
+++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-spi/src/main/java/org/apache/shenyu/plugin/cache/ICache.java
@@ -37,7 +37,7 @@ public interface ICache {
boolean cacheData(String key, byte[] bytes, long timeoutSeconds);
/**
- * Check the cache is exist or not.
+ * Check the cache is existed or not.
* @param key the cache key
* @return true exist
*/