Redis只是一个缓存吗

Redis只是一个缓存吗

本文介绍了Redis只是一个缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读一些Redis文档,并在上尝试了该教程。 。到目前为止,我看不到Redis和诸如Velocity或企业库缓存框架之类的缓存技术之间的任何区别

I have been reading some Redis docs and trying the tutorial at http://try.redis-db.com/. So far, I can't see any difference between Redis and caching technologies like Velocity or the Enterprise Library Caching Framework

您实际上只是在将对象添加到内存中使用唯一密钥的数据存储。似乎没有任何关系语义...

You're effectively just adding objects to an in-memory data store using a unique key. There do not seem to be any relational semantics...

我缺少什么?

推荐答案

否,Redis不仅仅是缓存。

No, Redis is much more than a cache.

就像缓存一样,Redis存储键=值对。但是与缓存不同,Redis可让您对值进行操作。 Redis中有5种数据类型-字符串,集合,哈希,列表和排序集合。每种数据类型都会公开各种操作。

Like a Cache, Redis stores key=value pairs. But unlike a cache, Redis lets you operate on the values. There are 5 data types in Redis - Strings, Sets, Hash, Lists and Sorted Sets. Each data type exposes various operations.

了解Redis的最佳方法是对应用程序建模,而不用考虑如何将其存储在数据库中。

The best way to understand Redis is to model an application without thinking about how you are going to store it in a database.

让我们说我们要构建StackOverflow.com。为简单起见,我们需要问题,答案,标签和用户。

Lets say we want to build StackOverflow.com. To keep it simple, we need Questions, Answers, Tags and Users.

每个对象都可以建模为地图。例如,问题是具有字段{id,title,date_asked,votes,asked_by,status}的地图。类似地,答案是具有字段{id,question_id,answer_text,answer_by,票数,状态}的地图。同样,我们可以为用户对象建模。

Each object can be modeled as a Map. For example, a Question is a map with fields {id, title, date_asked, votes, asked_by, status}. Similarly, an Answer is a map with fields {id, question_id, answer_text, answered_by, votes, status}. Similarly, we can model a user object.

每个对象都可以作为哈希直接存储在Redis中。要生成唯一的ID,可以使用atomic递增命令。像这样-

Each of these objects can be directly stored in Redis as a Hash. To generate unique ids, you can use the atomic increment command. Something like this -

$ HINCRBY unique_ids question 1
(integer) 1
$ HMSET question:1 title "Is Redis just a cache?" asked_by 12 votes 0
OK

$ HINCRBY unique_ids answer 1
(integer) 1
$ HMSET answer:1 question_id 1 answer_text "No, its a lot more" answered_by 15 votes 1
OK



处理投票



现在,每当有人提出问题或答案时,您只需要这样做

Handling Up Votes

Now, everytime someone upvotes a question or an answer, you just need to do this

$ HINCRBY question:1 votes 1
(integer) 1
$ HINCRBY question:1 votes 1
(integer) 2



主页问题列表



接下来,我们要存储最新的问题以显示在主页上。如果您正在编写.NET或Java程序,则可以将问题存储在列表中。事实证明,这也是将其存储在Redis中的最佳方法。

List of Questions for Homepage

Next, we want to store the most recent questions to display on the home page. If you were writing a .NET or Java program, you would store the questions in a List. Turns out, that is the best way to store this in Redis as well.

每次有人问问题时,我们都会将其ID添加到列表中。

Every time someone asks a question, we add its id to the list.

$ lpush questions question:1
(integer) 1
$ lpush questions question:2
(integer) 1

现在,当您要渲染首页时,您要求Redis提供最近的25个问题。

Now, when you want to render your homepage, you ask Redis for the most recent 25 questions.

$ lrange questions 0 24
1) "question:100"
2) "question:99"
3) "question:98"
4) "question:97"
5) "question:96"
...
25) "question:76"

现在您有了ID,可以使用流水线从Redis中检索项目并将其显示给用户。

Now that you have the ids, retrieve items from Redis using pipelining and show them to the user.

接下来,我们要检索每个标签的问题。但是,SO允许您在每个标签下查看投票最多的问题,新问题或未回答的问题。

Next, we want to retrieve questions for each tag. But SO allows you to see top voted questions, new questions or unanswered questions under each tag.

要对此建模,我们使用Redis的Sorted Set功能。排序集可让您将分数与每个元素相关联。然后,您可以根据元素的分数来检索它们。

To model this, we use Redis' Sorted Set feature. A Sorted Set allows you to associate a score with each element. You can then retrieve elements based on their scores.

让我们继续使用Redis标签吧

Lets go ahead and do this for the Redis tag

$ zadd questions_by_votes_tagged:redis 2 question:1
(integer) 1
$ zadd questions_by_votes_tagged:redis 10 question:2
(integer) 1
$ zadd questions_by_votes_tagged:redis 5 question:613
(integer) 1
$ zrange questions_by_votes_tagged:redis 0 5
1) "question:1"
2) "question:613"
3) "question:2"
$ zrevrange questions_by_votes_tagged:redis 0 5
1) "question:2"
2) "question:613"
3) "question:1"

我们在这里做了什么?我们将问题添加到排序的集合中,并为每个问题分配分数(投票数)。每次提出问题时,我们都会增加其分数。然后,当用户单击按标记排序的问题标记为Redis的问题时,我们只需执行 zrevrange 并返回最前面的问题。

What did we do over here? We added questions to a sorted set, and associated a score (number of votes) to each question. Each time a question gets upvoted, we will increment its score. And when a user clicks "Questions tagged Redis, sorted by votes", we just do a zrevrange and get back the top questions.

最后,还有一项奖励功能。如果您打开问题页面,那么SO会在添加新问题时通知您。 Redis如何在这里提供帮助?

And finally, a bonus feature. If you keep the questions page opened, SO will notify you when a new question is added. How can Redis help over here?

Redis具有发布订阅模型。您可以创建频道,例如 channel_questions_tagged_redis。然后,您预订个用户到特定频道。添加新问题后,您将发布消息到该频道。然后,所有用户都将收到该消息。您必须使用网络套接字或彗星之类的网络技术才能将消息实际传递到浏览器,但是Redis可以帮助您解决服务器端的所有问题。

Redis has a pub-sub model. You can create channels, for example "channel_questions_tagged_redis". You then subscribe users to a particular channel. When a new question is added, you would publish a message to that channel. All users would then get the message. You will have to use a web technology like web sockets or comet to actually deliver the message to the browser, but Redis helps you with all the plumbing on the server side.

与缓存不同,Redis将数据持久存储在硬盘上。您可以使用主从设置来提供更好的可靠性。要了解更多信息,请在此处遍历持久性和复制主题-

Unlike a Cache, Redis persists data on the hard disk. You can have a master-slave setup to provide better reliability. To learn more, go through Persistence and Replication topics over here - http://redis.io/documentation

这篇关于Redis只是一个缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:27