Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-side cache related naming changes #3758

Merged
merged 6 commits into from
Mar 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename to Hash'er's
sazzad16 committed Mar 10, 2024
commit b6830b17a3f590eccf74d8d3837efce448006326
21 changes: 10 additions & 11 deletions src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java
Original file line number Diff line number Diff line change
@@ -4,23 +4,23 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;

import redis.clients.jedis.csc.hash.CommandLongHash;
import redis.clients.jedis.csc.hash.OpenHftCommandHash;
import redis.clients.jedis.csc.hash.CommandLongHasher;
import redis.clients.jedis.csc.hash.OpenHftCommandHasher;

public class CaffeineClientSideCache extends ClientSideCache {

private final Cache<Long, Object> cache;

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache) {
this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE);
this(caffeineCache, DefaultClientSideCacheable.INSTANCE);
}

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, ClientSideCacheable cacheable) {
this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), cacheable);
this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
}

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHash hashing, ClientSideCacheable cacheable) {
super(hashing, cacheable);
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
super(commandHasher, cacheable);
this.cache = caffeineCache;
}

@@ -55,7 +55,7 @@ public static class Builder {
private final TimeUnit expireTimeUnit = TimeUnit.SECONDS;

// not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)'
private CommandLongHash longHashing = null;
private CommandLongHasher commandHasher = null;

private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;

@@ -71,8 +71,8 @@ public Builder ttl(int seconds) {
return this;
}

public Builder hash(CommandLongHash hashing) {
this.longHashing = hashing;
public Builder commandHasher(CommandLongHasher commandHasher) {
this.commandHasher = commandHasher;
return this;
}

@@ -88,8 +88,7 @@ public CaffeineClientSideCache build() {

cb.expireAfterWrite(expireTime, expireTimeUnit);

return longHashing != null
? new CaffeineClientSideCache(cb.build(), longHashing, cacheable)
return commandHasher != null ? new CaffeineClientSideCache(cb.build(), commandHasher, cacheable)
: new CaffeineClientSideCache(cb.build(), cacheable);
}
}
14 changes: 7 additions & 7 deletions src/main/java/redis/clients/jedis/csc/ClientSideCache.java
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
import java.util.function.Function;

import redis.clients.jedis.CommandObject;
import redis.clients.jedis.csc.hash.CommandLongHash;
import redis.clients.jedis.csc.hash.CommandLongHasher;
import redis.clients.jedis.util.SafeEncoder;

/**
@@ -23,15 +23,15 @@ public abstract class ClientSideCache {
protected static final int DEFAULT_EXPIRE_SECONDS = 100;

private final Map<ByteBuffer, Set<Long>> keyToCommandHashes = new ConcurrentHashMap<>();
private final CommandLongHash commandHashing;
private final CommandLongHasher commandHasher;
private final ClientSideCacheable cacheable;

protected ClientSideCache(CommandLongHash commandHashing) {
this(commandHashing, DefaultClientSideCacheable.INSTANCE);
protected ClientSideCache(CommandLongHasher commandHasher) {
this(commandHasher, DefaultClientSideCacheable.INSTANCE);
}

protected ClientSideCache(CommandLongHash commandHashing, ClientSideCacheable cacheable) {
this.commandHashing = commandHashing;
protected ClientSideCache(CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
this.commandHasher = commandHasher;
this.cacheable = cacheable;
}

@@ -81,7 +81,7 @@ public final <T> T get(Function<CommandObject<T>, T> loader, CommandObject<T> co
return loader.apply(command);
}

final long hash = commandHashing.hash(command);
final long hash = commandHasher.hash(command);

T value = (T) getValue(hash);
if (value != null) {
30 changes: 15 additions & 15 deletions src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java
Original file line number Diff line number Diff line change
@@ -5,32 +5,32 @@
import com.google.common.hash.HashFunction;
import java.util.concurrent.TimeUnit;

import redis.clients.jedis.csc.hash.CommandLongHash;
import redis.clients.jedis.csc.hash.GuavaCommandHash;
import redis.clients.jedis.csc.hash.CommandLongHasher;
import redis.clients.jedis.csc.hash.GuavaCommandHasher;

public class GuavaClientSideCache extends ClientSideCache {

private final Cache<Long, Object> cache;

public GuavaClientSideCache(Cache<Long, Object> guavaCache) {
this(guavaCache, GuavaCommandHash.DEFAULT_HASH_FUNCTION);
this(guavaCache, GuavaCommandHasher.DEFAULT_HASH_FUNCTION);
}

public GuavaClientSideCache(Cache<Long, Object> guavaCache, HashFunction hashFunction) {
this(guavaCache, new GuavaCommandHash(hashFunction));
this(guavaCache, new GuavaCommandHasher(hashFunction));
}

public GuavaClientSideCache(Cache<Long, Object> guavaCache, CommandLongHash hashing) {
super(hashing);
public GuavaClientSideCache(Cache<Long, Object> guavaCache, CommandLongHasher commandHasher) {
super(commandHasher);
this.cache = guavaCache;
}

public GuavaClientSideCache(Cache<Long, Object> guavaCache, ClientSideCacheable cacheable) {
this(guavaCache, new GuavaCommandHash(GuavaCommandHash.DEFAULT_HASH_FUNCTION), cacheable);
this(guavaCache, new GuavaCommandHasher(GuavaCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
}

public GuavaClientSideCache(Cache<Long, Object> cache, CommandLongHash hashing, ClientSideCacheable cacheable) {
super(hashing, cacheable);
public GuavaClientSideCache(Cache<Long, Object> cache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
super(commandHasher, cacheable);
this.cache = cache;
}

@@ -66,7 +66,7 @@ public static class Builder {

// not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)'
private HashFunction hashFunction = null;
private CommandLongHash longHashing = null;
private CommandLongHasher commandHasher = null;

private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;

@@ -84,12 +84,12 @@ public Builder ttl(int seconds) {

public Builder hashFunction(HashFunction function) {
this.hashFunction = function;
this.longHashing = null;
this.commandHasher = null;
return this;
}

public Builder hash(CommandLongHash hashing) {
this.longHashing = hashing;
public Builder commandHasher(CommandLongHasher commandHasher) {
this.commandHasher = commandHasher;
this.hashFunction = null;
return this;
}
@@ -106,8 +106,8 @@ public GuavaClientSideCache build() {

cb.expireAfterWrite(expireTime, expireTimeUnit);

return longHashing != null ? new GuavaClientSideCache(cb.build(), longHashing, cacheable)
: hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHash(hashFunction), cacheable)
return commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher, cacheable)
: hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction), cacheable)
: new GuavaClientSideCache(cb.build(), cacheable);
}
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import redis.clients.jedis.CommandObject;
import redis.clients.jedis.args.Rawable;

public abstract class AbstractCommandHash implements CommandLongHash {
public abstract class AbstractCommandHasher implements CommandLongHasher {

@Override
public final long hash(CommandObject command) {
16 changes: 0 additions & 16 deletions src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package redis.clients.jedis.csc.hash;

import redis.clients.jedis.CommandObject;

/**
* The interface for hashing a command object to support client-side caching.
*/
public interface CommandLongHasher {

/**
* Produce a 64-bit signed hash value from a command object.
* @param command the command object
* @return 64-bit signed hash value
*/
long hash(CommandObject command);
}
Original file line number Diff line number Diff line change
@@ -4,13 +4,13 @@
import com.google.common.hash.Hasher;
import redis.clients.jedis.CommandObject;

public class GuavaCommandHash implements CommandLongHash {
public class GuavaCommandHasher implements CommandLongHasher {

public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011();

private final HashFunction function;

public GuavaCommandHash(HashFunction function) {
public GuavaCommandHasher(HashFunction function) {
this.function = function;
}

Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@

import net.openhft.hashing.LongHashFunction;

public class OpenHftCommandHash extends PrimitiveArrayHash implements CommandLongHash {
public class OpenHftCommandHasher extends PrimitiveArrayCommandHasher implements CommandLongHasher {

public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3();

private final LongHashFunction function;

public OpenHftCommandHash(LongHashFunction function) {
public OpenHftCommandHasher(LongHashFunction function) {
this.function = function;
}

Original file line number Diff line number Diff line change
@@ -3,7 +3,12 @@
import redis.clients.jedis.Builder;
import redis.clients.jedis.args.Rawable;

public abstract class PrimitiveArrayHash extends AbstractCommandHash {
/**
* It is possible to extend {@link PrimitiveArrayCommandHasher this abstract class} in order to implement
* {@link CommandLongHasher} as {@link PrimitiveArrayCommandHasher#hashLongs(long[])} and
* {@link PrimitiveArrayCommandHasher#hashBytes(byte[])} can be supported by almost all Java hashing libraries.
*/
public abstract class PrimitiveArrayCommandHasher extends AbstractCommandHasher {

@Override
protected final long hashRawable(Rawable raw) {
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.csc.hash.OpenHftCommandHash;
import redis.clients.jedis.csc.hash.OpenHftCommandHasher;

public class ClientSideCacheLibsTest {

@@ -93,7 +93,7 @@ public void guavaMore() {
@Test
public void caffeineSimple() {
CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10)
.hash(new OpenHftCommandHash(LongHashFunction.xx())).build();
.commandHasher(new OpenHftCommandHasher(LongHashFunction.xx())).build();
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
4 changes: 2 additions & 2 deletions src/test/java/redis/clients/jedis/csc/MapClientSideCache.java
Original file line number Diff line number Diff line change
@@ -4,11 +4,11 @@
import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.csc.hash.PrimitiveArrayHash;
import redis.clients.jedis.csc.hash.PrimitiveArrayCommandHasher;

public class MapClientSideCache extends ClientSideCache {

private static final PrimitiveArrayHash HASHING = new PrimitiveArrayHash() {
private static final PrimitiveArrayCommandHasher HASHING = new PrimitiveArrayCommandHasher() {

@Override
protected long hashLongs(long[] longs) {