-3

I am newer to use jedis not spring-data-redis. now I want to set Map to cache with jedis. what should I do? who can help me? thank you!

VedantK
  • 9,728
  • 7
  • 66
  • 71
yu_lele
  • 21
  • 5

3 Answers3

0

Let's have a composite map M (instance of HashMap<String,Map<String ,String>>) in java with values:

A -> (A1 -> A1V; A2 -> A2V) 
B -> (B1 -> B1V; B2 -> B2V)

Considering it only one single map the value A1V has a composite key (A, A1) and B2V has a composite key (B, B2) and so on.

Considering that Redis is also a big map (key-value store) and map M needs to be stored under the key K (K - > M) the value A1V has a Redis composite key (K, A, A1) and B2V has a Redis composite key (K, B, B2) and so on.

So you can store your simple values in Redis simply designing your key-namespace like:

Keys:     Values:
K:A:A1 -> A1V
K:A:A2 -> A2V
K:B:B1 -> B1V
K:B:B2 -> B2V

where the keys are concatenated strings with separators.

If your simple values like A1V are not strings but a kind of objects like instances of class User, you can use Redis type Hash: http://redis.io/topics/data-types

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects

Laszlo Hirdi
  • 1,140
  • 12
  • 18
0

Try enableComplexMapKeySerialization, it works well in my case.

GsonBuilder gsonbuilder = new GsonBuilder();
gsonbuilder.enableComplexMapKeySerialization();
gson = gsonbuilder.setPrettyPrinting().create();
-2

we can trans the Map to byte[] with serialization then set map to cache. or use gson change map into string

yu_lele
  • 21
  • 5