Redis Tutorial 3--redis key value Design

Source: Internet
Author: User
Tags redis tutorial

tag is especially common in Internet applications, first look at the following relational data tables:

Book table:

Id

Name

Author

1

The Ruby programming Language

Mark Pilgrim

2

Ruby on Rail

David Flanagan

3

Programming Erlang

Joe Armstrong

Tag table:

Tag_name

book_id

Ruby

1

Ruby

2

Web

2

Erlang

3

Now use Redis to save the data for these two tables:

Save the book's data:

redis 127.0.0.1:6379> incr book_id # with book_id this Key Save Book of the table ID, every time you want to get a new

#book_id self-increment with incr command

(integer) 1

#incr command to return 1, then use Key to be book:1 of the Hash to save a Book Object , Object properties are Hash of the Field

Redis 127.0.0.1:6379> hset book:1 name "The Ruby programming Language"

(integer) 1

Redis 127.0.0.1:6379> hset book:1 Author "Mark Pilgrim"

(integer) 1

redis 127.0.0.1:6379> hgetall book:1 # with Hgetall command to test a , return Hash all the properties and values

1) "Name"

2) "The Ruby programming Language"

3) "Author"

4) "Mark Pilgrim"

redis 127.0.0.1:6379> incr book_id # Create the Section 2 a Book Object , First incr a book_id Get new Book of the ID

(integer) 2

Redis 127.0.0.1:6379> hset book:2 name "Ruby on rail"

(integer) 1

Redis 127.0.0.1:6379> hset book:2 Author "David Flanagan"

(integer) 1

Redis 127.0.0.1:6379> Hgetall Book:2

1) "Name"

2) "Ruby on Rail"

3) "Author"

4) "David Flanagan"

Redis 127.0.0.1:6379> incr book_id

(integer) 3

Redis 127.0.0.1:6379> hset book:3 name "Programming Erlang"

(integer) 1

Redis 127.0.0.1:6379> hset book:3 Author "Joe Armstrong"

(integer) 1

Redis 127.0.0.1:6379> Hgetall Book:3

1) "Name"

2) "Programming Erlang"

3) "Author"

4) "Joe Armstrong"

Save the tag data, use the collection to store the data, because the collection can find the intersection, the set, the difference set:

Redis 127.0.0.1:6379> sadd Tag:ruby 1

(integer) 1

Redis 127.0.0.1:6379> Sadd Tag:ruby 2

(integer) 1

Redis 127.0.0.1:6379> Sadd Tag:web 2

(integer) 1

Redis 127.0.0.1:6379> Sadd Tag:erlang 3

(integer) 1

If you want to get a book that belongs to Ruby and is a Web:

Redis 127.0.0.1:6379> sinter Tag:ruby tag:web

1) "2"

If you want to get a book that belongs to Ruby but doesn't belong to the Web:

Redis 127.0.0.1:6379> Sdiff Tag:ruby tag:web

1) "1"

Collections of books that belong to Ruby and belong to the Web:

Redis 127.0.0.1:6379> sunion Tag:ruby tag:web

1) "1"

2) "2"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Redis Tutorial 3--redis key value Design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.