Redis Usage Summary

Source: Internet
Author: User

I. Using REDIS cache data to create data consistent with MySQL

Let's start with the results of unifying the Redis data in MySQL tables and see what the differences between data structure design and Redis are in MySQL.

1. There are two data sheets as follows

CREATE TABLE ' user ' (

' id ' int (ten) is not NULL auto_increment,

' username ' varchar (not NULL COMMENT ' username '),

' Password ' varchar (+) not NULL COMMENT ' password ',

PRIMARY KEY (' id ')

) Engine=innodb auto_increment=3 DEFAULT charset=utf8 comment= ' user Information table ';

CREATE TABLE ' Archives ' (

' id ' int (ten) is not NULL auto_increment,

' userid ' int (ten) not NULL COMMENT ' user ID ',

' username ' varchar (a) Not NULL COMMENT ' name of the user who published the microblog ',

' Post_time ' datetime not NULL COMMENT ' release Time ',

' Content ' varchar ($) Not NULL COMMENT ' Weibo content ',

PRIMARY KEY (' id ')

Engine=innodb auto_increment=3 DEFAULT Charset=utf8 comment= ' user-published microblogging information Form ';

2.mysql table data is as follows

The user table data is as follows

Mysql> select * from user;

+----+----------+----------+

| ID | Username | password |

+----+----------+----------+

| 1 | Dongzi | 123456 |

| 2 | Lidong | 123456 |

+----+----------+----------+

Archives table data is as follows

Mysql> SELECT * from archives;

+----+--------+----------+---------------------+--------------------------------------------+

| ID | UserID | Username | Post_time | Content |

+----+--------+----------+---------------------+--------------------------------------------+

|      1 | 1 | Dongzi | 2016-06-22 16:00:40 | I am the user of the East son, my first Weibo |

|      2 | 2 | Lidong | 2016-06-22 16:10:40 | I am user Lidong, my first Weibo |

|      3 | 2 | Lidong | 2016-06-22 17:05:40 | I am user Lidong, my second Weibo |

+----+--------+----------+---------------------+--------------------------------------------+

3. Use Redis to add user tables and archives data

Increase the first user in the user table

INCR Global:user--Let the user table ID increment, at this time the ID is 1, in order to implement the MySQL Auto_increment

Set User:userid:1:username Dongzi

Set User:userid:1:password 123456

Set User:username:dongzi:userid 1--sets this key to reverse the user ID by using the username

?

Add a second user in the user table

INCR Global:user--now returns a value of 2

Set User:userid:2:username Lidong

Set User:userid:2:password 123456

Set User:username:lidong:userid 2--sets this key to reverse the user ID by using the username

Post the first Weibo

INCR global:archives? ?--record the ID of the Archives table

Hset archives:id:1 UserID 1

Hset archives:id:1 username Dongzi

Hset archives:id:1 post_time ' 2016-06-22 16:00:40 '

Hset archives:id:1 content ' I am the user of the East son, my first Weibo '

Lpush archives:userid:1 1--record all the Weibo IDs posted by user ID

Post a second Weibo blog

INCR global:archives

Hset archives:id:2 UserID 2

Hset archives:id:2 username Lidong

Hset archives:id:2 post_time ' 2016-06-22 16:14:34 '

Hset archives:id:2 content ' I am user Lidong, my first Weibo '

Lpush Archives:userid:2 2

Post Third Micro Blog

INCR global:archives

Hset Archives:id:3 UserID 2

Hset archives:id:3 username Lidong

Hset archives:id:3 post_time ' 2016-06-22 17:05:40 '

Hset archives:id:3 content ' I am user Lidong, my second Weibo '

Lpush Archives:userid:2 3


Eventually we used Redis to get the same data as MySQL, but there are some differences between the corresponding relationships in Redis.

Second, according to the specific business logic, reasonable set failure time

Three, high frequency, low-frequency data to be separated

High-frequency data is stored in the Redis cache and low-frequency data is not stored in the Redis cache

Iv. data types for reasonable use of Redis

For example:

1. Use the hash type:

Hset archives:id:1 UserID 1

Hset archives:id:1 username Dongzi

We use the hash type to store, can reach a similar data in MySQL information, so that each message in Redis has only one key, can be a large number of key reduction

2. Using the list type
Lpush Archives:userid:2 3
Create a key,list type to represent the Weibo ID of all publications with UserID 2, so that you can bind the Weibo ID to the user ID

V. Use string formatting as much as possible
Business logic satisfies, uses string format as much as possible, and reads easily when reading

Vi. you can use INCR to implement the self-increment ID to achieve mysql-like auto_increment function

For example:

INCR Global:user

Each time the incr is added, the new data is inserted with this self-increment ID value

Seven, the format of the reasonable set key

Advantages: The advantage of this is to easily reflect the relationship between the data, hierarchical, convenient management.

For example:

Set User:userid:2:username Lidong
The table name is placed first, the user ID is placed in the second position, the field that needs to be queried is placed in the third place, and then a colon concatenation is used to highlight the logical relationship, which is based on the table above, and the specific business logic is implemented.

VIII. create a "redundant" key to reverse-check the data
For example:
Set User:username:lidong:userid 2
Set this key is to use the user name to reverse the user ID, we know that the user name Lidong can know the user ID is 2, the corresponding can get the user ID 2 of all information

Redis Usage Summary

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.