5

I'm using Spring's RedisTemplate to interface with Redis.

Currently the data I'm storing in Redis uses the OpsForHash operations because that's most appropriate for the data I am storing.

But now I want to add data of a different structure which is Key -> List

Should I therefore, have different instances of RedisTemplate in each of my daos (parameterised as required) but connecting to the same instance of Redis? Is that correct? Or should I have a shared instance of RedisTemplate which I can use for storing both Hash-Structured data and List structured data? If it's the latter how do I do that when I'm restricted by the Parameterisation of the instance? i.e. currently I have

Key (String) --> Map

And now I want to add

Key (String) --> List

Is that possible using a single RedisTemplate?

Thanks!

Kash.
  • 242
  • 1
  • 3
  • 15

2 Answers2

7

Since your key type is String in both cases, you should be able to use the same instance of RedisTemplate, assuming you've parameterized RedisTemplate with the value type of your List. For example:

RedisTemplate<String, String> template;
// Hash Key/Value types can be anything as long as the proper serializers are set
HashOperations<String,String,Integer> hashOps = template.opsForHash();
hashOps.put("foo", "bar", 3);
// List value types are taken from RedisTemplate parameterization
ListOperations<String,String> listOps = template.opsForList();
listOps.leftPush("foolist", "bar");
Jennifer Hickey
  • 632
  • 1
  • 4
  • 9
  • Does this really work? If I tried to add a list to a string type it says types are incompatible. Same with list to hash or hash to list, etc. This was in redis cli but I can't imagine this being different in spring. – stewart99 Apr 01 '15 at 21:28
0

The first solution provided didn't work for me. But I did find a solution and posted it as a solution to another question

Checkout this answer https://stackoverflow.com/a/30484834/4671737

Community
  • 1
  • 1
Tony Murphy
  • 711
  • 9
  • 22