-
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.
Support TTL in client side caching (using Caffeine library)
- Loading branch information
Showing
8 changed files
with
128 additions
and
55 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
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,50 @@ | ||
package redis.clients.jedis.util; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.TimeUnit; | ||
import redis.clients.jedis.ClientSideCache; | ||
|
||
public class CaffeineCSC extends ClientSideCache { | ||
|
||
private static final int DEFAULT_MAXIMUM_SIZE = 10_000; | ||
|
||
private final Cache<ByteBuffer, Object> cache; | ||
|
||
public CaffeineCSC() { | ||
this(DEFAULT_MAXIMUM_SIZE); | ||
} | ||
|
||
public CaffeineCSC(int maximumSize) { | ||
this(Caffeine.newBuilder().maximumSize(maximumSize).build()); | ||
} | ||
|
||
public CaffeineCSC(int maximumSize, int ttlSeconds) { | ||
this(Caffeine.newBuilder().maximumSize(maximumSize).expireAfterWrite(ttlSeconds, TimeUnit.SECONDS).build()); | ||
} | ||
|
||
public CaffeineCSC(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); | ||
} | ||
} |
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
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,39 @@ | ||
package redis.clients.jedis.util; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import redis.clients.jedis.ClientSideCache; | ||
|
||
public class MapCSC extends ClientSideCache { | ||
|
||
private final Map<ByteBuffer, Object> cache; | ||
|
||
public MapCSC() { | ||
this(new HashMap<>()); | ||
} | ||
|
||
public MapCSC(Map<ByteBuffer, Object> map) { | ||
this.cache = map; | ||
} | ||
|
||
@Override | ||
public final void clear() { | ||
cache.clear(); | ||
} | ||
|
||
@Override | ||
protected void remove(ByteBuffer key) { | ||
cache.remove(key); | ||
} | ||
|
||
@Override | ||
protected void put(ByteBuffer key, Object value) { | ||
cache.put(key, value); | ||
} | ||
|
||
@Override | ||
protected Object get(ByteBuffer key) { | ||
return cache.get(key); | ||
} | ||
} |