Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the correct usage of org.redisson.api.RedissonClient.getMap(..)

Tags:

redis

redisson

Service1 running in Node1 Service1 adds user data in Redis Bucket "UserBucket"

Service2 running in Node2 Service2 needs to fetch the latest user data from Redis Bucket "Users"

I maintain a singleton instance of org.redisson.api.RedissonClient inside Service2

Initially my getCache() looked like

private static RMap<String, Map<String,User>> getCache(String bucketName) {
  return redissonCacheClient.getMap(bucketName);
}

Then I looked at getMap() implementation inside org.redisson.api.RedissonClient

return new RedissonMap<K, V>(connectionManager.getCommandExecutor(), name, this, null);

It seems every single getMap(..) call creating new instance of RedissonMap(..)

So I am now maintaining a single instance of RMap as follows (instead of calling getMap() every time) ...

private static RMap<String, Map<String,User>> getCache(String bucketName) {
        RMap<String, Map<String,User>> userMap;
        if (cacheByOrg.containsKey(bucketName)) {
            userMap = cacheByOrg.get(bucketName);
        } else {
            userMap = redissonCacheClient.getMap(bucketName);
            cacheByOrg.put(bucketName, userMap);
        }
        return geoFenceMap;
    }

But now I suspect, I am not getting the latest data from cache.

It would be great, if someone can confirm the correct usage of Redisson client.

Is it Okay to call getMap() many times (is it going to connect to the same instance of cache instance internally so that I always get latest data ? If yes, then can creating so many RedissonMap instance may lead to OOM / GC overhead ?

Otherwise should I call it only once and keep a local copy as demonstrated ?

like image 728
kaniska Mandal Avatar asked Dec 19 '25 09:12

kaniska Mandal


1 Answers

Redisson Group in github confirmed that

With each redisson.getMap() invocation, it returns an RMap instance. This instance can safely shared between each thread, feel free to reuse.

like image 119
kaniska Mandal Avatar answered Dec 24 '25 08:12

kaniska Mandal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!