Redis multiple key for value (multiplying search by keys)

Viewed 5645

I've just got stuck. I have an issue when I working with an entity, I want to save it to Redis, but after some time I want to get this entity, but I don't know what kind of query will be searched it.

So, I need to save several keys into Redis. And I will be able to search my Entity by several queries.

For example, I have an entity:

public class Book
{
  int Id,
  string Name
}

In one time, I want to search this Entity by Id, in another case by Name.

Have you any propositions or solving how I can do it? Maybe, I can use the tags or something like that.

Thanks a lot!!!

2 Answers

Redis isn't like a regular table-based database server; you can't search arbitrarily. If you want to be able to search for something by id, then you need to manually index by id - presumably via a "hash" or similar that goes from the keyed value to the main item key (however you are storing the main items).

If you want convenience over this type of thing, maybe look at RediSearch - an optional module for redis that simplifies this type of scenario while providing search options against nominated members. Your book objects would become "documents" in redisearch nomenclature.

Yes, I found the answer. In Redis, you can use the Dictionary of the values. So, following this issue, we have to save data by using Key & Value, where inside the Value we will have again Key & Value.

Then you will get Values by two keys.

As a result, you will have something like that

HSET myhash field1 "Hello"

More information on official site (https://redis.io/commands/hset)

If we speak about performance, it means that we have time complexity: O(N) where N is the total number of elements in all given sets.

By following these rules: O(n) time

1. Traversing an array
2. Traversing a linked list
3. Linear Search
4. Deletion of a specific element in a Linked List (Not sorted)
5. Comparing two strings
6. Checking for Palindrome
7. Counting/Bucket Sort and here too you can find a million more such examples.... In a nutshell, all Brute Force Algorithms, or Noob ones which require linearity, are based on O(n) time complexity

So, it means that the final access time to the element will be equal to about: O(n) + O(m);

Related