From 5c357dd452196d916fe8cd851369af416e899039 Mon Sep 17 00:00:00 2001 From: zhangpeng Date: Tue, 12 Jul 2022 17:49:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=9B=B8=E5=85=B3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload-cache-common/pom.xml | 4 ++ .../jarvis/cache/lock/ShardedJedisLock.java | 28 +++++--- .../jarvis/cache/lock/JedisClusterLock.java | 27 ++++++++ .../jarvis/cache/lock/ShardedJedisLock.java | 55 ++++++++++++++++ .../cache/redis/JedisClusterPipeline.java | 60 ++++++++--------- .../com/jarvis/cache/redis/JedisUtil.java | 8 +-- .../redis/RetryableJedisClusterPipeline.java | 19 +++--- .../cache/redis/ShardedJedisCacheManager.java | 64 ++++++++++--------- .../com/jarvis/cache/script/OgnlParser.java | 7 +- .../jarvis/cache/script/SpringELParser.java | 4 +- .../kryo/CacheWrapperSerializer.java | 5 +- .../serializer/kryo/DefaultKryoContext.java | 26 +++++--- pom.xml | 54 ++++++++-------- 13 files changed, 231 insertions(+), 130 deletions(-) create mode 100644 autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/JedisClusterLock.java create mode 100644 autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java diff --git a/autoload-cache-common/pom.xml b/autoload-cache-common/pom.xml index 722c53ab..bfc016bf 100644 --- a/autoload-cache-common/pom.xml +++ b/autoload-cache-common/pom.xml @@ -20,5 +20,9 @@ com.googlecode.combinatoricslib combinatoricslib + + org.slf4j + slf4j-api + diff --git a/autoload-cache-lock/autoload-cache-lock-jedis/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java b/autoload-cache-lock/autoload-cache-lock-jedis/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java index f1dae40d..51e860dd 100644 --- a/autoload-cache-lock/autoload-cache-lock-jedis/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java +++ b/autoload-cache-lock/autoload-cache-lock-jedis/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java @@ -1,8 +1,6 @@ package com.jarvis.cache.lock; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.ShardedJedis; -import redis.clients.jedis.ShardedJedisPool; +import redis.clients.jedis.JedisSharding; import redis.clients.jedis.params.SetParams; /** @@ -10,37 +8,47 @@ */ public class ShardedJedisLock extends AbstractRedisLock { - private ShardedJedisPool shardedJedisPool; + private JedisSharding jedisSharding; - public ShardedJedisLock(ShardedJedisPool shardedJedisPool) { - this.shardedJedisPool = shardedJedisPool; + public ShardedJedisLock(JedisSharding jedisSharding) { + this.jedisSharding = jedisSharding; } - private void returnResource(ShardedJedis shardedJedis) { - shardedJedis.close(); + private void returnResource(JedisSharding jedisSharding) { + jedisSharding.close(); } @Override protected boolean setnx(String key, String val, int expire) { - ShardedJedis shardedJedis = null; + /*ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); Jedis jedis = shardedJedis.getShard(key); return OK.equalsIgnoreCase(jedis.set(key, val, SetParams.setParams().nx().ex(expire))); } finally { returnResource(shardedJedis); + }*/ + try { + return OK.equalsIgnoreCase(jedisSharding.set(key, val, SetParams.setParams().nx().ex(expire))); + } finally { + returnResource(jedisSharding); } } @Override protected void del(String key) { - ShardedJedis shardedJedis = null; + /*ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); Jedis jedis = shardedJedis.getShard(key); jedis.del(key); } finally { returnResource(shardedJedis); + }*/ + try { + jedisSharding.del(key); + } finally { + returnResource(jedisSharding); } } diff --git a/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/JedisClusterLock.java b/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/JedisClusterLock.java new file mode 100644 index 00000000..516e797e --- /dev/null +++ b/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/JedisClusterLock.java @@ -0,0 +1,27 @@ +package com.jarvis.cache.lock; + +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.params.SetParams; + +/** + * + */ +public class JedisClusterLock extends AbstractRedisLock { + + private JedisCluster jedisCluster; + + public JedisClusterLock(JedisCluster jedisCluster) { + this.jedisCluster = jedisCluster; + } + + @Override + protected boolean setnx(String key, String val, int expire) { + return OK.equalsIgnoreCase(jedisCluster.set(key, val, SetParams.setParams().nx().ex(expire))); + } + + @Override + protected void del(String key) { + this.jedisCluster.del(key); + } + +} diff --git a/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java b/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java new file mode 100644 index 00000000..51e860dd --- /dev/null +++ b/autoload-cache-lock/autoload-cache-lock-jedis/src/src/main/java/com/jarvis/cache/lock/ShardedJedisLock.java @@ -0,0 +1,55 @@ +package com.jarvis.cache.lock; + +import redis.clients.jedis.JedisSharding; +import redis.clients.jedis.params.SetParams; + +/** + * + */ +public class ShardedJedisLock extends AbstractRedisLock { + + private JedisSharding jedisSharding; + + public ShardedJedisLock(JedisSharding jedisSharding) { + this.jedisSharding = jedisSharding; + } + + private void returnResource(JedisSharding jedisSharding) { + jedisSharding.close(); + } + + @Override + protected boolean setnx(String key, String val, int expire) { + /*ShardedJedis shardedJedis = null; + try { + shardedJedis = shardedJedisPool.getResource(); + Jedis jedis = shardedJedis.getShard(key); + return OK.equalsIgnoreCase(jedis.set(key, val, SetParams.setParams().nx().ex(expire))); + } finally { + returnResource(shardedJedis); + }*/ + try { + return OK.equalsIgnoreCase(jedisSharding.set(key, val, SetParams.setParams().nx().ex(expire))); + } finally { + returnResource(jedisSharding); + } + } + + @Override + protected void del(String key) { + /*ShardedJedis shardedJedis = null; + try { + shardedJedis = shardedJedisPool.getResource(); + Jedis jedis = shardedJedis.getShard(key); + jedis.del(key); + } finally { + returnResource(shardedJedis); + }*/ + try { + jedisSharding.del(key); + } finally { + returnResource(jedisSharding); + } + } + +} diff --git a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisClusterPipeline.java b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisClusterPipeline.java index 703d8c17..d93c3e27 100644 --- a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisClusterPipeline.java +++ b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisClusterPipeline.java @@ -10,12 +10,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import redis.clients.jedis.Client; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisClusterInfoCache; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.PipelineBase; -import redis.clients.jedis.Response; +import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisRedirectionException; import redis.clients.jedis.util.JedisClusterCRC16; import redis.clients.jedis.util.SafeEncoder; @@ -30,20 +25,21 @@ */ @Getter @Slf4j -public class JedisClusterPipeline extends PipelineBase implements Closeable { +public class JedisClusterPipeline extends Pipeline implements Closeable { private final JedisClusterInfoCache clusterInfoCache; /** * 根据顺序存储每个命令对应的Client */ - private final Queue clients; + private final Queue clients; /** * 用于缓存连接 */ - private final Map jedisMap; + private final Map jedisMap; public JedisClusterPipeline(JedisClusterInfoCache clusterInfoCache) { + super((Connection) null); this.clusterInfoCache = clusterInfoCache; this.clients = new LinkedList<>(); this.jedisMap = new HashMap<>(3); @@ -52,7 +48,8 @@ public JedisClusterPipeline(JedisClusterInfoCache clusterInfoCache) { /** * 同步读取所有数据. 与syncAndReturnAll()相比,sync()只是没有对数据做反序列化 */ - protected void sync() { + @Override + public void sync() { innerSync(null); } @@ -61,7 +58,8 @@ protected void sync() { * * @return 按照命令的顺序返回所有的数据 */ - protected List syncAndReturnAll() { + @Override + public List syncAndReturnAll() { List responseList = new ArrayList<>(clients.size()); innerSync(responseList); return responseList; @@ -71,8 +69,8 @@ private void innerSync(List formatted) { try { Response response; Object data; - for (Client client : clients) { - response = generateResponse(client.getOne()); + for (Connection connection : clients) { + response = generateResponse(connection.getOne()); if (null != formatted) { data = response.get(); formatted.add(data); @@ -89,44 +87,40 @@ private void innerSync(List formatted) { public void close() { clean(); clients.clear(); - for (Jedis jedis : jedisMap.values()) { - flushCachedData(jedis); - jedis.close(); + for (Connection connection : jedisMap.values()) { + flushCachedData(connection); + connection.close(); } jedisMap.clear(); } - private void flushCachedData(Jedis jedis) { + private void flushCachedData(Connection connection) { try { - //FIXME 这个count怎么取值? 执行命令的个数?? - jedis.getClient().getMany(jedisMap.size()); + connection.getMany(jedisMap.size()); //jedis.getClient().getAll(); } catch (RuntimeException ex) { // 其中一个client出问题,后面出问题的几率较大 } } - @Override - protected Client getClient(String key) { + protected Connection getClient(String key) { byte[] bKey = SafeEncoder.encode(key); return getClient(bKey); } - @Override - protected Client getClient(byte[] key) { - Client client = getClient(JedisClusterCRC16.getSlot(key)); - clients.add(client); - return client; + protected Connection getClient(byte[] key) { + Connection connection = getClient(JedisClusterCRC16.getSlot(key)); + clients.add(connection); + return connection; } - private Client getClient(int slot) { - JedisPool pool = clusterInfoCache.getSlotPool(slot); + private Connection getClient(int slot) { + ConnectionPool pool = clusterInfoCache.getSlotPool(slot); // 根据pool从缓存中获取Jedis - Jedis jedis = jedisMap.get(pool); - if (null == jedis) { - jedis = pool.getResource(); - jedisMap.put(pool, jedis); + Connection connection = jedisMap.get(pool); + if (null == connection) { + jedisMap.put(pool, pool.getResource()); } - return jedis.getClient(); + return connection; } } diff --git a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisUtil.java b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisUtil.java index e6e14b9a..54c819d5 100644 --- a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisUtil.java +++ b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/JedisUtil.java @@ -4,7 +4,7 @@ import com.jarvis.cache.to.CacheKeyTO; import com.jarvis.cache.to.CacheWrapper; import lombok.extern.slf4j.Slf4j; -import redis.clients.jedis.PipelineBase; +import redis.clients.jedis.Pipeline; import java.util.Collection; import java.util.Set; @@ -12,7 +12,7 @@ @Slf4j public class JedisUtil { - public static void executeMSet(PipelineBase pipeline, AbstractRedisCacheManager manager, Collection params) throws Exception { + public static void executeMSet(Pipeline pipeline, AbstractRedisCacheManager manager, Collection params) throws Exception { CacheKeyTO cacheKeyTO; String cacheKey; String hfield; @@ -49,7 +49,7 @@ public static void executeMSet(PipelineBase pipeline, AbstractRedisCacheManager } } - public static void executeMGet(PipelineBase pipeline, Set keys) { + public static void executeMGet(Pipeline pipeline, Set keys) { String hfield; String cacheKey; byte[] key; @@ -68,7 +68,7 @@ public static void executeMGet(PipelineBase pipeline, Set keys) { } } - public static void executeDelete(PipelineBase pipeline, Set keys) { + public static void executeDelete(Pipeline pipeline, Set keys) { String hfield; String cacheKey; byte[] key; diff --git a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/RetryableJedisClusterPipeline.java b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/RetryableJedisClusterPipeline.java index 9d940598..be73bf4c 100644 --- a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/RetryableJedisClusterPipeline.java +++ b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/RetryableJedisClusterPipeline.java @@ -1,12 +1,11 @@ package com.jarvis.cache.redis; import lombok.extern.slf4j.Slf4j; -import redis.clients.jedis.BinaryJedisCluster; import redis.clients.jedis.JedisCluster; -import redis.clients.jedis.JedisClusterConnectionHandler; import redis.clients.jedis.JedisClusterInfoCache; -import redis.clients.jedis.JedisSlotBasedConnectionHandler; import redis.clients.jedis.exceptions.JedisMovedDataException; +import redis.clients.jedis.providers.ClusterConnectionProvider; +import redis.clients.jedis.providers.ConnectionProvider; import java.lang.reflect.Field; import java.util.List; @@ -22,19 +21,19 @@ public abstract class RetryableJedisClusterPipeline { private static final Field FIELD_CACHE; static { - FIELD_CONNECTION_HANDLER = getField(BinaryJedisCluster.class, "connectionHandler"); - FIELD_CACHE = getField(JedisClusterConnectionHandler.class, "cache"); + FIELD_CONNECTION_HANDLER = getField(JedisCluster.class, "provider"); + FIELD_CACHE = getField(ConnectionProvider.class, "cache"); } - private final JedisSlotBasedConnectionHandler connectionHandler; + private final ClusterConnectionProvider connectionProvider; private final JedisClusterInfoCache clusterInfoCache; private int maxAttempts = 1; public RetryableJedisClusterPipeline(JedisCluster jedisCluster) { - connectionHandler = getValue(jedisCluster, FIELD_CONNECTION_HANDLER); - clusterInfoCache = getValue(connectionHandler, FIELD_CACHE); + connectionProvider = getValue(jedisCluster, FIELD_CONNECTION_HANDLER); + clusterInfoCache = getValue(connectionProvider, FIELD_CACHE); } public abstract void execute(JedisClusterPipeline pipeline) throws Exception; @@ -51,7 +50,7 @@ public void sync() throws Exception { } catch (JedisMovedDataException jre) { // if MOVED redirection occurred, rebuilds cluster's slot cache, // recommended by Redis cluster specification - connectionHandler.renewSlotCache(); + connectionProvider.renewSlotCache(); if (maxAttempts > 0) { maxAttempts--; sync(); @@ -77,7 +76,7 @@ public List syncAndReturnAll() throws Exception { } catch (JedisMovedDataException jre) { // if MOVED redirection occurred, rebuilds cluster's slot cache, // recommended by Redis cluster specification - connectionHandler.renewSlotCache(); + connectionProvider.renewSlotCache(); if (maxAttempts > 0) { maxAttempts--; return syncAndReturnAll(); diff --git a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/ShardedJedisCacheManager.java b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/ShardedJedisCacheManager.java index fedba751..94118815 100644 --- a/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/ShardedJedisCacheManager.java +++ b/autoload-cache-manager/autoload-cache-manager-jedis/src/main/java/com/jarvis/cache/redis/ShardedJedisCacheManager.java @@ -6,10 +6,8 @@ import com.jarvis.cache.to.CacheWrapper; import lombok.extern.slf4j.Slf4j; import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisSharding; import redis.clients.jedis.Pipeline; -import redis.clients.jedis.ShardedJedis; -import redis.clients.jedis.ShardedJedisPipeline; -import redis.clients.jedis.ShardedJedisPool; import java.io.IOException; import java.lang.reflect.Type; @@ -25,102 +23,108 @@ @Slf4j public class ShardedJedisCacheManager extends AbstractRedisCacheManager { - private final ShardedJedisPool shardedJedisPool; + private final JedisSharding jedisSharding; - public ShardedJedisCacheManager(ShardedJedisPool shardedJedisPool, ISerializer serializer) { + public ShardedJedisCacheManager(JedisSharding jedisSharding, ISerializer serializer) { super(serializer); - this.shardedJedisPool = shardedJedisPool; + this.jedisSharding = jedisSharding; } @Override protected IRedis getRedis() { - return new ShardedJedisClient(shardedJedisPool.getResource(), this); + return new ShardedJedisClient(jedisSharding, this); } public static class ShardedJedisClient implements IRedis { - private final ShardedJedis shardedJedis; + private final JedisSharding jedisSharding; private final AbstractRedisCacheManager cacheManager; - public ShardedJedisClient(ShardedJedis shardedJedis, AbstractRedisCacheManager cacheManager) { - this.shardedJedis = shardedJedis; + public ShardedJedisClient(JedisSharding jedisSharding, AbstractRedisCacheManager cacheManager) { + this.jedisSharding = jedisSharding; this.cacheManager = cacheManager; } @Override public void close() throws IOException { - if (null != shardedJedis) { - shardedJedis.close(); + if (null != jedisSharding) { + jedisSharding.close(); } } @Override public void set(byte[] key, byte[] value) { - Jedis jedis = shardedJedis.getShard(key); - jedis.set(key, value); + //Jedis jedis = shardedJedis.getShard(key); + //jedis.set(key, value); + jedisSharding.set(key, value); } @Override public void setex(byte[] key, int seconds, byte[] value) { - Jedis jedis = shardedJedis.getShard(key); - jedis.setex(key, seconds, value); + //Jedis jedis = shardedJedis.getShard(key); + //jedis.setex(key, seconds, value); + jedisSharding.setex(key, seconds, value); } @Override public void hset(byte[] key, byte[] field, byte[] value) { - Jedis jedis = shardedJedis.getShard(key); - jedis.hset(key, field, value); + //Jedis jedis = shardedJedis.getShard(key); + //jedis.hset(key, field, value); + jedisSharding.hset(key, field, value); } @Override public void hset(byte[] key, byte[] field, byte[] value, int seconds) { - Jedis jedis = shardedJedis.getShard(key); + /*Jedis jedis = shardedJedis.getShard(key); Pipeline pipeline = jedis.pipelined(); pipeline.hset(key, field, value); pipeline.expire(key, seconds); - pipeline.sync(); + pipeline.sync();*/ } @Override public void mset(Collection params) { - ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); + /*ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); pipeline.setShardedJedis(shardedJedis); try { JedisUtil.executeMSet(pipeline, this.cacheManager, params); } catch (Exception ex) { log.error(ex.getMessage(), ex); } - pipeline.sync(); + pipeline.sync();*/ } @Override public byte[] get(byte[] key) { - Jedis jedis = shardedJedis.getShard(key); - return jedis.get(key); + //Jedis jedis = shardedJedis.getShard(key); + //return jedis.get(key); + return jedisSharding.get(key); } @Override public byte[] hget(byte[] key, byte[] field) { - Jedis jedis = shardedJedis.getShard(key); - return jedis.hget(key, field); + //Jedis jedis = shardedJedis.getShard(key); + //return jedis.hget(key, field); + return jedisSharding.hget(key,field); } @Override public Map> mget(Type returnType, Set keys) throws Exception { - ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); + /*ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); pipeline.setShardedJedis(shardedJedis); JedisUtil.executeMGet(pipeline, keys); Collection values = pipeline.syncAndReturnAll(); - return cacheManager.deserialize(keys, values, returnType); + return cacheManager.deserialize(keys, values, returnType);*/ + return null; } @Override public void delete(Set keys) { - ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); + /*ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); pipeline.setShardedJedis(shardedJedis); JedisUtil.executeDelete(pipeline, keys); - pipeline.sync(); + pipeline.sync();*/ } } diff --git a/autoload-cache-script/autoload-cache-script-ognl/src/main/java/com/jarvis/cache/script/OgnlParser.java b/autoload-cache-script/autoload-cache-script-ognl/src/main/java/com/jarvis/cache/script/OgnlParser.java index f6525d51..561270d6 100644 --- a/autoload-cache-script/autoload-cache-script-ognl/src/main/java/com/jarvis/cache/script/OgnlParser.java +++ b/autoload-cache-script/autoload-cache-script-ognl/src/main/java/com/jarvis/cache/script/OgnlParser.java @@ -1,8 +1,7 @@ package com.jarvis.cache.script; import com.jarvis.cache.CacheUtil; -import ognl.Ognl; -import ognl.OgnlContext; +import ognl.*; import java.lang.reflect.Method; import java.util.HashMap; @@ -60,7 +59,9 @@ public T getElValue(String exp, Object target, Object[] arguments, Object re if (hasRetVal) { values.put(RET_VAL, retVal); } - OgnlContext context = new OgnlContext(values); + + //OgnlContext context = new OgnlContext(values); + OgnlContext context = new OgnlContext(null,null,new DefaultMemberAccess(true)); context.setRoot(arguments); Object res = Ognl.getValue(object, context, context.getRoot(), valueType); return (T) res; diff --git a/autoload-cache-script/autoload-cache-script-springel/src/main/java/com/jarvis/cache/script/SpringELParser.java b/autoload-cache-script/autoload-cache-script-springel/src/main/java/com/jarvis/cache/script/SpringELParser.java index 1342fcd8..429667c0 100644 --- a/autoload-cache-script/autoload-cache-script-springel/src/main/java/com/jarvis/cache/script/SpringELParser.java +++ b/autoload-cache-script/autoload-cache-script-springel/src/main/java/com/jarvis/cache/script/SpringELParser.java @@ -1,7 +1,6 @@ package com.jarvis.cache.script; import com.jarvis.cache.CacheUtil; -import lombok.extern.slf4j.Slf4j; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -17,7 +16,6 @@ * * */ -@Slf4j public class SpringELParser extends AbstractScriptParser { /** @@ -43,7 +41,7 @@ public class SpringELParser extends AbstractScriptParser { hash = CacheUtil.class.getDeclaredMethod("getUniqueHashStr", new Class[]{Object.class}); empty = CacheUtil.class.getDeclaredMethod("isEmpty", new Class[]{Object.class}); } catch (NoSuchMethodException e) { - log + e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (Exception e) { diff --git a/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/CacheWrapperSerializer.java b/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/CacheWrapperSerializer.java index f8ee8920..d010c83f 100644 --- a/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/CacheWrapperSerializer.java +++ b/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/CacheWrapperSerializer.java @@ -6,6 +6,8 @@ import com.esotericsoftware.kryo.io.Output; import com.jarvis.cache.to.CacheWrapper; +import java.util.ArrayList; + /** * autoload-cache CacheWrapper Serializer * @@ -22,8 +24,7 @@ public void write(Kryo kryo, Output output, CacheWrapper object) { } @Override - @SuppressWarnings("unchecked") - public CacheWrapper read(Kryo kryo, Input input, Class type) { + public CacheWrapper read(Kryo kryo, Input input, Class type) { int expire = input.readInt(true); long lastLoadTime = input.readLong(true); Object o = kryo.readClassAndObject(input); diff --git a/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/DefaultKryoContext.java b/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/DefaultKryoContext.java index 0484415a..7eeb4942 100644 --- a/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/DefaultKryoContext.java +++ b/autoload-cache-serializer/autoload-cache-serializer-kryo/src/main/java/com/jarvis/cache/serializer/kryo/DefaultKryoContext.java @@ -3,7 +3,7 @@ import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; -import com.esotericsoftware.kryo.pool.KryoPool; +import com.esotericsoftware.kryo.util.Pool; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -17,7 +17,7 @@ */ public class DefaultKryoContext implements KryoContext { private static final int DEFAULT_BUFFER_SIZE = 1024 * 100; - private KryoPool pool; + private Pool pool; private List registrations; public static KryoContext newKryoContextFactory(KryoClassRegistration registration) { @@ -30,11 +30,21 @@ private DefaultKryoContext() { registrations = new ArrayList<>(); //KryoFactory的create方法会延后调用 - pool = new KryoPool.Builder(() -> { + /*pool = new Pool.Builder(() -> { Kryo kryo = new Kryo(); registrations.forEach(reg -> reg.register(kryo)); return kryo; - }).softReferences().build(); + }).softReferences().build();*/ + //FIXME 32够不够 + Pool kryoPool = new Pool(true, false, 32) { + @Override + protected Kryo create () { + Kryo kryo = new Kryo(); + registrations.forEach(reg -> reg.register(kryo)); + return kryo; + } + }; + } @Override @@ -44,23 +54,23 @@ public byte[] serialize(Object obj) { @Override public byte[] serialize(Object obj, int bufferSize) { - Kryo kryo = pool.borrow(); + Kryo kryo = pool.obtain(); try (Output output = new Output(new ByteArrayOutputStream(), bufferSize)) { kryo.writeClassAndObject(output, obj); return output.toBytes(); } finally { - pool.release(kryo); + pool.free(kryo); } } @Override public Object deserialize(byte[] serialized) { - Kryo kryo = pool.borrow(); + Kryo kryo = pool.obtain(); try (Input input = new Input(new ByteArrayInputStream(serialized))) { Object o = kryo.readClassAndObject(input); return o; } finally { - pool.release(kryo); + pool.free(kryo); } } diff --git a/pom.xml b/pom.xml index d4ec38b5..5d7b0952 100644 --- a/pom.xml +++ b/pom.xml @@ -18,9 +18,9 @@ 1.8 UTF-8 UTF-8 - 5.2.21.RELEASE + 5.3.21 2.8.2 - 3.8.1 + 3.10.1 false @@ -59,7 +59,7 @@ org.slf4j slf4j-api - 1.7.23 + 1.7.36 org.apache.commons @@ -69,17 +69,17 @@ org.apache.commons commons-pool2 - 2.4.2 + 2.11.1 com.alibaba fastjson - [1.2.58,) + 1.2.83_noneautotype org.aspectj aspectjrt - 1.8.10 + 1.9.9.1 org.springframework @@ -89,12 +89,12 @@ io.lettuce lettuce-core - 5.3.1.RELEASE + 6.1.8.RELEASE redis.clients jedis - 3.3.0 + 4.2.3 net.spy @@ -104,12 +104,12 @@ com.caucho hessian - [4.0.38,) + [4.0.66,) com.esotericsoftware kryo - 4.0.2 + 5.3.0 uk.com.robust-it @@ -119,12 +119,12 @@ com.fasterxml.jackson.core jackson-databind - 2.12.6.1 + 2.13.3 com.fasterxml.jackson.core jackson-core - 2.10.4 + 2.13.3 cglib @@ -134,42 +134,42 @@ org.ow2.asm asm - 8.0.1 + 9.3 org.msgpack msgpack-core - 0.8.20 + 0.9.1 org.msgpack jackson-dataformat-msgpack - 0.8.20 + 0.9.1 ognl ognl - 3.1.18 + 3.3.2 org.apache.commons commons-lang3 - 3.10 + 3.12.0 com.google.protobuf protobuf-java - 3.16.1 + 3.21.1 com.googlecode.combinatoricslib combinatoricslib - 2.2 + 2.3 junit junit - 4.13.1 + 4.13.2 test @@ -178,7 +178,7 @@ org.projectlombok lombok - 1.18.10 + 1.18.24 compile @@ -206,7 +206,7 @@ org.apache.maven.plugins maven-source-plugin - 2.2.1 + 3.2.1 package @@ -220,7 +220,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.1.1 + 3.4.0 package @@ -236,7 +236,7 @@ org.codehaus.mojo flatten-maven-plugin - 1.2.5 + 1.2.7 true oss @@ -282,7 +282,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 + 3.0.1 verify @@ -295,7 +295,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.8 + 1.6.13 true oss @@ -307,4 +307,4 @@ - + \ No newline at end of file