Search code examples
javaspringspring-bootredisspring-data-redis

Spring data redis - Hash of Map<String, Long> is retrieved as Map<String, Integer>, how to fix this?


I am using Spring Data Redis and using hash operations to save the Map<String, Long> as a hash in Redis.

Suppose hash is "some hash", hashkey is "unique key" & hash value is "Map<String, Long>".

 Map<String,Long > data= new HashMap<>());
 data.put("k", 21474836470); //value = Integer.MAX*10

 hashOperations.put("some hash", "unique key", data);

 Map<String,Integer> result = hashOperations.get( "some hash", "unique key");

The problem here is the result is Map<String, Integer> instead of Map<String, Long>.

When the value in a map stored in Redis is greater than Integer.MAX, then I will get the wrong value in the result map.

enter image description here

I am able to save 10 times of Integer.MAX value through Redis-CLI. Seems the range of Redis integer is different from Java Integer.

Through Redis CLI I can get a large value of an integer, but on Java, the return type is Java integer, which will not work for Long value.

I am using Jackson2JsonRedisSerializer as a hash value and value serializer.

How do I fix this?


Solution

  • I tested with a value greater than Integer.MAX and Spring is able to convert it into a Long value.

    For values less than Integer.MAX it is converting to Integer. For values greater than Integer.MAX it is converting to Long.