Skip to content

Commit 1424ccd

Browse files
committed
feat: replace concurrent hashmap with caffeine
1 parent d328db4 commit 1424ccd

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pixels-common/src/main/java/io/pixelsdb/pixels/common/index/CachingSinglePointIndex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public CachingSinglePointIndex()
3838
if (cacheEnabled)
3939
{
4040
long capacity = Long.parseLong(config.getProperty("index.cache.capacity"));
41-
long expireAfterAccessSeconds = Long.parseLong(config.getProperty("index.cache.expiration.seconds"));
42-
this.cache = new LatestVersionCache();
41+
long expirationSeconds = Long.parseLong(config.getProperty("index.cache.expiration.seconds"));
42+
this.cache = new LatestVersionCache(capacity, expirationSeconds);
4343
} else
4444
{
4545
this.cache = null;

pixels-common/src/main/java/io/pixelsdb/pixels/common/index/LatestVersionCache.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@
2424
import io.pixelsdb.pixels.index.IndexProto;
2525

2626
import java.nio.charset.StandardCharsets;
27-
import java.util.Objects;
28-
import java.util.concurrent.ConcurrentHashMap;
29-
import java.util.concurrent.Executor;
30-
import java.util.concurrent.Executors;
3127
import java.util.concurrent.TimeUnit;
3228

3329
public class LatestVersionCache
3430
{
35-
private final ConcurrentHashMap<String, String> cache;
31+
private final Cache<String, String> cache;
3632

37-
public LatestVersionCache()
33+
public LatestVersionCache(long capacity, long expirationSeconds)
3834
{
39-
this.cache = new ConcurrentHashMap<>();
35+
this.cache = Caffeine.newBuilder()
36+
.maximumSize(capacity)
37+
.expireAfterWrite(expirationSeconds, TimeUnit.SECONDS)
38+
.build();
4039
}
4140

4241
public String get(String key)
4342
{
44-
return cache.get(key);
43+
return cache.getIfPresent(key);
4544
}
4645

4746
public void put(String key, String value)
@@ -51,12 +50,17 @@ public void put(String key, String value)
5150

5251
public void invalidate(String key)
5352
{
54-
cache.remove(key);
53+
cache.invalidate(key);
5554
}
5655

5756
public static String buildCacheKey(IndexProto.IndexKey key)
5857
{
59-
return key.getTableId() + key.getIndexId() + key.getKey().toString(StandardCharsets.ISO_8859_1);
58+
String indexKey = key.getKey().toString(StandardCharsets.ISO_8859_1);
59+
return new StringBuilder(20 + 20 + indexKey.length())
60+
.append(key.getTableId())
61+
.append(key.getIndexId())
62+
.append(indexKey)
63+
.toString();
6064
}
6165

6266
public static String buildCacheValue(long timestamp, long rowId)

0 commit comments

Comments
 (0)