diff --git a/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheConfig.java b/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheConfig.java index b5d2cbc25de894ad974caef7a4c050802f1b3cd2..259134740f6f29a01a1db7fad13f1eabae084a3e 100644 --- a/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheConfig.java +++ b/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheConfig.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -25,5 +25,16 @@ import io.jboot.support.redis.JbootRedisConfig; @ConfigModel(prefix = "jboot.cache.redis") public class JbootRedisCacheConfig extends JbootRedisConfig { + /** + * 全局的key前缀,所有缓存的key都会自动添加该前缀 + */ + private String globalKeyPrefix; + public String getGlobalKeyPrefix() { + return globalKeyPrefix; + } + + public void setGlobalKeyPrefix(String globalKeyPrefix) { + this.globalKeyPrefix = globalKeyPrefix; + } } diff --git a/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheImpl.java b/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheImpl.java index f29855234cfc437b031932bc915c2408c7112be0..6d4cbbb6d6991f22a2ecffefcabf197291074f08 100644 --- a/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheImpl.java +++ b/src/main/java/io/jboot/components/cache/redis/JbootRedisCacheImpl.java @@ -22,6 +22,7 @@ import io.jboot.support.redis.JbootRedisManager; import io.jboot.components.cache.JbootCacheBase; import io.jboot.exception.JbootIllegalConfigException; import io.jboot.support.redis.RedisScanResult; +import io.jboot.utils.StrUtil; import java.util.*; @@ -30,13 +31,20 @@ public class JbootRedisCacheImpl extends JbootCacheBase { private JbootRedis redis; - private static final String redisCacheNamesKey = "jboot_cache_names"; + private JbootRedisCacheConfig cacheConfig; + private static String redisCacheNamesKey = "jboot_cache_names"; + private String globalKeyPrefix = ""; public JbootRedisCacheImpl() { - JbootRedisCacheConfig redisConfig = Jboot.config(JbootRedisCacheConfig.class); - if (redisConfig.isConfigOk()) { - redis = JbootRedisManager.me().getRedis(redisConfig); + cacheConfig = Jboot.config(JbootRedisCacheConfig.class); + if (StrUtil.isNotBlank(cacheConfig.getGlobalKeyPrefix())) { + globalKeyPrefix = cacheConfig.getGlobalKeyPrefix() + ":"; + redisCacheNamesKey = globalKeyPrefix + redisCacheNamesKey; + } + + if (cacheConfig.isConfigOk()) { + redis = JbootRedisManager.me().getRedis(cacheConfig); } else { redis = Jboot.getRedis(); } @@ -119,7 +127,9 @@ public class JbootRedisCacheImpl extends JbootCacheBase { private String buildKey(String cacheName, Object key) { - StringBuilder keyBuilder = new StringBuilder(cacheName).append(":"); + StringBuilder keyBuilder = new StringBuilder(globalKeyPrefix) + .append(cacheName).append(":"); + if (key instanceof String) { keyBuilder.append("S"); } else if (key instanceof Number) { diff --git a/src/test/java/io/jboot/test/cache/redis/RedisCacheTester.java b/src/test/java/io/jboot/test/cache/redis/RedisCacheTester.java index aeb5068ebfd27eed383a287e98de79e963e00081..0b8b9dc3d037e8f0277487ea882889463b64daf2 100644 --- a/src/test/java/io/jboot/test/cache/redis/RedisCacheTester.java +++ b/src/test/java/io/jboot/test/cache/redis/RedisCacheTester.java @@ -15,6 +15,7 @@ public class RedisCacheTester { JbootApplication.setBootArg("jboot.cache.type", "redis"); JbootApplication.setBootArg("jboot.cache.redis.host", "127.0.0.1"); JbootApplication.setBootArg("jboot.cache.redis.port", "6379"); + JbootApplication.setBootArg("jboot.cache.redis.globalKeyPrefix", "myapp"); } @Test