From 37dee1ac2f37bde0e76710793727e5f3a913fd26 Mon Sep 17 00:00:00 2001 From: qiujiayu Date: Fri, 11 Sep 2015 11:44:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=EF=BC=88namespace=EF=BC=89,=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E5=90=84=E4=B8=AA=E7=B3=BB=E7=BB=9F=E9=97=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../jarvis/cache/memcache/CachePointCut.java | 36 ++++++++++++++----- .../cache/redis/ShardedCachePointCut.java | 26 ++++++++++++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 2f7efcf8..5bbfeedd 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.qiujiayu autoload-cache - 1.7 + 1.8 jar AutoLoadCache User Spring AOP and annotation to do with cache. diff --git a/src/main/java/com/jarvis/cache/memcache/CachePointCut.java b/src/main/java/com/jarvis/cache/memcache/CachePointCut.java index ea24c2b3..28d99ba3 100644 --- a/src/main/java/com/jarvis/cache/memcache/CachePointCut.java +++ b/src/main/java/com/jarvis/cache/memcache/CachePointCut.java @@ -17,20 +17,39 @@ public class CachePointCut extends AbstractCacheManager { private MemcachedClient memcachedClient; + private String namespace; + public CachePointCut(AutoLoadConfig config) { super(config); } + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace=namespace; + } + + private String appendNamespace(String cacheKey) { + if(null != namespace && namespace.length() > 0) { + return namespace + ":" + cacheKey; + } + return cacheKey; + } + @Override public void setCache(String cacheKey, CacheWrapper result, int expire) { result.setLastLoadTime(System.currentTimeMillis()); + cacheKey=appendNamespace(cacheKey); memcachedClient.set(cacheKey, expire, result); } @SuppressWarnings("unchecked") @Override - public CacheWrapper get(String key) { - return (CacheWrapper)memcachedClient.get(key); + public CacheWrapper get(String cacheKey) { + cacheKey=appendNamespace(cacheKey); + return (CacheWrapper)memcachedClient.get(cacheKey); } /** @@ -38,13 +57,14 @@ public CacheWrapper get(String key) { * @param key */ @Override - public void delete(String key) { - if(null == memcachedClient || null == key) { + public void delete(String cacheKey) { + if(null == memcachedClient || null == cacheKey) { return; } + cacheKey=appendNamespace(cacheKey); try { - memcachedClient.delete(key); - this.getAutoLoadHandler().resetAutoLoadLastLoadTime(key); + memcachedClient.delete(cacheKey); + this.getAutoLoadHandler().resetAutoLoadLastLoadTime(cacheKey); } catch(Exception e) { } } @@ -78,8 +98,8 @@ public void deleteDefinedCacheKey(String keySpEL, Object[] arguments) { public void delete(List keys) { try { if(null != keys && !keys.isEmpty()) { - for(String key: keys) { - this.delete(key); + for(String cacheKey: keys) { + this.delete(cacheKey); } } } catch(Exception e) { diff --git a/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java b/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java index 8246f81c..b16b8f4d 100644 --- a/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java +++ b/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java @@ -30,22 +30,40 @@ public class ShardedCachePointCut extends AbstractCacheManager { private ShardedJedisPool shardedJedisPool; + private String namespace; + public ShardedCachePointCut(AutoLoadConfig config) { super(config); } + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace=namespace; + } + private void returnResource(ShardedJedis shardedJedis) { shardedJedis.close(); } + private String appendNamespace(String cacheKey) { + if(null != namespace && namespace.length() > 0) { + return namespace + ":" + cacheKey; + } + return cacheKey; + } + @Override - public void setCache(final String cacheKey, final CacheWrapper result, final int expire) { + public void setCache(String cacheKey, final CacheWrapper result, final int expire) { if(null == shardedJedisPool || null == cacheKey) { return; } if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) { throw new java.lang.RuntimeException("cacheKey:" + cacheKey + "; has '*' or '?'"); } + cacheKey=appendNamespace(cacheKey); ShardedJedis shardedJedis=null; try { result.setLastLoadTime(System.currentTimeMillis()); @@ -61,13 +79,14 @@ public void setCache(final String cacheKey, final CacheWrapper res @SuppressWarnings("unchecked") @Override - public CacheWrapper get(final String cacheKey) { + public CacheWrapper get(String cacheKey) { if(null == shardedJedisPool || null == cacheKey) { return null; } CacheWrapper res=null; ShardedJedis shardedJedis=null; try { + cacheKey=appendNamespace(cacheKey); shardedJedis=shardedJedisPool.getResource(); Jedis jedis=shardedJedis.getShard(cacheKey); byte bytes[]=jedis.get(keySerializer.serialize(cacheKey)); @@ -118,10 +137,11 @@ public void deleteDefinedCacheKey(String keySpEL, Object[] arguments) { * @param cacheKey 如果传进来的值中 带有 * 或 ? 号,则会使用批量删除(遍历所有Redis服务器) */ @Override - public void delete(final String cacheKey) { + public void delete(String cacheKey) { if(null == shardedJedisPool || null == cacheKey) { return; } + cacheKey=appendNamespace(cacheKey); final AutoLoadHandler autoLoadHandler=this.getAutoLoadHandler(); ShardedJedis shardedJedis=null; if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) {// 如果是批量删除缓存,则要遍历所有redis,避免遗漏。