Skip to content

Commit

Permalink
Test GuavaCSC and CaffeineCSC (#3742)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Feb 28, 2024
1 parent 2480b02 commit 26606b9
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
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));
}
}

0 comments on commit 26606b9

Please sign in to comment.