Redis source port number 6379 of
Redis port number is 6379, but the port numbers are not randomly selected, from "MERZ", the word corresponding to the digital phone which is in 6379. "MERZ" the friend circle among Redis representatives of Antirez silly meaning.
data structure
Redis can only be a string of key, value can be a String, Hash, List, Sorted Set (Zset).
String
Redis string is a dynamic string (SDS Simple Dynamic String), an internal structure somewhat similar to the java ArrayList, are frequently taken to reduce the pre-allocated memory expansion. FIG len is the actual length of the string, capacity space (capacity arrays) pre-assigned. When you create a string, as long as len and capacity, using an array of bytes stored content.
struct SDS<T> {
T capacity; // 数组容量
T len; // 数组长度
byte flags; // 特殊标识位
byte[] content; // 数组内容
}
- If less than 1M, are doubling expansion capacity
- If the more than 1M, 1M each expansion
- The maximum capacity of the string is 512M
Some of the basic operations String
- Ordinary get set
127.0.0.1:6379> set name amber
OK
127.0.0.1:6379> get name
"amber"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
- Batch mset, mget
127.0.0.1:6379> set name amber
OK
127.0.0.1:6379> set name2 nick
OK
127.0.0.1:6379> mget name name2
1) "amber"
2) "nick"
127.0.0.1:6379> mset name3 wade name4 hellen
OK
127.0.0.1:6379> mget name name2 name3 name4
1) "amber"
2) "nick"
3) "wade"
4) "hellen"
127.0.0.1:6379>
- Set an expiration time
- The first expire
127.0.0.1:6379> set name amber
OK
127.0.0.1:6379> expire name 5
(integer) 1
127.0.0.1:6379> get name
"amber"
//等待5s
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
- Use SETEX
SETEX name time value
127.0.0.1:6379> setex name 5 amber
OK
127.0.0.1:6379> get name
"amber"
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
- Increment decrement
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> incr age
(integer) 19
127.0.0.1:6379> incrby age 5
(integer) 24
127.0.0.1:6379> incrby age -5
(integer) 19
127.0.0.1:6379> decr age
(integer) 18
127.0.0.1:6379>
List
Redis of the list structure a bit like in Java LinkedList, but in fact, real estate is not just a simple linkedlist, the bottom is quicklist (too deep after a wait of learning ...)
Feature
Very efficient insertion and deletion of list, the time complexity is O (1), but the positioning of the index is very slow, i.e., O (n)
operating
- Left and right out (queue)
127.0.0.1:6379> lpush names amber nick wade
(integer) 3
127.0.0.1:6379> rpop names
"amber"
127.0.0.1:6379> rpop names
"nick"
127.0.0.1:6379> rpop names
"wade"
127.0.0.1:6379> rpop names
(nil)
127.0.0.1:6379>
Of course, you can also Sakon left out (stack), you can test it yourself.
- Index Operations
- lindex equivalent of java get (int index) according to the index value, but because you want to traverse the list, if the data is large, resulting in increased overhead
- ltrim key index1 index2 and index1 between data retention index2
127.0.0.1:6379> lpush names amber nick wade
(integer) 3
127.0.0.1:6379> lindex names 0
"wade"
127.0.0.1:6379> lindex names 1
"nick"
127.0.0.1:6379> lindex names 2
"amber"
127.0.0.1:6379> ltrim names 0 1
127.0.0.1:6379> lindex names 0
"wade"
127.0.0.1:6379> lindex names 1
"nick"
127.0.0.1:6379> lindex names 2
(nil)
127.0.0.1:6379>
hash (hash)
Redis is similar to a hash of java HashMap
Feature
Redis of java in the HashMap Hash different from when rehash.
When redis rehash will be two structures while preserving the old and new, and slowly move the old data to the new data in a subsequent timing among the tasks.
operating
127.0.0.1:6379> hmset person name amber age 18
OK
127.0.0.1:6379> hgetall person
1) "name"
2) "amber"
3) "age"
4) "18"
127.0.0.1:6379> hget person name
"amber"
127.0.0.1:6379> hset person gender 1
(integer) 1
127.0.0.1:6379> hgetall person
1) "name"
2) "amber"
3) "age"
4) "18"
5) "gender"
6) "1"
set
Redis is set in the equivalent of java HashSet, equivalent to achieve an internal dictionary
Feature
The only value
operating
127.0.0.1:6379> sadd names amber
(integer) 1
127.0.0.1:6379> sadd names amber
(integer) 0
127.0.0.1:6379> sadd names nick wade
(integer) 2
127.0.0.1:6379> smembers names
1) "amber"
2) "wade"
3) "nick"
zset(sorted set)
The equivalent binding Redis zset sorted set of java and the HashMap. Set on the basis of the score may also be imparted to the value (weight for weight)
Feature
zset because there is need to sort score, but the ordinary list to find sales is too low. Therefore zst using hierarchical system. Somewhat similar countries -> provincial -> City -> xxx. The bottom of the township is willing to Dili is our level L0, and all the elements are connected in series, each selected city is located on several elements L2, L2 elements every few levels the same way you select province located L3 level. When we insert a new node time, just from the top level to start the search to locate the corresponding position on the line. Is not it a bit to find like-half of the array.
operating
In fact, there are some operations, but here is not to show
127.0.0.1:6379> zadd names 2 amber
(integer) 1
127.0.0.1:6379> zadd names 3 wade
(integer) 1
127.0.0.1:6379> zadd names 1 nick
(integer) 1
127.0.0.1:6379> zrange names 0 2
1) "nick"
2) "amber"
3) "wade"
127.0.0.1:6379>
Data structure knowledge to expand
- All of the data structures can be set time redis
1. 设置时间
expire key 时间
2. 查看时间
ttl key