Skip to content

Commit 586ff84

Browse files
committed
basic support for max age in default impls
1 parent 7c45a18 commit 586ff84

File tree

5 files changed

+61
-22
lines changed

5 files changed

+61
-22
lines changed

src/main/java/io/vertx/redis/client/CachingRedisClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static CachingRedisClient create(Vertx vertx) {
4848
* @return the caching client
4949
*/
5050
static CachingRedisClient create(Vertx vertx, Redis redis) {
51-
return create(vertx, redis, ClientSideCache.lfu(CachingRedisOptions.DEFAULT_MAX_SIZE));
51+
return create(vertx, redis, ClientSideCache.lfu(new CachingRedisOptions()));
5252
}
5353

5454
/**
@@ -83,7 +83,7 @@ static CachingRedisClient create(Vertx vertx, Redis redis, ClientSideCache cache
8383
* @return the caching client
8484
*/
8585
static CachingRedisClient create(Vertx vertx, Redis redis, CachingRedisOptions options) {
86-
return create(vertx, redis, ClientSideCache.lfu(options.getMaxCacheSize()), options);
86+
return create(vertx, redis, ClientSideCache.lfu(options), options);
8787
}
8888

8989
/**

src/main/java/io/vertx/redis/client/ClientSideCache.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@
2626
public interface ClientSideCache {
2727

2828
/**
29-
* Create a simple least-frequently-used (LFU) cache store with a max size.
29+
* Create a simple least-frequently-used (LFU) cache store with a max size and max item age.
3030
*
31-
* @param maxSize the max size
31+
* @param options the cache options
3232
* @return the new cache store
3333
*/
34-
static ClientSideCache lfu(int maxSize) {
35-
return new LFUClientSideCache(maxSize);
34+
static ClientSideCache lfu(CachingRedisOptions options) {
35+
return new LFUClientSideCache(options);
3636
}
3737

3838
/**
39-
* Create a simple least-recently-used (LRU) cache store with max size.
39+
* Create a simple least-recently-used (LRU) cache store with a max size and max item age.
4040
*
41-
* @param maxSize the max size
41+
* @param options the cache options
4242
* @return the new cache store
4343
*/
44-
static ClientSideCache lru(int maxSize) {
45-
return new LRUClientSideCache(maxSize);
44+
static ClientSideCache lru(CachingRedisOptions options) {
45+
return new LRUClientSideCache(options);
4646
}
4747

4848
/**

src/main/java/io/vertx/redis/client/impl/cache/AbstractClientSideCache.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.vertx.redis.client.impl.cache;
1717

1818
import io.vertx.codegen.annotations.Nullable;
19+
import io.vertx.redis.client.CachingRedisOptions;
1920
import io.vertx.redis.client.ClientSideCache;
2021
import io.vertx.redis.client.Response;
2122

@@ -29,14 +30,16 @@
2930
public abstract class AbstractClientSideCache implements ClientSideCache {
3031

3132
protected final int maxSize;
32-
protected final ConcurrentMap<CacheKey, Response> data;
33+
protected final long maxAgeMillis;
34+
protected final ConcurrentMap<CacheKey, CacheEntry> data;
3335
protected final ConcurrentMap<CacheKey, Long> sorting;
3436

35-
protected AbstractClientSideCache(int maxSize) {
36-
if (maxSize <= 1) {
37+
protected AbstractClientSideCache(CachingRedisOptions options) {
38+
if (options.getMaxCacheSize() <= 1) {
3739
throw new IllegalArgumentException("maxSize must be > 1");
3840
}
39-
this.maxSize = maxSize;
41+
this.maxSize = options.getMaxCacheSize();
42+
this.maxAgeMillis = options.getMaxAgeUnit().toMillis(options.getMaxAge());
4043
this.data = new ConcurrentHashMap<>();
4144
this.sorting = new ConcurrentHashMap<>();
4245
}
@@ -48,14 +51,25 @@ protected AbstractClientSideCache(int maxSize) {
4851
@Override
4952
public @Nullable Response get(CacheKey key) {
5053
sorting.computeIfPresent(key, this::valueAfterGet);
51-
return data.get(key);
54+
final CacheEntry entry = data.get(key);
55+
56+
if (entry == null) {
57+
return null;
58+
}
59+
60+
if (entry.getAgeMillis() > maxAgeMillis) {
61+
delete(key);
62+
return null;
63+
}
64+
65+
return entry.getResponse();
5266
}
5367

5468
@Override
5569
public void put(CacheKey key, Response value) {
70+
check(); // check first so we don't remove the key we're about to put
5671
sorting.put(key, initialValueOnSet(key, value));
57-
data.put(key, value);
58-
check();
72+
data.put(key, new CacheEntry(value));
5973
}
6074

6175
@Override
@@ -77,7 +91,7 @@ public void close() {
7791
}
7892

7993
private void check() {
80-
if (data.size() <= maxSize) {
94+
if (data.size() < maxSize) {
8195
return;
8296
}
8397

@@ -89,4 +103,27 @@ private void check() {
89103
data.remove(toRemove);
90104
sorting.remove(toRemove);
91105
}
106+
107+
protected static final class CacheEntry {
108+
109+
private final Response response;
110+
private long insertTime;
111+
112+
CacheEntry(Response response) {
113+
this.response = response;
114+
this.insertTime = System.currentTimeMillis();
115+
}
116+
117+
Response getResponse() {
118+
return response;
119+
}
120+
121+
long getAgeMillis() {
122+
return System.currentTimeMillis() - insertTime;
123+
}
124+
125+
void touch() {
126+
this.insertTime = System.currentTimeMillis();
127+
}
128+
}
92129
}

src/main/java/io/vertx/redis/client/impl/cache/LFUClientSideCache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package io.vertx.redis.client.impl.cache;
1717

18+
import io.vertx.redis.client.CachingRedisOptions;
1819
import io.vertx.redis.client.ClientSideCache;
1920
import io.vertx.redis.client.Response;
2021

2122
public class LFUClientSideCache extends AbstractClientSideCache implements ClientSideCache {
2223

23-
public LFUClientSideCache(int maxSize) {
24-
super(maxSize);
24+
public LFUClientSideCache(CachingRedisOptions options) {
25+
super(options);
2526
}
2627

2728
@Override

src/main/java/io/vertx/redis/client/impl/cache/LRUClientSideCache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package io.vertx.redis.client.impl.cache;
1717

18+
import io.vertx.redis.client.CachingRedisOptions;
1819
import io.vertx.redis.client.ClientSideCache;
1920
import io.vertx.redis.client.Response;
2021

2122
public class LRUClientSideCache extends AbstractClientSideCache implements ClientSideCache {
2223

23-
public LRUClientSideCache(int maxSize) {
24-
super(maxSize);
24+
public LRUClientSideCache(CachingRedisOptions options) {
25+
super(options);
2526
}
2627

2728
@Override

0 commit comments

Comments
 (0)