This article by larrylgq prepared, reproduced please note the Source: http://blog.csdn.net/larrylgq/article/details/7399769
Author: Lu guiqiang
Email: larry.lv.word@gmail.com
Strings type
The string type is binary secure.
Redis strings can contain any data, such as JPG images or serialized objects. From the perspective of internal implementation, the string can be regarded as a byte array, and the maximum value is 1 GB.
Struct sdshdr {
Long Len; // The length of the Buf
Long free; // number of available bytes of the Buf
Char Buf []; // actual string content
};
1: Set
Set the value corresponding to the key (string type)
> Set name Larry
OK
2: setnx
That is, set not exist. If the key already exists, 0 is returned.
> Set name Larry
OK
> Setnx name Larry
(Integer) 0
3: setex
Store key-value pairs and set the validity period
> Setex name 10 Larry
OK
> Get name
"Larry"
10 seconds later...
> Get name
(Nil)
4:Setrange
If yes, replace it with \ x00. If no, replace the missing character with \ x00. A number indicates the subscript to be replaced.
> Get name
"Larry"
> Setrange name 0 LV
(Integer) 5
> Get name
"Lvrry"
> Setrange name 10 LV
"Lvrry \ x00 \ x00 \ x00 \ x00 \ x00lv"
5:Mset
Set multiple key values each time
> Mset key1 value1 key2 value2
OK
6:Msetnx
If multiple key values are set at a time, they are atomic and both succeed or fail. If the operation fails, 0 is returned, and all operations are rolled back.
7:Get
Returns a value based on the key if no value exists (nil)
> Get asdasds
(Nil)
8:GetSet
Get the old value and set a new value. If no value exists, return (nil)
> Get name
"Larry"
> GetSet name LV
"Larry"
> Get name
"LV"
> GetSet dsadasd LV
(Nil)
9:Getrange
Obtain the value based on the specified subscript. A negative number indicates that the value starts from the right.
> Getrange 0 1 Name
"La"
10:Mget
Multiple values are returned. If no value exists, return (nil)
> Get name sdadasd
1) "Larry"
2) (nil)
11:Incr
Add operation. If the character is not int type, an error is returned (error) Err value is not an integer or out of range
> Set age 20
OK
Incr age
(Integer) 21
12:Incrby
Add operation
> Get age
"21"
> Incrby age 5
(Integer) 26
13:Decr
Subtraction operation
14:Decrby
Subtraction
15:Append
Append operation, returns the string length
> Append name LV
(Integer) 15
> Get name
"Larrylv"
16:Strlen
Returns the length of a value.
Hashes type
The new hash object is stored by zipmap (also known as small hash). When the condition exceeds the following configuration
Hash-max-zipmap-entries 64 // a maximum of 64 fields can be entered.
Hash-max-zipmap-value 512 // The maximum value is 512 bytes.
Redis automatically replaces zipmap with normal hash implementation.
1:Hset
> Hset testhash field1 Test
(Integer) 1
2:Hsetnx
Hsetnx testhash field1 Test
(Integer) 1
> Hsetnx testhash field1 Test
(Integer) 0
3:Hmset
> Hmset testhash field1 A field2 B
OK
4:Hget
> Hget testhash field1
""
5:Hmet
> Hmet testhash field1 field2 field3
1) ""
2) "B"
3) (nil)
6:Hincrby
Similar to incrby
7:Hexists
> Hexists testhash field10
(Integer) 0
8:Hlen
Number of returned hash Fields
> Len testhash
(Integer) 2
9:Hdel
Number of successfully deleted items returned after deletion
> Hdel testhash field1 field2
(Integer) 2
10:Hkeys
Returns all keys of the current hash.
11:Hvals
Returns all values of the current hash.
12:Hgetall
Returns the entire hash.
Lists: Each element has a front and back pointer, so the space occupied by the element is: element size + 24 bytes + 16 bytes
The list type is a two-way linked list with the string type as the child element. The maximum length is (2 to the power of 32 ).
You can use lpush, lpop, rpush, and rpop to add and delete elements from the head and tail of the linked list.
In addition, to prevent pop operations on the poll list from being blocked
1:Lpush
Add a string element to the list header and return the list length.
> Lpush testlist "Larry"
(Integer) 1
2:Rpush
Add the string element at the end of the list
3:Linsert
Add the string element to a specified position. If the string element fails to be added, return (integer)-1.
Linsert testlist before "LV"
(Integer) 2
4:Lset
Set the value of the specified subscript. An error (error) Err Index out of range is returned when the subscript is out of the Range.
> Lset testlist 0 OK
OK
5:Lrem
Delete the values of Count specified values in order. Values of Count = 0 are all deleted,> 0 is deleted in positive order, and <0 is deleted in reverse order.
> Lrem testlist 2 "LV"
(Integer) 2
6:Ltrim
Only data within the specified range is retained.
> Ltrim testlist 1-1 // indicates that the first element is deleted.
OK
7:Lpop
Delete and return header data
8:Rpop
Delete and return the end data
9:Rpoplpush
Remove the tail data of the first list and add it to the header of the second list.
> Rpoplpush testlist testlist2
10: lindex
Returns the element at the index position.
11:Llen
Returns the length of the list.
Set
Implement through hash table
1:Sadd
Add Element
> Sadd testset "LV"
(Integer) 1
2:Srem
Delete Element
3:Spop
Randomly Delete and return an element
4:Sdiff
Returns the difference set of two sets, which is unique to the current set.
5:Sdiffstore
Returns the difference set of two sets and stores the result as the third set.
6:Sinter
Returns the intersection of two sets.
7:Sinterstore
Returns the intersection of two sets and saves them as a set.
8:Sunion
Returns the union of two sets.
9:Sunionstore
Returns the union of two sets and saves them as the third set.
10;Smove
Move the element of the first set to the second set
11:Scard
Returns the number of set elements.
12:Sismember
Returns the number of special values in a set.
13:Srandmember
Returns an element randomly, but does not delete it.
Sorted Sets
1: zadd
Add Element
> Zadd testzset 1 "one"
(Integer) 1
2:Zrem
Delete Element
3:Zincrby
If this element exists, add the score of this element.
4:Zrank
Returns the ranking of the current element, from small to large.
5:Zrevrank
Returns the ranking of the current element, from small to large.
6:Zrevrange
Returns the elements of a given range in ascending order of score.
7:Zrangebyscore
Returns the element of a score in a given range.
8:Zcount
Returns the number of elements in the given score range.
9:Zcard
Returns the number of elements in a collection.
10:Zscore
Returns the score corresponding to the element.
11:Zremrangebyrank
Deletes the element of the score ranking in the specified range.
12:Zremrangebyscore
Deletes the element of a score in a given range.
In addition, redis supports powerful sort... by... get... operations:
>./Redis-cli sadd uid 001
1
>./Redis-cli sadd uid 002
1
>./Redis-cli set UID: sort: 001 98 # uid score
OK
>./Redis-cli set UID: sort: 002 97
OK
>./Redis-cli set title: 001 "Iam 001" # uid content
OK
>./Redis-cli set title: 002 "iam002"
OK
>. /Redis-cli sort uid by uid: sort: * get title: * # obtain the ID matching the UID: Sort field from the friends list and sort it according to the sorted order, obtain information from uid table with key
1. Iam 001
2. iam002