-
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.
Prepare client side caching - design 2 (#3889)
* Separate CacheConnection * Introduce CacheKey and CacheEntry * Little tweak maximumSize test in CaffeineClientSideCacheTest * Remove resetting timeout; we'll PING instead
- Loading branch information
Showing
18 changed files
with
247 additions
and
145 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
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
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
66 changes: 66 additions & 0 deletions
66
src/main/java/redis/clients/jedis/csc/CacheConnection.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,66 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import redis.clients.jedis.Connection; | ||
import redis.clients.jedis.JedisClientConfig; | ||
import redis.clients.jedis.JedisSocketFactory; | ||
import redis.clients.jedis.Protocol; | ||
import redis.clients.jedis.RedisProtocol; | ||
import redis.clients.jedis.exceptions.JedisException; | ||
import redis.clients.jedis.util.RedisInputStream; | ||
|
||
public class CacheConnection extends Connection { | ||
|
||
private final ClientSideCache clientSideCache; | ||
private final ReentrantLock lock; | ||
|
||
public CacheConnection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, | ||
ClientSideCache clientSideCache) { | ||
|
||
super(socketFactory, clientConfig); | ||
|
||
if (protocol != RedisProtocol.RESP3) { | ||
throw new JedisException("Client side caching is only supported with RESP3."); | ||
} | ||
this.clientSideCache = Objects.requireNonNull(clientSideCache); | ||
initializeClientSideCache(); | ||
|
||
lock = new ReentrantLock(); | ||
} | ||
|
||
@Override | ||
protected Object protocolRead(RedisInputStream inputStream) { | ||
if (lock != null) { | ||
lock.lock(); | ||
try { | ||
return Protocol.read(inputStream); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} else { | ||
return Protocol.read(inputStream); | ||
} | ||
} | ||
|
||
@Override | ||
protected void protocolReadPushes(RedisInputStream inputStream) { | ||
if (lock != null && lock.tryLock()) { | ||
try { | ||
//super.setSoTimeout(1); | ||
Protocol.readPushes(inputStream, clientSideCache); | ||
} finally { | ||
//super.rollbackTimeout(); | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
|
||
private void initializeClientSideCache() { | ||
sendCommand(Protocol.Command.CLIENT, "TRACKING", "ON"); | ||
String reply = getStatusCodeReply(); | ||
if (!"OK".equals(reply)) { | ||
throw new JedisException("Could not enable client tracking. Reply: " + reply); | ||
} | ||
} | ||
} |
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,30 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import redis.clients.jedis.Connection; | ||
import redis.clients.jedis.annots.Internal; | ||
|
||
@Internal | ||
public class CacheEntry<T> { | ||
|
||
private final CacheKey<T> cacheKey; | ||
private final T value; | ||
private final Connection connection; | ||
|
||
public CacheEntry(CacheKey<T> cacheKey, T value, Connection connection) { | ||
this.cacheKey = cacheKey; | ||
this.value = value; | ||
this.connection = connection; | ||
} | ||
|
||
public CacheKey<T> getCacheKey() { | ||
return cacheKey; | ||
} | ||
|
||
public T getValue() { | ||
return value; | ||
} | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
} |
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,28 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import java.util.Objects; | ||
import redis.clients.jedis.CommandObject; | ||
import redis.clients.jedis.annots.Internal; | ||
|
||
@Internal | ||
public class CacheKey<T> { | ||
|
||
private final CommandObject<T> command; | ||
|
||
public CacheKey(CommandObject<T> command) { | ||
this.command = Objects.requireNonNull(command); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return command.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
final CacheKey other = (CacheKey) obj; | ||
return Objects.equals(this.command, other.command); | ||
} | ||
} |
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
Oops, something went wrong.