From dd7e98eb9c4930068341a68c04ea852b9d2f8a4e Mon Sep 17 00:00:00 2001 From: qiujiayu Date: Wed, 22 Apr 2015 14:29:51 +0800 Subject: [PATCH] =?UTF-8?q?Redis=E4=BD=BF=E7=94=A8lua=20=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=88=A0=E9=99=A4=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pom.xml | 4 +- .../com/jarvis/cache/redis/CachePointCut.java | 73 +++++++++++-------- .../cache/redis/ShardedCachePointCut.java | 23 +++--- 4 files changed, 59 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 2dc0fdb4..b1ef752a 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ AutoLoadHandler(自动加载处理器)主要做的事情:当缓存即将 com.github.qiujiayu autoload-cache - 1.5 + 1.6 diff --git a/pom.xml b/pom.xml index 8c0f3cd3..b4f63acc 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.qiujiayu autoload-cache - 1.5 + 1.6 jar AutoLoadCache User Spring AOP and annotation to do with cache. @@ -12,7 +12,7 @@ The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt - repo + The Apache Software License, Version 2.0 A business-friendly OSS license diff --git a/src/main/java/com/jarvis/cache/redis/CachePointCut.java b/src/main/java/com/jarvis/cache/redis/CachePointCut.java index d98048a2..e8c25b81 100644 --- a/src/main/java/com/jarvis/cache/redis/CachePointCut.java +++ b/src/main/java/com/jarvis/cache/redis/CachePointCut.java @@ -2,14 +2,13 @@ import java.io.Serializable; import java.util.List; -import java.util.Set; import org.apache.log4j.Logger; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import com.jarvis.cache.AbstractCacheManager; import com.jarvis.cache.AutoLoadHandler; @@ -20,13 +19,20 @@ /** * 缓存切面,用于拦截数据并调用Redis进行缓存 * @author jiayu.qiu + * @see com.jarvis.cache.redis.ShardedCachePointCut + * @deprecated 建议使用com.jarvis.cache.redis.ShardedCachePointCut */ +@Deprecated public class CachePointCut extends AbstractCacheManager { private static final Logger logger=Logger.getLogger(CachePointCut.class); private List> redisTemplateList; + private StringRedisSerializer keySerializer=new StringRedisSerializer(); + + private JdkSerializationRedisSerializer valSerializer=new JdkSerializationRedisSerializer(); + public CachePointCut(AutoLoadConfig config) { super(config); } @@ -43,7 +49,7 @@ public RedisTemplate getRedisTemplate(String key) { @Override public void setCache(final String cacheKey, final CacheWrapper result, final int expire) { if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) { - throw new java.lang.RuntimeException("cacheKey:" + cacheKey + "; has '*' or '?'"); + throw new RuntimeException("cacheKey:" + cacheKey + "; has '*' or '?'"); } try { result.setLastLoadTime(System.currentTimeMillis()); @@ -52,12 +58,15 @@ public void setCache(final String cacheKey, final CacheWrapper res @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { - byte[] key=redisTemplate.getStringSerializer().serialize(cacheKey); - JdkSerializationRedisSerializer serializer=(JdkSerializationRedisSerializer)redisTemplate.getValueSerializer(); - byte[] val=serializer.serialize(result); - // connection.set(key, val); - // connection.expire(key, expire); - connection.setEx(key, expire, val); + + try { + byte[] key=keySerializer.serialize(cacheKey); + byte[] val=valSerializer.serialize(result); + connection.setEx(key, expire, val); + } catch(Exception ex) { + logger.error(ex.getMessage(), ex); + } + return null; } }); @@ -75,15 +84,20 @@ public CacheWrapper get(final String cacheKey) { @Override public CacheWrapper doInRedis(RedisConnection connection) throws DataAccessException { - byte[] key=redisTemplate.getStringSerializer().serialize(cacheKey); + + byte[] key=keySerializer.serialize(cacheKey); byte[] value=connection.get(key); if(null != value && value.length > 0) { - JdkSerializationRedisSerializer serializer= - (JdkSerializationRedisSerializer)redisTemplate.getValueSerializer(); - @SuppressWarnings("unchecked") - CacheWrapper res=(CacheWrapper)serializer.deserialize(value); - return res; + + try { + @SuppressWarnings("unchecked") + CacheWrapper res=(CacheWrapper)valSerializer.deserialize(value); + return res; + } catch(Exception ex) { + logger.error(ex.getMessage(), ex); + } + } return null; } @@ -137,26 +151,24 @@ public void delete(final String cacheKey) { return; } final AutoLoadHandler autoLoadHandler=this.getAutoLoadHandler(); + String params[]=new String[]{cacheKey}; + final byte[][] p=new byte[params.length][]; + for(int i=0; i < params.length; i++) { + p[i]=keySerializer.serialize(params[i]); + } if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) { + final StringBuilder script=new StringBuilder(); + script.append("local keys = redis.call('keys', KEYS[1]);\n"); + script.append("if(not keys or #keys == 0) then \n return nil; \n end \n"); + script.append("redis.call('del', unpack(keys)); \n return keys;"); + for(final RedisTemplate redisTemplate: redisTemplateList) { redisTemplate.execute(new RedisCallback() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { - byte[] key=redisTemplate.getStringSerializer().serialize(cacheKey); - Set keys=connection.keys(key); - if(null != keys && keys.size() > 0) { - byte[][] keys2=new byte[keys.size()][]; - keys.toArray(keys2); - connection.del(keys2); - - for(byte[] tmp: keys2) { - JdkSerializationRedisSerializer serializer= - (JdkSerializationRedisSerializer)redisTemplate.getValueSerializer(); - String tmpKey=(String)serializer.deserialize(tmp); - autoLoadHandler.resetAutoLoadLastLoadTime(tmpKey); - } - } + byte[] scriptBytes=keySerializer.serialize(script.toString()); + connection.eval(scriptBytes, ReturnType.STATUS, 1, p); return null; } }); @@ -167,8 +179,7 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { - byte[] key=redisTemplate.getStringSerializer().serialize(cacheKey); - + byte[] key=keySerializer.serialize(cacheKey); connection.del(key); autoLoadHandler.resetAutoLoadLastLoadTime(cacheKey); return null; diff --git a/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java b/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java index 52b39a7e..8246f81c 100644 --- a/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java +++ b/src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java @@ -2,7 +2,7 @@ import java.io.Serializable; import java.util.Collection; -import java.util.Set; +import java.util.List; import org.apache.log4j.Logger; @@ -128,16 +128,21 @@ public void delete(final String cacheKey) { try { shardedJedis=shardedJedisPool.getResource(); Collection list=shardedJedis.getAllShards(); + StringBuilder script=new StringBuilder(); + script.append("local keys = redis.call('keys', KEYS[1]);\n"); + script.append("if(not keys or #keys == 0) then \n return nil; \n end \n"); + script.append("redis.call('del', unpack(keys)); \n return keys;"); for(Jedis jedis: list) { - Set keys=jedis.keys(keySerializer.serialize(cacheKey)); - if(null != keys && keys.size() > 0) { - byte[][] keys2=new byte[keys.size()][]; - keys.toArray(keys2); - jedis.del(keys2); - for(byte[] tmp: keys2) { - String tmpKey=(String)keySerializer.deserialize(tmp); - autoLoadHandler.resetAutoLoadLastLoadTime(tmpKey); + try { + @SuppressWarnings("unchecked") + List keys=(List)jedis.eval(script.toString(), 1, cacheKey); + if(null != keys && keys.size() > 0) { + for(String tmpKey: keys) { + autoLoadHandler.resetAutoLoadLastLoadTime(tmpKey); + } } + } catch(Exception ex) { + logger.error(ex.getMessage(), ex); } } } catch(Exception ex) {