Redis Common Commands

Source: Internet
Author: User
Tags server port redis server

http://www.4wei.cn/archives/1002509

Redis Common Command Set
1) connection Operation command
Quit: Close connection (connection)
Auth: simple Password Authentication
Help cmd: View cmd assistance, for example: helper quit

2) Persistence
Save: Synchronize data to disk
Bgsave: Asynchronously saving data to disk
Lastsave: Returns the UNIX timestamp when the data was last successfully saved to disk
Shundown: Save data synchronously to disk, and then close the service

3) Remote Service control
Info: Provide information and statistics about the server
Monitor: Live dump of received requests
slaveof: Changing Replication policy settings
Config: Configure the Redis server at run time

4) command for value operation
exists (key): Verify that a key exists
Del (key): Delete a key
type (key): The type of the return value
keys (pattern) : Returns all keys that satisfy the given pattern
Randomkey: A randomly returned key space
Keyrename (Oldname, newname): Rename key
Dbsize: Returns the number of keys in the current database
Expire: Setting a Key's active time (s)
TTL: Get the active time of a key
Select index: Query by index
Move (key, Dbindex) : Move the key in the current database to the Dbindex database
FLUSHDB: Delete all keys in the currently selected database
Flushall: Delete all keys in all databases

5) string
Set (key, value): Assigns a value to the string named key in the database to the values
Get (Key): Returns a string that is named key in the database, value
Getset (key, Value): Assigns a string named key to the last value
Mget (key1, Key2,..., key N): Returns the value
Setnx (key, value) of multiple strings in the library: Adds a string, The name is key, value
Setex (key, Time, value): Adds a string to the library, sets the expiration Times
Mset (key n, value N): bulk Sets the value of multiple string
Msetnx ( Key N, value N): If all strings with the name key I do not exist
INCR (key): string 1 operation with name Key
Incrby (key, Integer) : String with Name Key added integer
DECR (key): string minus 1 operation with name Key
Decrby (Key, Integer): string with Name key reduced integer
Append (Key, value): String with the name key appended value
substr (key, start, end): Returns a substring of the value of string with the name key

6) List
Rpush (key, value): Adds an element of value to the list at the end of the name Key
Lpush (key, value): Adds an element of value to the list header named Key
Llen (Key): Returns the length of the list named key
Lrange (key, start, end): Returns the element between start and end in list named key
LTrim (key, start, end) : Intercepts the list
Lindex (key, index) named key: Returns the element with the index position in the list named key
LSet (key, index, value) : Assigns the element of the index position in the list named key
Lrem (key, Count, value): Removes the element with value in the list of Count keys
Lpop (key) : Returns and removes the first element of the list named key
Rpop (key): Returns and removes the tail element in the list named key
Blpop (Key1, Key2,... key N, timeout): The block version of the Lpop command.
Brpop (key1, Key2,... key N, timeout): The block version of Rpop.
Rpoplpush (Srckey, Dstkey): Returns and removes the tail element of the list named Srckey, and adds the element to the head of the list named Dstkey

7) Set
Sadd (Key, member): Adds an element to a set named Key member
Srem (Key, member): Removes the element in a set named Key member
Spop (key) : Randomly returns and deletes an element in the set named Key
Smove (Srckey, Dstkey, member): Move to the collection element
SCard (key): Returns the cardinality of the set named Key
Sismember (key, Member): Member is the element with the set named Key
Sinter (Key1, Key2,... key N): Intersection
Sinterstore (Dstkey, (keys)) : Seek the intersection and save the intersection to the collection of Dstkey
Sunion (Key1, keys): Set
Sunionstore (Dstkey, (keys)): Sets and saves the assembly to Dstkey's collection
Sdiff ( Key1, (keys)): Differential set
Sdiffstore (Dstkey, (keys)): Differential set and save difference set to Dstkey collection
Smembers (key): Returns all elements of a set named key
Srandmember (key): Randomly returns an element of set with the name key

8) Hash
Hset (Key, field, value): Adds an element to a hash named key field
Hget (Key, field): Returns the value of field corresponding to the hash named key
Hmget (Key, fields): Returns the value of field I in the hash named key
Hmset (Key, fields): Adds an element to a hash named key field
Hincrby (Key, field, integer): Adds an integer to the value of field in the hash named key
Hexists (Key, field): A field with the key field in the hash named key
Hdel (Key, field): Delete The field with the key field in the hash named key
Hlen (Key): Returns the number of elements in a hash with the name key
Hkeys (Key): Returns all keys in a hash with the name key
Hvals (Key): Returns the value corresponding to all keys in a hash with the name key
Hgetall (Key): Returns all keys (field) and their corresponding value in a hash with the name key

Redis Advanced Applications
1. Security
After setting up a client connection, any operation is required before specifying a password, an external user can make 150W access for a second, and the password modification settings redis.conf The Requirepass attribute in the password, of course, I give Primos
Then, if you want to do this, you can use it when you log in:
Sudo/opt/java/redis/bin/redis-cli-a Primos
Or, after entering, Auth Primos, and then you can manipulate it.

2, master-slave replication
I prepared two VMS for this operation, 192.168.15.128 and 192.168.15.133 IP respectively
Master-slave replication allows multiple slave servers to have the same database copy as master server
Configuration is configured on slave above slave
Slaveof 192.168.15.128 6379
Masterauth Primos
If there is no master-slave synchronization then check the firewall is not a problem, I use UFW, set the sudo ufw allow 6379 can be
At this time, you can view the specific situation through info

3. Transaction processing
Redis's support for transactions is relatively straightforward, and Redis can only guarantee that commands in one client-initiated transaction can be executed consecutively, without inserting other client commands in the middle. When a client issues a multi command in a connection, the connection goes into the context of a transaction, and the subsequent command is not executed immediately, but is placed first in a queue, and Redis executes all the commands in the queue sequentially when the EXEC command is executed.
Like one of my examples below
Set Age 100
Multi
Set age 10
Set age 20
Exec
Get age-the content should be 20
Multi
Set age 20
Set age 10
Exec
Get age-this time the content is 10, fully reflects the way in the order of the queue execution
Discard cancel all transactions, that is, transaction rollback
However, when the Redis transaction execution has the individual error, the transaction does not rollback, will carry on the error content execution, the wrong content abandons directly, the newest is the 2.6.7 also has this question
Optimistic lock
Watch key If there is a change to the key without watch, then the outdate transaction cannot be executed.

4. Persistence mechanism
Redis is an in-memory database that supports persistence
Snapshotting snapshot mode, default storage mode, default write to Dump.rdb binary file, Redis can be configured to automatically snapshot if more than M key is modified in n seconds
Append-only file aof mode, Redis will append each function to the file when using AOF, and will re-execute the saved write in the file when Redis restarts.
In memory.
5, publish the subscription message Sbusribe Publish operation, in fact, similar to Linux under the release of the message
6, the use of virtual memory
You can configure VM capabilities, save paths, max memory on-line, page size, page sizes, max worker threads
Temporarily modify IP address ifconfig eth0 192.168.15.129

REDIS-CLI parameters
USAGE:REDIS-CLI [OPTIONS] [cmd [arg [arg ...]]
-HServer hostname (default:127.0.0.1)
-PServer Port (default:6379)
-SServer socket (Overrides hostname and port)
-APassword to use when connecting to the server
-RExecute specified command N times
-I.When-r is used, waits seconds per command.
It is possible to specify Sub-second times like-i 0.1
-n database Number
-X Read last argument from STDIN
-d multi-bulk delimiter in for Raw formatting (default: \ n)
-C Enable cluster mode (follow-ask and-moved redirections)
--raw use raw formatting f or replies (default when STDOUT was not a TTY)
--latency Enter A special mode continuously sampling latency
--slave S Imulate a slave showing commands received from the Master
--pipe Transfer Raw Redis protocol from stdin to server
-- Bigkeys Sample Redis keys looking for big keys
--eval send an eval command us ing the Lua script at 
--help output This help and exit
--version output V Ersion and exit

Examples:
cat/etc/passwd | Redis-cli-x Set MYPASSWD
REDIS-CLI Get MYPASSWD
Redis-cli-r Lpush mylist X
Redis-cli-r 100-i 1 Info | grep Used_memory_human:
REDIS-CLI--eval Myscript.lua key1 key2, arg1 arg2 arg3
(Note:when using--eval the comma separates keys[] from argv[] items)

Common commands:
1) View keys number
Keys *//View all keys
Keys prefix_*//View all keys prefixed with "Prefix_"

2) Clear the database
FLUSHDB//Clears all keys for the current database
Flushall//Clears all keys for all databases

Redis Common Commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.