|
| 1 | +package com.jarvis.cache.redis; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.lang.reflect.Type; |
| 5 | + |
| 6 | +import org.apache.log4j.Logger; |
| 7 | + |
| 8 | +import com.jarvis.cache.AbstractCacheManager; |
| 9 | +import com.jarvis.cache.exception.CacheCenterConnectionException; |
| 10 | +import com.jarvis.cache.script.AbstractScriptParser; |
| 11 | +import com.jarvis.cache.serializer.ISerializer; |
| 12 | +import com.jarvis.cache.serializer.StringSerializer; |
| 13 | +import com.jarvis.cache.to.AutoLoadConfig; |
| 14 | +import com.jarvis.cache.to.CacheKeyTO; |
| 15 | +import com.jarvis.cache.to.CacheWrapper; |
| 16 | + |
| 17 | +import redis.clients.jedis.JedisCluster; |
| 18 | +import redis.clients.jedis.ShardedJedis; |
| 19 | + |
| 20 | +/** |
| 21 | + * Redis缓存管理 |
| 22 | + * @author jiayu.qiu |
| 23 | + */ |
| 24 | +public class JedisClusterCacheManager extends AbstractCacheManager { |
| 25 | + |
| 26 | + private static final Logger logger=Logger.getLogger(JedisClusterCacheManager.class); |
| 27 | + |
| 28 | + private static final StringSerializer keySerializer=new StringSerializer(); |
| 29 | + |
| 30 | + private JedisCluster jedisCluster; |
| 31 | + |
| 32 | + /** |
| 33 | + * Hash的缓存时长:等于0时永久缓存;大于0时,主要是为了防止一些已经不用的缓存占用内存;hashExpire小于0时,则使用@Cache中设置的expire值(默认值为-1)。 |
| 34 | + */ |
| 35 | + private int hashExpire=-1; |
| 36 | + |
| 37 | + /** |
| 38 | + * 是否通过脚本来设置 Hash的缓存时长 |
| 39 | + */ |
| 40 | + private boolean hashExpireByScript=false; |
| 41 | + |
| 42 | + public JedisClusterCacheManager(AutoLoadConfig config, ISerializer<Object> serializer, AbstractScriptParser scriptParser) { |
| 43 | + super(config, serializer, scriptParser); |
| 44 | + } |
| 45 | + |
| 46 | + private void returnResource(ShardedJedis shardedJedis) { |
| 47 | + shardedJedis.close(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> result, final Method method, final Object args[]) throws CacheCenterConnectionException { |
| 52 | + if(null == jedisCluster || null == cacheKeyTO) { |
| 53 | + return; |
| 54 | + } |
| 55 | + String cacheKey=cacheKeyTO.getCacheKey(); |
| 56 | + if(null == cacheKey || cacheKey.length() == 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + ShardedJedis shardedJedis=null; |
| 60 | + try { |
| 61 | + int expire=result.getExpire(); |
| 62 | + String hfield=cacheKeyTO.getHfield(); |
| 63 | + if(null == hfield || hfield.length() == 0) { |
| 64 | + if(expire == 0) { |
| 65 | + |
| 66 | + jedisCluster.set(keySerializer.serialize(cacheKey), getSerializer().serialize(result)); |
| 67 | + } else { |
| 68 | + jedisCluster.setex(keySerializer.serialize(cacheKey), expire, getSerializer().serialize(result)); |
| 69 | + } |
| 70 | + } else { |
| 71 | + hashSet(cacheKey, hfield, result); |
| 72 | + } |
| 73 | + } catch(Exception ex) { |
| 74 | + logger.error(ex.getMessage(), ex); |
| 75 | + } finally { |
| 76 | + returnResource(shardedJedis); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void hashSet(String cacheKey, String hfield, CacheWrapper<Object> result) throws Exception { |
| 81 | + byte[] key=keySerializer.serialize(cacheKey); |
| 82 | + byte[] field=keySerializer.serialize(hfield); |
| 83 | + byte[] val=getSerializer().serialize(result); |
| 84 | + int hExpire; |
| 85 | + if(hashExpire < 0) { |
| 86 | + hExpire=result.getExpire(); |
| 87 | + } else { |
| 88 | + hExpire=hashExpire; |
| 89 | + } |
| 90 | + if(hExpire == 0) { |
| 91 | + jedisCluster.hset(key, field, val); |
| 92 | + } else { |
| 93 | + jedisCluster.hset(key, field, val); |
| 94 | + jedisCluster.expire(key, hExpire); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + @Override |
| 100 | + public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method, final Object args[]) throws CacheCenterConnectionException { |
| 101 | + if(null == jedisCluster || null == cacheKeyTO) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + String cacheKey=cacheKeyTO.getCacheKey(); |
| 105 | + if(null == cacheKey || cacheKey.length() == 0) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + CacheWrapper<Object> res=null; |
| 109 | + ShardedJedis shardedJedis=null; |
| 110 | + try { |
| 111 | + byte bytes[]=null; |
| 112 | + String hfield=cacheKeyTO.getHfield(); |
| 113 | + if(null == hfield || hfield.length() == 0) { |
| 114 | + bytes=jedisCluster.get(keySerializer.serialize(cacheKey)); |
| 115 | + } else { |
| 116 | + bytes=jedisCluster.hget(keySerializer.serialize(cacheKey), keySerializer.serialize(hfield)); |
| 117 | + } |
| 118 | + Type returnType=method.getGenericReturnType(); |
| 119 | + res=(CacheWrapper<Object>)getSerializer().deserialize(bytes, returnType); |
| 120 | + } catch(Exception ex) { |
| 121 | + logger.error(ex.getMessage(), ex); |
| 122 | + } finally { |
| 123 | + returnResource(shardedJedis); |
| 124 | + } |
| 125 | + return res; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 根据缓存Key删除缓存 |
| 130 | + * @param cacheKeyTO 缓存Key |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public void delete(CacheKeyTO cacheKeyTO) throws CacheCenterConnectionException { |
| 134 | + if(null == jedisCluster || null == cacheKeyTO) { |
| 135 | + return; |
| 136 | + } |
| 137 | + String cacheKey=cacheKeyTO.getCacheKey(); |
| 138 | + if(null == cacheKey || cacheKey.length() == 0) { |
| 139 | + return; |
| 140 | + } |
| 141 | + logger.debug("delete cache:" + cacheKey); |
| 142 | + try { |
| 143 | + String hfield=cacheKeyTO.getHfield(); |
| 144 | + if(null == hfield || hfield.length() == 0) { |
| 145 | + jedisCluster.del(keySerializer.serialize(cacheKey)); |
| 146 | + } else { |
| 147 | + jedisCluster.hdel(keySerializer.serialize(cacheKey), keySerializer.serialize(hfield)); |
| 148 | + } |
| 149 | + this.getAutoLoadHandler().resetAutoLoadLastLoadTime(cacheKeyTO); |
| 150 | + } catch(Exception ex) { |
| 151 | + logger.error(ex.getMessage(), ex); |
| 152 | + } finally { |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public JedisCluster getJedisCluster() { |
| 157 | + return jedisCluster; |
| 158 | + } |
| 159 | + |
| 160 | + public void setJedisCluster(JedisCluster jedisCluster) { |
| 161 | + this.jedisCluster=jedisCluster; |
| 162 | + } |
| 163 | + |
| 164 | + public int getHashExpire() { |
| 165 | + return hashExpire; |
| 166 | + } |
| 167 | + |
| 168 | + public void setHashExpire(int hashExpire) { |
| 169 | + if(hashExpire < 0) { |
| 170 | + return; |
| 171 | + } |
| 172 | + this.hashExpire=hashExpire; |
| 173 | + } |
| 174 | + |
| 175 | + public boolean isHashExpireByScript() { |
| 176 | + return hashExpireByScript; |
| 177 | + } |
| 178 | + |
| 179 | + public void setHashExpireByScript(boolean hashExpireByScript) { |
| 180 | + this.hashExpireByScript=hashExpireByScript; |
| 181 | + } |
| 182 | +} |
0 commit comments