Redis 只是一个缓存吗? [英] Is Redis just a cache?

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

问题描述

我一直在阅读一些 Redis 文档并在 http://try.redis-db.com/ 尝试教程.到目前为止,我看不出 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, ask_by, status} 的地图.类似地,Answer 是一个包含字段 {id, question_id, answer_text, answers_by, votes, status} 的地图.类似地,我们可以为用户对象建模.

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 increment 命令.像这样的东西 -

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 的排序集功能.排序集允许您将分数与每个元素相关联.然后,您可以根据元素的分数检索元素.

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".然后您订阅 用户到特定频道.添加新问题时,您将发布一条消息到该频道.然后所有用户都会收到该消息.您将不得不使用网络套接字或 Comet 等网络技术将消息实际传送到浏览器,但 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 将数据持久保存在硬盘上.您可以进行主从设置以提供更好的可靠性.要了解更多信息,请浏览此处的持久性和复制主题 - http://redis.io/documentation

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 只是一个缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆