2424import io .pixelsdb .pixels .index .IndexProto ;
2525
2626import 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 ;
3127import java .util .concurrent .TimeUnit ;
3228
3329public 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