-
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.
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 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
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); | ||
} | ||
} |