Today to learn the basic data types of Redis, there are five types of data in Redis, namely String,hash,list,set,zset. The following are described separately.
One, string (strings)
The string type is the most basic data type of redis, and it can store any form of string, including binary data. A string type that allows storage of data with a maximum capacity of 512M. Strings are the basis for the other 4 types of data.
1 string-related commands
1) Get/set command
key value # assigns a key to key # Gets the value of key
127.0.0.1:6379> SET str ' hello,world 'OK127.0.0.1:6379> GET str"Hello,world"
Returns a null value when the key does not exist.
2) incr increment command
A string type can store any form of string, and it can be automatically incremented with the INCR command when the stored string is in integer form.
key127.0.0.1:6379> INCR Bar (integer) 2127.0.0.1:6379> INCR Bar ( Integer) 3
When the key does not exist, the key is automatically created and incremented from 0.
2 String Practice
1) Article Traffic statistics
A common function of blogs is to count the number of articles visited, here we can use a key named Post:id:page.view for each article to record the number of visits to the article, each time someone accesses the INCR command for the key increment.
Naming rules for Keys: Object type: Object ID: Object property
For example, User:1:friends represents a friend list of users with ID 1, which increases legibility and maintainability.
2) generate self-increment ID
How do I mark a unique ID for each article? Auto_increment can be used in a relational database, but it can be implemented in Redis in a different pattern: using the Name object type (plural form) for each type of object: The key of Count to store the number of the current object type, incrementing the value without adding a new object.
3) Store article data
The article data includes the title, content, publish time and other fields, and we know that a string type can only store a string, then how to store the article data? Because string types can store binary data, they can be serialized using Messagepack, which is faster and takes up less space. Here is the pseudo-code:
#get new article ID$postid= INCR Posts:Count #serialize many fields of a blog post into a string$serialpost=Serialize($posttitle,$content,$author,$time)#Store the serialized article in a stringSET Post:$postid:d ATA =$serialpost#Read Id=42 's article data from Redis$serialpost= GET post:42:Data#Deserialize an article into individual fields$posttitle,$content,$author,$time=unserialize($serialpost)#get the number of articles accessed$count= INCR Post:42:page.view
3 string-related extension commands
1) Add the specified integer
Key increment127.0.0.1:6379> GET goo"2" 127.0.0.1:6379> Incrby Goo 3(integer) 5
2) decrease the specified integer
Keykey value127.0.0.1:6379> GET goo"5" 127.0.0.1:6379> DECR Goo (integer) 4127.0.0.1:6379> decrby Goo 2(integer) 2
3) Append value to tail
APPEND key value, if the key does not exist, sets the value to the key, and the return value is the appended length.
127.0.0.1:6379> GET str"Hello,world" 127.0.0.1:6379> APPEND str ', Zhao '(integer) 16127.0.0.1:6379> GET str"Hello,world,zhao"
4) Get the string length
STRLEN Key
STRLEN str (integer) 16
5) Set/Get multiple key values at the same time
Mget/mset key1 Key2 ....
127.0.0.1:6379> MGET str goo Bar1) "Hello,world,zhao" 2) "2" 3 "3"
First, hash (hash type)
Redis data types