From c72a38cc4f5533d88e97de98d077f68733d26441 Mon Sep 17 00:00:00 2001 From: caojiajun Date: Mon, 9 Sep 2024 14:45:28 +0800 Subject: [PATCH] feat(kv): optimize lru cache clear on slot change (#314) --- .../proxy/upstream/kv/cache/HashLRUCache.java | 40 +++++++------ .../upstream/kv/cache/KeyMetaLRUCache.java | 26 ++++---- .../proxy/upstream/kv/cache/SetLRUCache.java | 46 ++++++++------- .../proxy/upstream/kv/cache/SlotCacheKey.java | 50 ++++++++++++++++ .../proxy/upstream/kv/cache/SlotLRUCache.java | 38 ++++++------ .../upstream/kv/cache/ZSetIndexLRUCache.java | 30 +++++----- .../proxy/upstream/kv/cache/ZSetLRUCache.java | 59 ++++++++++--------- 7 files changed, 171 insertions(+), 118 deletions(-) create mode 100644 camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotCacheKey.java diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/HashLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/HashLRUCache.java index ec1e6c6fb..5a964428f 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/HashLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/HashLRUCache.java @@ -67,43 +67,45 @@ public boolean isHotKey(byte[] key) { } public void putAllForRead(int slot, byte[] cacheKey, RedisHash hash) { - localCache.put(slot, new BytesKey(cacheKey), hash); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.put(slotCacheKey, hash); } public void putAllForWrite(int slot, byte[] cacheKey, RedisHash hash) { - localCacheForWrite.put(slot, new BytesKey(cacheKey), hash); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCacheForWrite.put(slotCacheKey, hash); } public RedisHash getForRead(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisHash hash = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisHash hash = localCache.get(slotCacheKey); if (hash == null) { - hash = localCacheForWrite.get(slot, bytesKey); + hash = localCacheForWrite.get(slotCacheKey); if (hash != null) { - localCache.put(slot, bytesKey, hash); - localCacheForWrite.remove(slot, bytesKey); + localCache.put(slotCacheKey, hash); + localCacheForWrite.remove(slotCacheKey); } } return hash; } public RedisHash getForWrite(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisHash hash = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisHash hash = localCache.get(slotCacheKey); if (hash == null) { - hash = localCacheForWrite.get(slot, bytesKey); + hash = localCacheForWrite.get(slotCacheKey); } return hash; } public Map hset(int slot, byte[] cacheKey, Map fieldMap) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisHash hash1 = localCacheForWrite.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisHash hash1 = localCacheForWrite.get(slotCacheKey); Map result1 = null; if (hash1 != null) { result1 = hash1.hset(fieldMap); } - RedisHash hash2 = localCache.get(slot, bytesKey); + RedisHash hash2 = localCache.get(slotCacheKey); Map result2 = null; if (hash2 != null) { result2 = hash2.hset(fieldMap); @@ -115,13 +117,13 @@ public Map hset(int slot, byte[] cacheKey, Map hdel(int slot, byte[] cacheKey, Set fields) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisHash hash1 = localCacheForWrite.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisHash hash1 = localCacheForWrite.get(slotCacheKey); Map result1 = null; if (hash1 != null) { result1 = hash1.hdel(fields); } - RedisHash hash2 = localCache.get(slot, bytesKey); + RedisHash hash2 = localCache.get(slotCacheKey); Map result2 = null; if (hash2 != null) { result2 = hash2.hdel(fields); @@ -133,9 +135,9 @@ public Map hdel(int slot, byte[] cacheKey, Set field } public void del(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - localCache.remove(slot, bytesKey); - localCacheForWrite.remove(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.remove(slotCacheKey); + localCacheForWrite.remove(slotCacheKey); } public void clear() { diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/KeyMetaLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/KeyMetaLRUCache.java index 162d6bd0f..85943db87 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/KeyMetaLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/KeyMetaLRUCache.java @@ -5,8 +5,6 @@ import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf; import com.netease.nim.camellia.redis.proxy.upstream.kv.conf.RedisKvConf; import com.netease.nim.camellia.redis.proxy.upstream.kv.meta.KeyMeta; -import com.netease.nim.camellia.redis.proxy.util.RedisClusterCRC16Utils; -import com.netease.nim.camellia.tools.utils.BytesKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,12 +56,12 @@ private void rebuild() { } public ValueWrapper get(int slot, byte[] key) { - BytesKey bytesKey = new BytesKey(key); - KeyMeta keyMeta = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, key); + KeyMeta keyMeta = localCache.get(slotCacheKey); if (keyMeta != null) { return () -> keyMeta; } - Boolean bool = nullCache.get(slot, bytesKey); + Boolean bool = nullCache.get(slotCacheKey); if (bool != null) { return () -> null; } @@ -71,21 +69,21 @@ public ValueWrapper get(int slot, byte[] key) { } public void remove(int slot, byte[] key) { - BytesKey bytesKey = new BytesKey(key); - localCache.remove(slot, bytesKey); - nullCache.put(slot, bytesKey, Boolean.TRUE); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, key); + localCache.remove(slotCacheKey); + nullCache.put(slotCacheKey, Boolean.TRUE); } public void put(int slot, byte[] key, KeyMeta keyMeta) { - BytesKey bytesKey = new BytesKey(key); - localCache.put(slot, bytesKey, keyMeta); - nullCache.remove(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, key); + localCache.put(slotCacheKey, keyMeta); + nullCache.remove(slotCacheKey); } public void setNull(int slot, byte[] key) { - BytesKey bytesKey = new BytesKey(key); - localCache.remove(slot, bytesKey); - nullCache.put(slot, bytesKey, Boolean.TRUE); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, key); + localCache.remove(slotCacheKey); + nullCache.put(slotCacheKey, Boolean.TRUE); } public void clear() { diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SetLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SetLRUCache.java index 771e7b409..aa177429b 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SetLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SetLRUCache.java @@ -67,43 +67,45 @@ public boolean isHotKey(byte[] key) { } public void putAllForRead(int slot, byte[] cacheKey, RedisSet set) { - localCache.put(slot, new BytesKey(cacheKey), set); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.put(slotCacheKey, set); } public void putAllForWrite(int slot, byte[] cacheKey, RedisSet set) { - localCacheForWrite.put(slot, new BytesKey(cacheKey), set); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCacheForWrite.put(slotCacheKey, set); } public RedisSet getForRead(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisSet set = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisSet set = localCache.get(slotCacheKey); if (set == null) { - set = localCacheForWrite.get(slot, bytesKey); + set = localCacheForWrite.get(slotCacheKey); if (set != null) { - localCache.put(slot, bytesKey, set); - localCacheForWrite.remove(slot, bytesKey); + localCache.put(slotCacheKey, set); + localCacheForWrite.remove(slotCacheKey); } } return set; } public RedisSet getForWrite(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisSet set = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisSet set = localCache.get(slotCacheKey); if (set == null) { - set = localCacheForWrite.get(slot, bytesKey); + set = localCacheForWrite.get(slotCacheKey); } return set; } public Set sadd(int slot, byte[] cacheKey, Set memberSet) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisSet set = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisSet set = localCache.get(slotCacheKey); Set result = null; if (set != null) { result = set.sadd(memberSet); } - set = localCacheForWrite.get(slot, bytesKey); + set = localCacheForWrite.get(slotCacheKey); if (set != null) { result = set.sadd(memberSet); } @@ -111,13 +113,13 @@ public Set sadd(int slot, byte[] cacheKey, Set memberSet) { } public Set spop(int slot, byte[] cacheKey, int count) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisSet set = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisSet set = localCache.get(slotCacheKey); Set result = null; if (set != null) { result = set.spop(count); } - set = localCacheForWrite.get(slot, bytesKey); + set = localCacheForWrite.get(slotCacheKey); if (set != null) { if (result != null) { set.srem(result); @@ -129,13 +131,13 @@ public Set spop(int slot, byte[] cacheKey, int count) { } public Set srem(int slot, byte[] cacheKey, Collection members) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisSet set = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisSet set = localCache.get(slotCacheKey); Set result = null; if (set != null) { result = set.srem(members); } - set = localCacheForWrite.get(slot, bytesKey); + set = localCacheForWrite.get(slotCacheKey); if (set != null) { result = set.srem(members); } @@ -143,9 +145,9 @@ public Set srem(int slot, byte[] cacheKey, Collection member } public void del(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - localCache.remove(slot, bytesKey); - localCacheForWrite.remove(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.remove(slotCacheKey); + localCacheForWrite.remove(slotCacheKey); } public void clear() { diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotCacheKey.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotCacheKey.java new file mode 100644 index 000000000..987f55a5e --- /dev/null +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotCacheKey.java @@ -0,0 +1,50 @@ +package com.netease.nim.camellia.redis.proxy.upstream.kv.cache; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +/** + * Created by caojiajun on 2024/9/9 + */ +public class SlotCacheKey { + private final byte[] key; + private final int slot; + private int hashCode; + + public SlotCacheKey(int slot, byte[] key) { + this.slot = slot; + this.key = key; + } + + public byte[] getKey() { + return key; + } + + public int getSlot() { + return slot; + } + + @Override + public boolean equals(Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + + SlotCacheKey slotCacheKey = (SlotCacheKey) object; + + return Arrays.equals(key, slotCacheKey.key); + } + + @Override + public int hashCode() { + if (hashCode == 0) { + hashCode = Arrays.hashCode(key); + } + return hashCode; + } + + @Override + public String toString() { + if (key == null) return null; + return new String(key, StandardCharsets.UTF_8); + } +} diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotLRUCache.java index 8c64a831e..d79a948e4 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/SlotLRUCache.java @@ -13,7 +13,7 @@ */ public class SlotLRUCache { - private final ConcurrentLinkedHashMap[] array; + private final ConcurrentLinkedHashMap[] array; private final int segmentSize; private final boolean is2Power; @@ -22,7 +22,7 @@ public SlotLRUCache(int capacity) { is2Power = MathUtil.is2Power(segmentSize); array = new ConcurrentLinkedHashMap[segmentSize]; for (int i = 0; i< segmentSize; i++) { - ConcurrentLinkedHashMap subMap = new ConcurrentLinkedHashMap.Builder() + ConcurrentLinkedHashMap subMap = new ConcurrentLinkedHashMap.Builder() .initialCapacity(capacity / segmentSize) .maximumWeightedCapacity(capacity / segmentSize) .build(); @@ -30,46 +30,46 @@ public SlotLRUCache(int capacity) { } } - public V get(int slot, BytesKey cacheKey) { - int index = MathUtil.mod(is2Power, slot, segmentSize); - ConcurrentLinkedHashMap subMap = array[index]; + public V get(SlotCacheKey cacheKey) { + int index = MathUtil.mod(is2Power, cacheKey.getSlot(), segmentSize); + ConcurrentLinkedHashMap subMap = array[index]; return subMap.get(cacheKey); } - public void put(int slot, BytesKey cacheKey, V value) { - int index = MathUtil.mod(is2Power, slot, segmentSize); - ConcurrentLinkedHashMap subMap = array[index]; + public void put(SlotCacheKey cacheKey, V value) { + int index = MathUtil.mod(is2Power, cacheKey.getSlot(), segmentSize); + ConcurrentLinkedHashMap subMap = array[index]; subMap.put(cacheKey, value); } - public void remove(int slot, BytesKey cacheKey) { - int index = MathUtil.mod(is2Power, slot, segmentSize); - ConcurrentLinkedHashMap subMap = array[index]; + public void remove(SlotCacheKey cacheKey) { + int index = MathUtil.mod(is2Power, cacheKey.getSlot(), segmentSize); + ConcurrentLinkedHashMap subMap = array[index]; subMap.remove(cacheKey); } public void clear() { - for (ConcurrentLinkedHashMap subMap : array) { + for (ConcurrentLinkedHashMap subMap : array) { subMap.clear(); } } public void clear(int slot) { int index = MathUtil.mod(is2Power, slot, segmentSize); - ConcurrentLinkedHashMap subMap = array[index]; - subMap.clear(); + ConcurrentLinkedHashMap subMap = array[index]; + subMap.entrySet().removeIf(entry -> entry.getKey().getSlot() == slot); } public void setCapacity(int capacity) { - for (ConcurrentLinkedHashMap subMap : array) { + for (ConcurrentLinkedHashMap subMap : array) { subMap.setCapacity(capacity / segmentSize); } } public long size() { long estimateSize = 0; - for (ConcurrentLinkedHashMap map : array) { + for (ConcurrentLinkedHashMap map : array) { estimateSize += map.size(); } return estimateSize; @@ -77,9 +77,9 @@ public long size() { public long estimateSize() { long estimateSize = 0; - for (ConcurrentLinkedHashMap map : array) { - estimateSize += map.size() * 8L; - for (Map.Entry entry : map.entrySet()) { + for (ConcurrentLinkedHashMap map : array) { + estimateSize += map.size() * 12L; + for (Map.Entry entry : map.entrySet()) { estimateSize += entry.getKey().getKey().length; V value = entry.getValue(); if (value instanceof EstimateSizeValue) { diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetIndexLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetIndexLRUCache.java index aa51dcfc0..994c00290 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetIndexLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetIndexLRUCache.java @@ -59,43 +59,43 @@ private void rebuild() { } public byte[] getForRead(int slot, byte[] cacheKey, BytesKey ref) { - BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey())); - byte[] bytes = localCache.get(slot, indexCacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, BytesUtils.merge(cacheKey, ref.getKey())); + byte[] bytes = localCache.get(slotCacheKey); if (bytes != null) { return bytes; } - bytes = localCacheForWrite.get(slot, indexCacheKey); + bytes = localCacheForWrite.get(slotCacheKey); if (bytes != null) { - localCache.put(slot, indexCacheKey, bytes); - localCacheForWrite.remove(slot, indexCacheKey); + localCache.put(slotCacheKey, bytes); + localCacheForWrite.remove(slotCacheKey); return bytes; } return null; } public byte[] getForWrite(int slot, byte[] cacheKey, BytesKey ref) { - BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey())); - byte[] bytes = localCacheForWrite.get(slot, indexCacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, BytesUtils.merge(cacheKey, ref.getKey())); + byte[] bytes = localCacheForWrite.get(slotCacheKey); if (bytes != null) { return bytes; } - return localCache.get(slot, indexCacheKey); + return localCache.get(slotCacheKey); } public void putForWrite(int slot, byte[] cacheKey, BytesKey ref, byte[] raw) { - BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey())); - localCacheForWrite.put(slot, indexCacheKey, raw); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, BytesUtils.merge(cacheKey, ref.getKey())); + localCacheForWrite.put(slotCacheKey, raw); } public void putForRead(int slot, byte[] cacheKey, BytesKey ref, byte[] raw) { - BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey())); - localCache.put(slot, indexCacheKey, raw); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, BytesUtils.merge(cacheKey, ref.getKey())); + localCache.put(slotCacheKey, raw); } public void remove(int slot, byte[] cacheKey, BytesKey ref) { - BytesKey indexCacheKey = new BytesKey(BytesUtils.merge(cacheKey, ref.getKey())); - localCache.remove(slot, indexCacheKey); - localCacheForWrite.remove(slot, indexCacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, BytesUtils.merge(cacheKey, ref.getKey())); + localCache.remove(slotCacheKey); + localCacheForWrite.remove(slotCacheKey); } public void clear() { diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetLRUCache.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetLRUCache.java index 8413d74fa..7938623da 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetLRUCache.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/cache/ZSetLRUCache.java @@ -68,42 +68,45 @@ public boolean isHotKey(byte[] key) { } public void putZSetForWrite(int slot, byte[] cacheKey, RedisZSet zSet) { - localCacheForWrite.put(slot, new BytesKey(cacheKey), zSet); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCacheForWrite.put(slotCacheKey, zSet); } public void putZSetForRead(int slot, byte[] cacheKey, RedisZSet zSet) { - localCache.put(slot, new BytesKey(cacheKey), zSet); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.put(slotCacheKey, zSet); } public RedisZSet getForRead(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisZSet zSet = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet == null) { - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { - localCache.put(slot, bytesKey, zSet); - localCacheForWrite.remove(slot, bytesKey); + localCache.put(slotCacheKey, zSet); + localCacheForWrite.remove(slotCacheKey); } } return zSet; } public RedisZSet getForWrite(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - RedisZSet zSet = localCache.get(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet == null) { - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); } return zSet; } public Map zadd(int slot, byte[] cacheKey, Map map) { - RedisZSet zSet = localCache.get(slot, new BytesKey(cacheKey)); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + RedisZSet zSet = localCache.get(slotCacheKey); Map result = null; if (zSet != null) { result = zSet.zadd(map); } - zSet = localCacheForWrite.get(slot, new BytesKey(cacheKey)); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { result = zSet.zadd(map); } @@ -111,13 +114,13 @@ public Map zadd(int slot, byte[] cacheKey, Map zrem(int slot, byte[] cacheKey, Collection members) { + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); Map result = null; - BytesKey bytesKey = new BytesKey(cacheKey); - RedisZSet zSet = localCache.get(slot, bytesKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet != null) { result = zSet.zrem(members); } - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { result = zSet.zrem(members); } @@ -125,13 +128,13 @@ public Map zrem(int slot, byte[] cacheKey, Collection zremrangeByRank(int slot, byte[] cacheKey, int start, int stop) { - BytesKey bytesKey = new BytesKey(cacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); Map result = null; - RedisZSet zSet = localCache.get(slot, bytesKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByRank(start, stop); } - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByRank(start, stop); } @@ -139,14 +142,13 @@ public Map zremrangeByRank(int slot, byte[] cacheKey, int star } public Map zremrangeByScore(int slot, byte[] cacheKey, ZSetScore minScore, ZSetScore maxScore) { - BytesKey bytesKey = new BytesKey(cacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); Map result = null; - - RedisZSet zSet = localCache.get(slot, bytesKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByScore(minScore, maxScore); } - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByScore(minScore, maxScore); } @@ -154,14 +156,13 @@ public Map zremrangeByScore(int slot, byte[] cacheKey, ZSetSco } public Map zremrangeByLex(int slot, byte[] cacheKey, ZSetLex minLex, ZSetLex maxLex) { - BytesKey bytesKey = new BytesKey(cacheKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); Map result = null; - - RedisZSet zSet = localCache.get(slot, bytesKey); + RedisZSet zSet = localCache.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByLex(minLex, maxLex); } - zSet = localCacheForWrite.get(slot, bytesKey); + zSet = localCacheForWrite.get(slotCacheKey); if (zSet != null) { result = zSet.zremrangeByLex(minLex, maxLex); } @@ -169,9 +170,9 @@ public Map zremrangeByLex(int slot, byte[] cacheKey, ZSetLex m } public void del(int slot, byte[] cacheKey) { - BytesKey bytesKey = new BytesKey(cacheKey); - localCache.remove(slot, bytesKey); - localCacheForWrite.remove(slot, bytesKey); + SlotCacheKey slotCacheKey = new SlotCacheKey(slot, cacheKey); + localCache.remove(slotCacheKey); + localCacheForWrite.remove(slotCacheKey); } public void clear() {