Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple key pointing to single value in Redis (Cache) with java

I want to store multiple keys with single value using jedis (Redis cache) with java.

I have three keys like user_1, driver_10, admin_5 and value = this is user, and I want to get value by using any one key among those three.

like image 218
user3864113 Avatar asked Jun 02 '17 07:06

user3864113


People also ask

How set multiple keys in Redis?

Based on my research, I can set multiple keys in a redis using the mset command like so: $redis->mSet(array('key0' => 'value0', 'key1' => 'value1'));

Can Redis have multiple keys?

Redis quickly stretched this concept with data types, where a single key could refer to multiple (even millions of) pieces of data. As modules came to the ecosystem, the idea of a key was stretched even further because a single piece of data could now span multiple keys (for a RediSearch index, for example).

How do I cache a Java object in Redis?

You can't store objects directly into redis. So convert the object into String and then put it in Redis. In order to do that your object must be serialized. Convert the object to ByteArray and use some encoding algorithm (ex base64encoding) and convert it as String then store in Redis.

How many keys can Redis handle?

Redis can handle up to 2^32 keys, and was tested in practice to handle at least 250 million keys per instance. Every hash, list, set, and sorted set, can hold 2^32 elements. In other words your limit is likely the available memory in your system.


1 Answers

Having multiple keys point to same value is not supported in Redis for now, see issue #2668.

You would need a workaround.

Some ideas below, possibly obvious or stupid :)


Maybe have an intermediate key:
- user_10id_123
- driver_5id_123
- id_123data_that_you_dont_want_to_duplicate

You could implement that logic in your client code, or in custom Lua scripts on server, and have your client code use those scripts (but I don't know enough about that to provide details).

If you implement the indirection logic on client side, and if accesses are unbalanced, for example you would access data via user key 99% of the time, and via driver key 1% of the time, it might be worth avoiding 2 client-server round trips for the 99% case. For this you can encode redirections. For example, if first character is # then the rest is the data. If first character is @ then the rest is the actual key.

  • user_10#data_that_you_dont_want_to_duplicate
  • driver_5@user_10
like image 59
Hugues M. Avatar answered Sep 19 '22 15:09

Hugues M.