Find key by value

前端 未结 1 1737
生来不讨喜
生来不讨喜 2020-12-05 17:54

The think I\'m trying to implement is an id table. Basically it has the structure (user_id, lecturer_id) which user_id refers to the primary key in my User table and lecture

相关标签:
1条回答
  • 2020-12-05 18:21

    One of the things you learn fast while working with redis is that you get to design your data structure around your accessing needs, specially when it comes to relations (it's not a relational database after all)

    There is no way to search by "value" with a O(1) time complexity as you already noticed, but there are ways to approach what you describe using redis. Here's what I would recommend:

    • Store your user data by user id (in e.g. a hash) as you are already doing.
    • Have an additional set for each lecturer id containing all user ids that correspond to the lecturer id in question.

    This might seem like duplicating the data of the relation, since your user data would have to store the lecture id, and your lecture data would store user ids, but that's the (tiny) price to pay if one is to build relations in a no-relational data store like redis. In practical terms this works well; memory is rarely a bottleneck for small-ish data-sets (think thousands of ids).

    To get a better picture at how are people using redis to model applications with relations, I recommend reading Design and implementation of a simple Twitter clone and the source code of Lamernews, both of which are written by redis author Salvatore Sanfilippo.

    0 讨论(0)
提交回复
热议问题