Redis key-value database nosql data modeling (1) ------ How to store single table data

                                                                Author: QQ 14588019 WonderfulLife

Relational database user data table users (create a unique index on the username field):

id    username               password    name     surname
1     [email protected]        pass1          Bob       Smith
2     [email protected]    pass2          Mario     Rossi

3 [email protected] pass3 Tom Smith
Redis Data Storage Form 1 Key Name: String (GET&SET):
Key Value

user:1:username                  [email protected]
user:1:password                   pass1
user:1:name                         Bob
user:1:surname                    Smith
user:2:username                  [email protected]
user:2:password                   pass2
user:2:name                         Mario
user:2:surname                    Rossi
user:3:username                  [email protected]
user:3:password                   pass3
user:3:name                         Tom
user:3:surname                    Smith
user:[email protected]:id            1
user:[email protected]:id      2
user:[email protected]:id             3

Redis data storage form 2 Key name: json string (GET&SET):

Key            Value

users:1       {\"id\":1,\"username\":\"[email protected]\",\"password\":\"pass1\",\"name\":\"Bob\",\"surname\":\"Smith\"}
users:2       {\"id\":2,\"username\":\"[email protected]\",\"password\":\"pass2\",\"name\":\"Mario\",\"surname\":\"Rossi\"}
users:3       {\"id\":3,\"username\":\"[email protected]\",\"password\":\"pass3\",\"name\":\"Tom\",\"surname\":\"Smith\"}

================================================================

Redis data storage form 1 Key name: string (GET&SET):

If we know any $id, then we can read a complete user record through the following 4 keys, and of course we can also read out several field values ​​you need.

user:$id:username,   user:$id:password,   user:$id:name,   user:$id:surname

Group 1 directives add the following record:
id    username  	password    name     surname
1     [email protected]      pass1       Bob      Smith

INCR user:id
WATCH user:id
GET user:id
MULTI
SETNX user:[email protected]:id 1
SET user:1:username "[email protected]"
SET user:1:password "pass1"
SET user:1:name "Bob"
SET user:1:surname "Smith"
LPUSH user:list user:1:username
EXEC


Group 2 instructions add the following record:
id    username  	password    name     surname
2 [email protected] pass2 Mario Rossi

INCR user:id
WATCH user:id
GET user:id
MULTI
SETNX user: [email protected]: id 2
SET user:2:username "[email protected]"
SET user:2:password "pass2"
SET user:2:name "Mario"
SET user:2:surname "Rossi"
LPUSH user:list user:2:username
EXEC


The third group of instructions adds the following record:
id    username  	password    name     surname
3     [email protected]       pass3       Tom      Smith

INCR user:id
WATCH user:id
GET user:id
MULTI
SETNX user:[email protected]:id 3
MSET user:3:username "[email protected]" user:3:password "pass3" user:3:name "Tom" user:3:surname "Smith"
LPUSH user:list user:3:username
EXEC

Explanation: The first command in the above three sets of commands is to use INCR user:id to generate the primary key id value of the record. This value can be used for paging or searching for a record set containing a continuous range of primary keys by primary key range. At this time, the user The :id function is equivalent to implementing a users:count key name to store the total number of records

But in this example, I use the queue uses:list to store the primary key id and username information of the record, and later I will demonstrate how to use the queue to paginate the query

SETNX user:[email protected]:id 1
SETNX user:[email protected]:id 2
SETNX user:[email protected]:id 3
Why do you need to execute the above 3 commands and what are they used for? Their function is to find the primary key of the record through Email. Email is the only index, and it has a one-to-one relationship with the primary key id. The method finds other field information of this record;
the role of WATCH user:id Monitor whether the value of a key has been changed by other programs. If the key is modified between WATCH and EXEC, the execution of the code block of this MULTI/EXEC transaction will fail (return false), and the MULTI/EXEC transaction executes a series of commands with atomic guarantees

The hands-on steps are as follows:


127.0.0.1:6379> MGET user:1:username user:1:password user:1:name user:1:surname
1) "[email protected]"
2) "pass1"
3) "Bob"
4) "Smith"
127.0.0.1:6379> MGET user:2:username user:2:password user:2:name user:2:surname
1) "[email protected]"
2) "pass2"
3) "Mario"
4) "Rossi"
127.0.0.1:6379> MGET user:3:username user:3:password user:3:name user:3:surname
1) "[email protected]"
2) "pass3"
3) "Tom"
4) "Smith"
127.0.0.1:6379>
If some/some users are deleted from the system, it is wrong to use GET user:id return value = total records to paginate

For example: to delete the record with id = 2, we need to delete all key-value pairs with the primary key id = 2 to ensure the referential integrity of the data

127.0.0.1:6379> GET user:2:username
"[email protected]"
127.0.0.1:6379>
127.0.0.1:6379> MGET user:[email protected]:id user:2:username user:2:password user:2:name user:2:surname
1) "2"
2) "[email protected]"
3) "pass2"
4) "Mario"
5) "Rossi"
127.0.0.1:6379> DEL user:[email protected]:id user:2:username user:2:password user:2:name user:2:surname
(integer) 5
127.0.0.1:6379> LRANGE user:list 0 -1
1) "user:3:username"
2) "user:2:username"
3) "user:1:username"
127.0.0.1:6379> LREM user:list 1 user:2:username
(integer) 1
127.0.0.1:6379> LRANGE user:list 0 -1
1) "user:3:username"
2) "user:1:username"
127.0.0.1:6379>


If you use the user:list queue (Data of type List) to store the primary key string of the record for paging:

127.0.0.1:6379> LLEN user:list
(integer) 2
127.0.0.1:6379> LRANGE user:list 0  20
1) "user:3:username"
2) "user:1:username"
127.0.0.1:6379> LRANGE user:list 21 40
(empty list or set)
127.0.0.1:6379> LRANGE user:list 41 60
(empty list or set)
127.0.0.1:6379>

Explanation:
LLEN user:list # Returns the total number of records In the phpredis extension, the $redis->lsize("user:list") method will return the total number of records
LRANGE user:list 0 20 # This is the primary key of 20 records on page 1
LRANGE user:list 21 40 # This is the primary key of 20 records on page 2
LRANGE user:list 41 60 # This is the primary key of 20 records on page 3

If you use the SortedSet type to store the primary key key string of the record for paging:

127.0.0.1:6379> FLUSHDB # Clear the database before practice

Group 1 directives add the following record:  
id    username      password    name     surname  
1     [email protected]      pass1       Bob      Smith  
  
INCR user:id  
WATCH user:id  
GET user:id  
MULTI  
SETNX user:[email protected]:id 1  
SET user:1:username "[email protected]"  
SET user:1:password "pass1"  
SET user:1:name "Bob"  
SET user:1:surname "Smith"  
ZADD user:zset 1 user:1:username  
EXEC  
  
Group 2 instructions add the following record:  
id    username      password    name     surname  
2 [email protected] pass2 Mario Rossi  
  
INCR user:id  
WATCH user:id  
GET user:id  
MULTI  
SETNX user: [email protected]: id 2  
SET user:2:username "[email protected]"  
SET user:2:password "pass2"  
SET user:2:name "Mario"  
SET user:2:surname "Rossi"  
ZADD user:zset 2 user:2:username
EXEC  
  
The third group of instructions adds the following record:  
id    username      password    name     surname  
3     [email protected]       pass3       Tom      Smith  
  
INCR user:id  
WATCH user:id  
GET user:id  
MULTI  
SETNX user:[email protected]:id 3  
MSET user:3:username "[email protected]" user:3:password "pass3" user:3:name "Tom" user:3:surname "Smith"  
ZADD user:zset 3 user:3:username  
EXEC  

127.0.0.1:6379> ZCARD user:zset # Return the total number of records
127.0.0.1:6379> ZRANGE user:zset 0 20 # This is the primary key of 20 records on page 1
1) "user:1:username"
2) "user:2:username"
3) "user:3:username"
	... ...
20) "user:20:username"
127.0.0.1:6379> ZRANGE user:zset 21 40 # This is the primary key of 20 records on page 2
127.0.0.1:6379> ZRANGE user:zset 41 60 # This is the primary key of 20 records on page 3

We can also read the primary keys required for pagination in reverse order:
127.0.0.1:6379> ZREVRANGE user:zset 0 20 # This is the primary key of 20 records on page 1
1) "user:60:username"
2) "user:59:username"
3) "user:58:username"
	... ...
20) "user:51:username"
127.0.0.1:6379> ZREVRANGE user:zset 21 40 # This is the primary key of 20 records on page 2
127.0.0.1:6379> ZREVRANGE user:zset 41 60 # This is the primary key of 20 records on page 3

If some/some users are deleted from Redis, use GET user:2:username to return the Email value corresponding to the primary key
For example: to delete the record with id = 2, we need to delete all key-value pairs related to the primary key id = 2 to ensure the referential integrity of the data
127.0.0.1:6379> GET user:2:username
"[email protected]"
127.0.0.1:6379> DEL user:[email protected]:id user:2:username user:2:password user:2:name user:2:surname
(integer) 5
127.0.0.1:6379> ZREM user:zset user:2:username # The first parameter is the key of the sorted set, and the second parameter is the key of the deleted record
(integer) 1
127.0.0.1:6379> ZRANGE user:zset 0  20  WITHSCORES
1) "user:1:username"
2) "1"
3) "user:3:username"
4) "3"
127.0.0.1:6379> ZCARD user:zset
(integer) 2
127.0.0.1:6379>

If we use the hash set type HashSet to store 1 entire record:

127.0.0.1:6379> FLUSHALL # Clear all data stored in Redis
Group 1 directives add the following record:
id    username  	password    name     surname
1     [email protected]      pass1       Bob      Smith

MULTI
SETNX user:[email protected]:id 1
HMSET user:1:hset id  1  username "[email protected]"  password "pass1"  name "Bob"  surname "Smith"
LPUSH user:list user:1:hset
EXEC

Group 2 instructions add the following record:
id    username  	password    name     surname
2 [email protected] pass2 Mario Rossi

MULTI
SETNX user: [email protected]: id 2
HMSET users:2:hset id  2  username "[email protected]"  password "pass2"  name "Mario"  surname "Rossi"
LPUSH user:list user:2:hset
EXEC


The third group of instructions adds the following record:
id    username  	password    name     surname
3     [email protected]       pass3       Tom      Smith

MULTI
SETNX user:[email protected]:id 3
HMSET users:3:hset id  3  username "[email protected]"  password "pass3"  name "Tom"  surname "Smith"
LPUSH user:list user:3:hset
EXEC

127.0.0.1:6379> LRANGE user:list 0 -1 # View all primary key information in the queue user:list
1) "user:3:hset"
2) "user:2:hset"
3) "user:1:hset"

If you use the queue List type to store the primary key user:list string of records for paging:
127.0.0.1:6379> LLEN user:list # return the total number of records
127.0.0.1:6379> LRANGE user:list 0 20 # This is the primary key of 20 records on page 1
127.0.0.1:6379> LRANGE user:list 21 40 # This is the primary key of 20 records on page 2
127.0.0.1:6379> LRANGE user:list 41 60 # This is the primary key of 20 records on page 3

If we use a queue (list type) to store all the records:

127.0.0.1:6379> FLUSHALL # Clear all data stored in Redis
127.0.0.1:6379> LPUSH users:list "{\"id\":1,\"username\":\"[email protected]\",\"password\":\"pass1\",\"name\":\"Bob\",\"surname\":\"Smith\"}"
127.0.0.1:6379> LPUSH users:list "{\"id\":2,\"username\":\"[email protected]\",\"password\":\"pass2\",\"name\":\"Mario\",\"surname\":\"Rossi\"}"
127.0.0.1:6379> LPUSH users:list "{\"id\":3,\"username\":\"[email protected]\",\"password\":\"pass3\",\"name\":\"Tom\",\"surname\":\"Smith\"}"

If you use the queue List type to store json records for pagination:
127.0.0.1:6379> LLEN users:list # Returns the total number of records  
127.0.0.1:6379> LRANGE users:list 0 20 # This is the primary key of 20 records on page 1
1) "{\"id\":3,\"username\":\"[email protected]\",\"password\":\"pass3\",\"name\":\"Tom\",\"surname\":\"Smith\"}"
2) "{\"id\":2,\"username\":\"[email protected]\",\"password\":\"pass2\",\"name\":\"Mario\",\"surname\":\"Rossi\"}"
3) "{\"id\":1,\"username\":\"[email protected]\",\"password\":\"pass1\",\"name\":\"Bob\",\"surname\":\"Smith\"}"
127.0.0.1:6379> LRANGE users:list 21 40 # This is the primary key of 20 records on page 2
(empty list or set)
127.0.0.1:6379> LRANGE users:list 41 60 # This is the primary key of 20 records on page 3
(empty list or set)
127.0.0.1:6379>

Redis data storage form two key name: json string (GET&SET):

Group 1 directives add the following record:  
id    username      password    name     surname  
1     [email protected]      pass1       Bob      Smith  

SET user:id  1    
WATCH user:id    
GET user:id    
MULTI    
SETNX user:[email protected]:id 1    
SET user:1  "{\"id\":1,\"username\":\"[email protected]\",\"password\":\"pass1\",\"name\":\"Bob\",\"surname\":\"Smith\"}"
ZADD user:zset 1 user:1:username    
EXEC

Group 2 instructions add the following record:  
id    username      password    name     surname  
2 [email protected] pass2 Mario Rossi  
  
SET user:id  2
WATCH user:id  
GET user:id  
MULTI  
SETNX user: [email protected]: id 2  
SET user:2  "{\"id\":2,\"username\":\"[email protected]\",\"password\":\"pass2\",\"name\":\"Mario\",\"surname\":\"Rossi\"}"  
ZADD user:zset 2 user:2:username  
EXEC  
  
  
The third group of instructions adds the following record:  
id    username      password    name     surname  
3     [email protected]       pass3       Tom      Smith  
  
SET user:id  3
WATCH user:id  
GET user:id  
MULTI  
SETNX user:[email protected]:id 3  
SET user:3  "{\"id\":3,\"username\":\"[email protected]\",\"password\":\"pass3\",\"name\":\"Tom\",\"surname\":\"Smith\"}"
ZADD user:zset 3 user:3:username
EXEC  

127.0.0.1:6379> ZCARD user:zset # Return the total number of records  
127.0.0.1:6379> ZRANGE user:zset 0 20 # This is the primary key of 20 records on page 1  
1) "user:1:username"  
2) "user:2:username"  
3) "user:3:username"  
    ... ...   
20) "user:20:username"  
127.0.0.1:6379> ZRANGE user:zset 21 40 # This is the primary key of 20 records on page 2  
127.0.0.1:6379> ZRANGE user:zset 41 60 # This is the primary key of 20 records on page 3  
  
We can also read the primary keys required for pagination in reverse order:  
127.0.0.1:6379> ZREVRANGE user:zset 0 20 # This is the primary key of 20 records on page 1  
1) "user:60:username"  
2) "user:59:username"  
3) "user:58:username"  
    ... ...   
20) "user:51:username"  
127.0.0.1:6379> ZREVRANGE user:zset 21 40 # This is the primary key of 20 records on page 2  
127.0.0.1:6379> ZREVRANGE user:zset 41 60 # This is the primary key of 20 records on page 3
After this article is explained, it may not be published commercially without permission!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325865620&siteId=291194637