Skip to content

Test GuavaCSC and CaffeineCSC #3742

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

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/util/GuavaCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class GuavaCSC extends ClientSideCache {
private final Cache<Long, Object> cache;
private final HashFunction function;

public GuavaCSC(Cache<Long, Object> guavaCache) {
this(guavaCache, DEFAULT_HASH_FUNCTION);
}

public GuavaCSC(Cache<Long, Object> guavaCache, HashFunction hashFunction) {
this.cache = guavaCache;
this.function = hashFunction;
Expand Down
121 changes: 121 additions & 0 deletions src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.CacheBuilder;

import java.util.function.Supplier;
import net.openhft.hashing.LongHashFunction;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.util.CaffeineCSC;
import redis.clients.jedis.util.GuavaCSC;

public class ClientSideCacheLibsTest {

protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1);

protected Jedis control;

@Before
public void setUp() throws Exception {
control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build());
control.flushAll();
}

@After
public void tearDown() throws Exception {
control.close();
}

private static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build();

private static final Supplier<GenericObjectPoolConfig<Connection>> singleConnectionPoolConfig
= () -> {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(1);
return poolConfig;
};

@Test
public void guavaSimple() {
GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10).hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build();
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
control.del("foo");
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}

@Test
public void guavaMore() {

com.google.common.cache.Cache guava = CacheBuilder.newBuilder().recordStats().build();

try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaCSC(guava), singleConnectionPoolConfig.get())) {
control.set("foo", "bar");
assertEquals(0, guava.size());
assertEquals("bar", jedis.get("foo"));
assertEquals(1, guava.size());
control.flushAll();
assertEquals(1, guava.size());
assertEquals("bar", jedis.get("foo"));
assertEquals(1, guava.size());
jedis.ping();
assertEquals(0, guava.size());
assertNull(jedis.get("foo"));
assertEquals(0, guava.size());
}

com.google.common.cache.CacheStats stats = guava.stats();
assertEquals(1L, stats.hitCount());
assertThat(stats.missCount(), Matchers.greaterThan(0L));
}

@Test
public void caffeineSimple() {
CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10).hashFunction(LongHashFunction.xx()).build();
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
control.del("foo");
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}

@Test
public void caffeineMore() {

com.github.benmanes.caffeine.cache.Cache caffeine = Caffeine.newBuilder().recordStats().build();

try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineCSC(caffeine, LongHashFunction.city_1_1()),
singleConnectionPoolConfig.get())) {
control.set("foo", "bar");
assertEquals(0, caffeine.estimatedSize());
assertEquals("bar", jedis.get("foo"));
assertEquals(1, caffeine.estimatedSize());
control.flushAll();
assertEquals(1, caffeine.estimatedSize());
assertEquals("bar", jedis.get("foo"));
assertEquals(1, caffeine.estimatedSize());
jedis.ping();
assertEquals(0, caffeine.estimatedSize());
assertNull(jedis.get("foo"));
assertEquals(0, caffeine.estimatedSize());
}

com.github.benmanes.caffeine.cache.stats.CacheStats stats = caffeine.stats();
assertEquals(1L, stats.hitCount());
assertThat(stats.missCount(), Matchers.greaterThan(0L));
}
}