Redis provides a wealth of commands that allow us to connect to the client for direct manipulation. Here's a quick introduction to some of the most commonly used commands, including strings, lists, collections, ordered collections, hash tables, and some other common commands.
"Basic Operations"
1. Add records: Typically used to set a string (string) type, or an integer type, or to overwrite its corresponding value if the key already exists. Shell code set name James
2. Get record: Get value by key. Shell code get Name
3. Increment/decrement: For integer types, you still use a string-like operation: And you can increment, decrement operations. Shell code set Age DECR age INCR
4. Add when key does not exist: Shell Code setnx name Nick
5. Set the expiration time: to avoid the continuous growth of data volume, such as the following command: Set the expiration time of 5s. Shell Code Setex name 5 Bill
The above command, equivalent to: Shell code set name Bill expire name 5
List actions can use lists (list) to simulate queues/stacks (stack).
1. Add element: UserList the list from the right to the string James. Shell Code Rpush userlist James
2. Remove element: Removes the first element from the left side of the userlist. Shell Code Lpop userlist
3. List range: For example, the following command gets all the elements from 0 (the left starting position) to-1 (the last position on the right) and contains the elements from the starting position. Shell Code Lrange userlist 0-1
4. Set the element: set the userlist position 1 to the new value, enclose the string containing the spaces in quotation marks. Shell code LSet userlist 1 "Nick Xu"
5. List Length: Shell code Llen userlist
6. Cropping list: After executing the following command, the list userlist contains only the contiguous elements of the original list from position 1 to 3. Shell Code LTrim userlist 1 3
The elements in the collection operations collection cannot be duplicated, and the collection is unordered.
1. Add elements: You can add multiple elements at the same time. Shell Code sadd Fruit Watermelon Sadd Fruit apple Pear
2. View all elements in the collection: Shell code smembers fruit
3. Removal element: Shell code Srem Fruit Apple
4. Collection size: Returns the number of elements contained in the collection. Shell Code SCard Fruit
5. Whether the collection contains elements: Shell code sismember Fruit Pear
6. The operation of the set: The following command returns the set of food and fruit, plus the intersection (sinter), the difference set (Sdiff) operation. Shell Code sunion Food Fruit
"Ordered set" sorted set
1. Add element: Sort according to the second argument. Shell Code zadd User James
2. Repeat add: There is the same value, the weight parameter is updated to 24. Shell Code zadd User James
3. Set Range: Find all elements from 0 to 1, and be ordered. Shell Code zrange User 0-1
"Hash Table operation"
1. Add element: Add the key to the hash table Shannxi, the value is Xian member. Shell Code