首页 >> 大全

很好的redis入门

2023-12-17 大全 16 作者:考证青年

最近互联网公司中很流行key-value数据库,但这是大学数据库课上学不到的东西,所以我讲通俗的翻译篇这方面的教程

简短总结下Redis NOSQL世界的充满冒险的旅程

我们旅途的行程

就像其他的旅途一样, 我们的旅途也是有很多行程组成, 在开始之前我先将他们写下来:

Redis? 这是什么?可用的数据类型表在哪里?一个简单例子回顾 行程1: Redis? 这是什么?

为了长话短说, Redis是一种加速的key-value数据库. 为什么加速? 简单说他非常快(所有的数据都加载在内存中和磁盘中)和丰富的特色 (它支持多种数据类型和对其的复杂操作).

一个很重要的redis特色是它不是普通意义上的数据库;它是一种理解上的数据库(能为你储存和保存数据),他它不支持任何的sql语句(其他关系型数据库都支持sql语句). 不要害怕, Redis不是一种容易丢失数据的数据库 。它只是不提供我们钟爱的SQL语句的支持,但是它有健壮和测试的能和我们交互的协议 。

在Redis中,不处理表和查询,连接,视图语句;不提供整型和浮动字符串等。你不得不去操作低层的数据集结构和更多的原始类型。

行程2:可用的数据类型

让我们进一步观察下这种奇怪的数据库的工作原理。按照前面行程所说, Redis是基于key-value形式存储的,我们重点要观察他key的部分.

Key是一种简单的字符串( not safe. :now, key are safe.) 例如“" 或"".你不能在key中有空格,但对字符等支持的非常好如 ".", ":", "_" , 所以我们可以用以下类似的key "", "user:123:age", "user:123:" 他们是完全合法的。

我们必须注意key,因为它不像关系型数据库,依靠字段名来区分数据。 Redis 根本就不认识数据表,非常简单的语句" from users WHERE =123;"不得不通过其他的方法来实现。举个例子,为了实现前面的SQL查询,我们不得不通过"user:123:"为key来得到你要的结果。通过ID 来达到关系型数据库中字段的效果。为了更深入了解key,所以我们来看下key可用的数据类型。

(以下说的很清楚,就不翻译了)

Redis the most basic ; they are ,- of of 1 Gb.

You can set a key to a value using the SET and get it using GET. If you need to store a value, such as , you can save it on a and use INCR or DECR to or it.

Lists

Redis of Redis by order. You can think of a list as a chain, you can add a new link on the left (the head) or on the right (the tail); you can also add a link in the , but you have to broke link .

You can add to a list using the LPUSH and RPUSH (L for left and R for right), get an ( it) with LPOP and RPOP and get a range of ( any) with . You can also add an on a given point with LSET, but, as for the chain , this is much more than a push.

At the time of , the , few days ago, it will be in the . are a long that helps data in a more and neat way. a sort of key-value a key, so, for an hash "user" may some to wich , just like do in such as ruby, , etc…

Sets

Redis just like the , of ; in this case the are Redis . As you could argue, sets are from lists for two main : are and they are all , you can’t have two equal .

You can add an to a set with the SADD, an with SREM, get (and ) a with SPOP and the set , , and SDIFF .

sets

Redis quite like sets, with the that every set has an used to take this in order with .

You can work with sets as with ones, what is just the names of the : ZADD to add an , ZREM to it and so on. Two are ZINCR and , the is used to the score of an and the to get its score.

行程3: 表在哪里呢?

So, with Redis data is from what we’re used to with SQL , you haven’t a to query the , you just the keys in the . are -, you can’t use a set on a list, you’ll get an error. could be via -clior using one ofthe many your . In the , we’ll be about the way you’ll query Redis, we’ll just write the and you’ll issue them as you .

Let’s think about a SQL table where we could save users for some :

user1

pass1

Bob

Smith

user2

pass2

Mario

Rossi

存储数据

We want now to save the same data in Redis, so we have to our to fit this . Maybe, it’s to think about the from an - point of view; in the SQL , our would get users’ data a as the users’ id; in other words, we need a way to data . , we need to get user’s data with the sole of an . This could be if we use the user id as a part of the redis key, so the of the table would as

从零开始学redis__redis入门

user:1:

user1

user:1:

pass1

user:1:name

Bob

user:1:

Smith

user:2:

user2

user:2:

pass2

user:2:name

Mario

user:2:

Rossi

Well, given the id of an user, we could now get all its data the :id:,user:id:,user:id::id:.

用户登录

Our data are for a login , so we have to a way to gets the user’s id, given the ; in other words, we have to and user ids. This can be done if we add redis key to our data :user::id.

user:user1:id

user:user2:id

So, try to login to our , we can get his id its and so we’ll get all its data.

is the of our ids. In the SQL world we say, for , "id int key ", now we have to a to that we have a id for each user. As in the case, Redis has a : we can key "user:" and use it as a that we via the INCR we add a new user.

* FROM users;

The next is to get the user list. We could think that what we’ve done is to get this list: we could get the value "user:" and get users’ data from 0 to this value in one or more steps. Well, now let’s think about an user that has it’s (our next ), when we’ll all the from 0 to user: we’ll find its old id that now not has any data.

this is not a , we ’t waste our time to get the data of users that don’t exist , so we’ll a new key "user:list" of list (or set) type in wich we’ll add all new user ids, them when . We using a list this give us the to do a sort of "" (maybe you’re about "LIMIT"?) using the .

删除用户

The last is a of "data "; what would when we a user? We sould that to it. In other words, we all the :id:*,user::idand the id in "user:list".

行程4: 一个简单实例

As an of what we in this , let’s try to an that will work as a that let us group our books by . This will be more than the user’s table, but we’ll teach us how to Redis.

In our , we be able to our books, such as title, (s), topic(s), pages, price, ISBN and a . , we could have a book with more than one and that cover more than one topic at the same time (for , a book that is about ad ruby). , one could have more than one book and a topic could be with in more than one book. From what we’ve it that there’re amany-to- and books and and books.

The SQL case

First of all, let’s try to the SQL (with some data) that model this , in order to see how works the in the Redis idiom:

Books

Ruby: The ’ Guide

829

$26

The bible of the ruby

496

$42

An to

Dave

Chad

Andy

Hunt

Simon

Books about

ruby

Books about ruby

Books about

Books-

Books-

The Redis case

It be clear that the three Books, and are not a ; we’ve how to save this data in Redis in the leg (see the next image). The when the link Books- and Books- that the two many-to-many . How to them? Let’s think about only, the for is a twin (with a name) of the and we’ll use the same .

For every book we have to know the that to it and, in the same way, for every topic we have to know every book that deals with it. In other words, for each book we need a of the topic ids and for each topic a of the book ids. This is a where sets fit ! We’re going to two sets,book:id::id:books, in the we’ll save the topic ids that match the book and in the we’ll save the ids of the books that match this topic. For , to the data shown in the sub-leg, the book " " (book id 2) will have a :2: set type, with (1, 3); while the have a :1: (1, 2).

As said early, the same works for , so we could say thata SQL’smany-to- in Redis as two sets. This is , it give us the to an for free: we could find a book that more than one topic doing a set :id: to the we are on. So, for , the :1:books(books about ) :2:books(books about ruby) will the set(1), i.e. only the book " Ruby" with id 1.

A care be used we from our . As shown in the , for , have a to books and books have a to , so, what to ? Well, we :id:*keys, but doing this we have to all to the book id from the :id:books. , we the book id from :. If we want to a topic, we have to in a very way: :id:*key we have to all books by the topic we want to and the topic id from the :id:. The same apply for .

We like fun and , so to lear by doing a look tothe code of our . It is in ruby, using ’ redis-rb. Both the code and the look’n’feel are very rough, so feel free to make a pull if you have .

行程5: 回顾

我们的旅途结束了, 让我们回顾下整理下旅途中的行李和纪念品。

在我们行李里放满了Redis相关内容: 数据类型, 命令等,

纪念品是三类redis使用模式:

原文::

tags: redis

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了