1

I need to use two key values in Redis cache so that I can retrieve based on one Key? Kindly help on this,

Both keys would be string, and the value will be an entity where the key values will also be present.

Example:

: <compressed_json>

json structure:

    result{
         a{ 
           key: <something>
           b:<something>
           c:<something>
    } 
   }

I need to get value from redis, either by using key or by 'b', but i wouldn't have both values at the same time.

  • Did you check https://redisearch.io? – Guy Korland Jul 13 '20 at 06:34
  • For redis, this often namespace the keys, by combine two or more strings together to become one key, e.g. the key "User:10:article:10001", then can use the key to write and get value, and the entity can be stored as Redis Hash type in Redis or you can also serialize the object to json string, and then store the string to Redis String type. – Li-Tian Jul 13 '20 at 15:10
  • how i can acheive that by using jedis methods? Do you mean to say i need to combine the two key strings as a single key? @Li-Tian – Sathish Kumar Jul 13 '20 at 15:48
  • Best option is : RediSql, see answer below. – Ted Jan 31 '21 at 17:09

2 Answers2

1

To implement the feature, it needs to build a simple Inverted index like below diagram, store your data as three parts.

Index data with inverted index

  • Build two indexs for the two keys, the index is just a Redis String type, the key use the format like index:{key value}, the value is stored the key of the document that's the JSON string.
  • The json string is also stored as Redis String type, key is Doc:{ID}, the ID can be dynamically generated, e.g. UUID/GUID.

Get The Document by index (key 1 or key N)

  • Combine the value to index:{key value} format
  • Use the combined string key to call Redis retrieve the doc key
  • Use the doc key to call Redis retrieve the doc body.

enter image description here

Li-Tian
  • 810
  • 6
  • 5
  • Thank you @Li-Tian. Above approach says that then i need to get the Id first and then json string. Is there any jedis method, that supports this with single retrieve? – Sathish Kumar Jul 14 '20 at 14:11
  • As I know, redis itslef doesn't support use multi key for just one value. You can write the logic in LUA script, so it support single retrieve that's only one network roundtrip. Another approach is store the value two times to Redis Hash, and each time is associated with different key, so you also use single retrieve, but this approach will duplicate store the value . – Li-Tian Jul 14 '20 at 14:24
  • Thank you @Li-Tian – Sathish Kumar Jul 14 '20 at 16:49
1

Another solution would be to deploy RediSQL it will give you access to a fully functional SQL database, where you can query for both keys.

Siscia
  • 1,421
  • 1
  • 12
  • 29