JSON client-side caching on Java for Valkey and Redis
Client-side caching stores frequently accessed data directly within application memory. Depending on the caching method, this can make application response times faster, improving the overall user experience. Storing and retrieving JSON objects in client-side caches simplifies data handling for further speed gains.
Many developers prefer to use the fast, in-memory data store Redis (or its open-source equivalent Valkey) as a cache. While Java doesn’t natively support Redis, the open-source client Redisson provides a solution with familiar Java objects, classes, and methods. Redisson can even implement JSON client-side caching on Java for Valkey and Redis. Here’s what Java developers need to do.
Client-side caching with a Redisson object
Client-side caching minimizes the need to repeatedly query Redis or Valkey, resulting in faster response times and reduced server load. Redisson’s RLocalCachedJsonStore
object provides a JSON Store implementation with a local cache.
RLocalCachedJsonStore
employs a publish/subscribe (pub/sub) channel to connect local cache instances with the same name and distribute update/invalidate events to maintain consistency. By caching JSON Store entries on the Redisson side, this method enhances read operations up to 45 times faster than other implementations.
LocalCachedJsonStoreOptions options = LocalCachedJsonStoreOptions.name("object_name_example")
// Defines codec used for key
.keyCodec(codec)
// Defines codec used for JSON value
.valueCodec(codec)
// Defines whether to store a cache miss into the local cache.
// Default value is false.
.storeCacheMiss(false);
// Defines store mode of cache data.
// Follow options are available:
// LOCALCACHE - store data in local cache only and use Redis or Valkey only for data update/invalidation.
// LOCALCACHE_REDIS - store data in both Redis or Valkey and local cache.
.storeMode(StoreMode.LOCALCACHE_REDIS)
// Defines Cache provider used as local cache store.
// Follow options are available:
// REDISSON - uses Redisson own implementation
// CAFFEINE - uses Caffeine implementation
.cacheProvider(CacheProvider.REDISSON)
// Defines local cache eviction policy.
// Follow options are available:
// LFU - Counts how often an item was requested. Those that are used least often are discarded first.
// LRU - Discards the least recently used items first
// SOFT - Uses soft references, entries are removed by GC
// WEAK - Uses weak references, entries are removed by GC
// NONE - No eviction
.evictionPolicy(EvictionPolicy.NONE)
// If cache size is 0 then local cache is unbounded.
.cacheSize(1000)
// Defines strategy for load missed local cache updates after connection failure.
//
// Follow reconnection strategies are available:
// CLEAR - Clear local cache if map instance has been disconnected for a while.
// NONE - Default. No reconnection handling
.reconnectionStrategy(ReconnectionStrategy.NONE)
// Defines local cache synchronization strategy.
//
// Follow sync strategies are available:
// INVALIDATE - Default. Invalidate cache entry across all RLocalCachedJsonStore instances on map entry change
// UPDATE - Insert/update cache entry across all RLocalCachedJsonStore instances on map entry change
// NONE - No synchronizations on map changes
.syncStrategy(SyncStrategy.INVALIDATE)
// time to live for each entry in local cache
.timeToLive(Duration.ofSeconds(10))
// max idle time for each entry in local cache
.maxIdle(Duration.ofSeconds(10));
Here is sample Java code for creating the JSON Store object:
LocalCachedJsonStoreOptions ops = LocalCachedJsonStoreOptions.name("test")
.keyCodec(StringCodec.INSTANCE)
.valueCodec(new JacksonCodec<>(MyObject.class));
RLocalCachedJsonStore store = redisson.getLocalCachedJsonStore(ops);
MyObject t1 = new MyObject();
t1.setName("name1");
MyObject t2 = new MyObject();
t2.setName("name2");
Map entries = new HashMap<>();
entries.put("1", t1);
entries.put("2", t2);
// multiple entries at once
store.set(entries);
// or set entry per call
store.set("1", t1);
store.set("2", t2);
// with ttl
store.set("1", t1, Duration.ofSeconds(100));
// set if not set previously
store.setIfAbsent("1", t1);
// set if entry already exists
store.setIfExists("1", t1);
Redisson, the Redis and Valkey client for Java developers
JSON client-side caching is just one of the many Redis and Valkey features Redisson offers Java developers. With Redisson, Java programmers can use Redis or Valkey as a cache, message broker, database, in-memory data store, and numerous other applications.
In addition, Redisson PRO users get extra local cache implementations, including Map Cache, Live Object service, Spring Cache, Hibernate Cache, and many others. To learn more, check out the feature comparison between Redisson and Redisson PRO.