Redis types and their respective pros and cons

2019-05-25 02:00发布

I understand that Redis lists, sets, and hashes

what are the pros/cons of each type, and an example of where a set would be used over a list, or a hash over a set etc

标签: redis
1条回答
爷的心禁止访问
2楼-- · 2019-05-25 02:55

The major thing you want to think about is what kind of operations you are going to be asking of it... This over than the performance. Because, if an operation is not available, it just won't work.

Look at the Redis commands documentation, first of all, and make sure the set of commands available are there for you. Most of the reasoning I am using here is the same in general purpose programming as well. For example, why you would use a dictionary vs. a list in Python. The Redis command documentation typically has the Big-O notation for the operations. Most individual lookups, inserts, etc. are O(1), so there isn't much difference across types. I haven't found much difference in terms of storage space for each of these, either.

There are many other reasons here because I'm being rather general. This post could likely be expanded out to a chapter in a book! It's not a straightforward question.

Hash: This is for if you want to grab values by keys. Almost like a key-value store in a key-value store. Also, you can do "class"-like things, by having subparameters by name (e.g., with keys "address", "date of birth", ...)

List: Basically a set without a key, it is a collection of things where finding an individual one instantly isn't a big deal. For example, you might have a list of transactions a user has been involved in.

Set: Basically a Hash, but has no value. This is for when you want to know "is this thing in a set or not?". Sets, unlike lists, take constant time to figure out if something is a member. Also, unlike lists, the items in the set are unique. With sets, you can also do intersections and other set stuff. This is not something you can do with Hashes. For example, you want to find all the users that bought stuff at your Italy office (one set), and all the users that bought stuff at your France office (another set), by doing a union .

Sorted Sets: Like a set, but has additional options involving finding ranges or ranks of items.

查看更多
登录 后发表回答