-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for client-side caching (#3658)
- Loading branch information
Showing
6 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package redis.clients.jedis; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import redis.clients.jedis.exceptions.JedisException; | ||
import redis.clients.jedis.util.SafeEncoder; | ||
|
||
public class ClientSideCache { | ||
|
||
private final Map<ByteBuffer, Object> cache = new HashMap<>(); | ||
|
||
protected ClientSideCache() { | ||
} | ||
|
||
protected void invalidateKeys(List list) { | ||
if (list == null) { | ||
cache.clear(); | ||
return; | ||
} | ||
|
||
list.forEach(this::invalidateKey); | ||
} | ||
|
||
private void invalidateKey(Object key) { | ||
if (key instanceof byte[]) { | ||
cache.remove(convertKey((byte[]) key)); | ||
} else { | ||
throw new JedisException("" + key.getClass().getSimpleName() + " is not supported. Value: " + String.valueOf(key)); | ||
} | ||
} | ||
|
||
protected void setKey(Object key, Object value) { | ||
cache.put(getMapKey(key), value); | ||
} | ||
|
||
protected <T> T getValue(Object key) { | ||
return (T) getMapValue(key); | ||
} | ||
|
||
private Object getMapValue(Object key) { | ||
return cache.get(getMapKey(key)); | ||
} | ||
|
||
private ByteBuffer getMapKey(Object key) { | ||
if (key instanceof byte[]) { | ||
return convertKey((byte[]) key); | ||
} else { | ||
return convertKey(SafeEncoder.encode(String.valueOf(key))); | ||
} | ||
} | ||
|
||
private static ByteBuffer convertKey(byte[] b) { | ||
return ByteBuffer.wrap(b); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/redis/clients/jedis/JedisClientSideCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package redis.clients.jedis; | ||
|
||
import redis.clients.jedis.exceptions.JedisException; | ||
|
||
public class JedisClientSideCache extends Jedis { | ||
|
||
private final ClientSideCache cache; | ||
|
||
public JedisClientSideCache(final HostAndPort hostPort, final JedisClientConfig config) { | ||
this(hostPort, config, new ClientSideCache()); | ||
} | ||
|
||
public JedisClientSideCache(final HostAndPort hostPort, final JedisClientConfig config, | ||
ClientSideCache cache) { | ||
super(hostPort, config); | ||
if (config.getRedisProtocol() != RedisProtocol.RESP3) { | ||
throw new JedisException("Client side caching is only supported with RESP3."); | ||
} | ||
|
||
this.cache = cache; | ||
this.connection.setClientSideCache(cache); | ||
clientTrackingOn(); | ||
} | ||
|
||
private void clientTrackingOn() { | ||
String reply = connection.executeCommand(new CommandObject<>( | ||
new CommandArguments(Protocol.Command.CLIENT).add("TRACKING").add("ON").add("BCAST"), | ||
BuilderFactory.STRING)); | ||
if (!"OK".equals(reply)) { | ||
throw new JedisException("Could not enable client tracking. Reply: " + reply); | ||
} | ||
} | ||
|
||
@Override | ||
public String get(String key) { | ||
connection.readPushesWithCheckingBroken(); | ||
String cachedValue = cache.getValue(key); | ||
if (cachedValue != null) return cachedValue; | ||
|
||
String value = super.get(key); | ||
if (value != null) cache.setKey(key, value); | ||
return value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/redis/clients/jedis/JedisClientSideCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package redis.clients.jedis; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mockito; | ||
|
||
public class JedisClientSideCacheTest { | ||
|
||
protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1); | ||
|
||
protected Jedis jedis; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); | ||
jedis.flushAll(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
jedis.close(); | ||
} | ||
|
||
private static final JedisClientConfig configForCache = DefaultJedisClientConfig.builder() | ||
.resp3().socketTimeoutMillis(20).password("foobared").build(); | ||
|
||
@Test | ||
public void simple() { | ||
try (JedisClientSideCache jCache = new JedisClientSideCache(hnp, configForCache)) { | ||
jedis.set("foo", "bar"); | ||
assertEquals("bar", jCache.get("foo")); | ||
jedis.del("foo"); | ||
assertNull(jCache.get("foo")); | ||
} | ||
} | ||
|
||
@Test | ||
public void simpleMock() { | ||
ClientSideCache cache = Mockito.mock(ClientSideCache.class); | ||
try (JedisClientSideCache jCache = new JedisClientSideCache(hnp, configForCache, cache)) { | ||
jedis.set("foo", "bar"); | ||
assertEquals("bar", jCache.get("foo")); | ||
jedis.del("foo"); | ||
assertNull(jCache.get("foo")); | ||
} | ||
|
||
InOrder inOrder = Mockito.inOrder(cache); | ||
inOrder.verify(cache).invalidateKeys(Mockito.notNull()); | ||
inOrder.verify(cache).getValue("foo"); | ||
inOrder.verify(cache).setKey("foo", "bar"); | ||
inOrder.verify(cache).invalidateKeys(Mockito.notNull()); | ||
inOrder.verify(cache).getValue("foo"); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
@Test | ||
public void flushall() { | ||
try (JedisClientSideCache jCache = new JedisClientSideCache(hnp, configForCache)) { | ||
jedis.set("foo", "bar"); | ||
assertEquals("bar", jCache.get("foo")); | ||
jedis.flushAll(); | ||
assertNull(jCache.get("foo")); | ||
} | ||
} | ||
} |