Skip to content

Commit

Permalink
Also Guava cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Jan 25, 2024
1 parent f66697f commit 322ffeb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<!-- Optional dependencies -->
<!-- Client-side caching -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.0.0-jre</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand All @@ -96,7 +105,8 @@
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<!-- test -->

<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/redis/clients/jedis/util/CaffeineCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public CaffeineCSC(int maximumSize) {
}

public CaffeineCSC(int maximumSize, int ttlSeconds) {
this(Caffeine.newBuilder().maximumSize(maximumSize).expireAfterWrite(ttlSeconds, TimeUnit.SECONDS).build());
this(Caffeine.newBuilder().maximumSize(maximumSize)
.expireAfterWrite(ttlSeconds, TimeUnit.SECONDS).build());
}

public CaffeineCSC(Cache<ByteBuffer, Object> caffeineCache) {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/redis/clients/jedis/util/GuavaCSC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package redis.clients.jedis.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.ClientSideCache;

public class GuavaCSC extends ClientSideCache {

private static final int DEFAULT_MAXIMUM_SIZE = 10_000;

private final Cache<ByteBuffer, Object> cache;

public GuavaCSC() {
this(DEFAULT_MAXIMUM_SIZE);
}

public GuavaCSC(int maximumSize) {
this(CacheBuilder.newBuilder().maximumSize(maximumSize).build());
}

public GuavaCSC(int maximumSize, int ttlSeconds) {
this(CacheBuilder.newBuilder().maximumSize(maximumSize)
.expireAfterWrite(ttlSeconds, TimeUnit.SECONDS).build());
}

public GuavaCSC(Cache<ByteBuffer, Object> caffeineCache) {
this.cache = caffeineCache;
}

@Override
public final void clear() {
cache.invalidateAll();
}

@Override
protected void remove(ByteBuffer key) {
cache.invalidate(key);
}

@Override
protected void put(ByteBuffer key, Object value) {
cache.put(key, value);
}

@Override
protected Object get(ByteBuffer key) {
return cache.getIfPresent(key);
}
}

0 comments on commit 322ffeb

Please sign in to comment.