In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what Redis commands are commonly used, and the editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Redis common command set
1) Connect operation command
Quit: close connection (connection)
Auth: simple password authentication
Help cmd: view cmd help, for example: help quit
2) persistence
Save: save data synchronization to disk
Bgsave: save data asynchronously to disk
Lastsave: returns the Unix timestamp of the last time data was successfully saved to disk
Shundown: save data synchronization to disk, and then shut down the service
3) remote service control
Info: provides server information and statistics
Monitor: real-time dump of received requests
Slaveof: change replication policy settings
Config: configuring the Redis server at run time
4) commands for value operations
Exists (key): confirm whether a key exists
Del (key): delete a key
Type (key): type of return value
Keys (pattern): returns all key that satisfy a given pattern
Randomkey: one of the randomly returned key spaces
Keyrename (oldname, newname): rename key
Dbsize: returns the number of key in the current database
Expire: set the active time of a key (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: deletes all key in the currently selected database
Flushall: delete all key in all databases
5) String
Set (key, value): assigns the value value to the string named key in the database
Get (key): returns the value of the string named key in the database
Getset (key, value): assign the last value to the string named key
Mget (key1, key2, … , key N): returns the value of multiple string in the library
Setnx (key, value): add string with the name key and the value value
Setex (key, time, value): add string to the library and set the expiration time time
Mset (key N, value N): set the values of multiple string in batch
Msetnx (key N, value N): if all string named key I do not exist
Incr (key): string add 1 operation named key
Incrby (key, integer): string named key adds integer
Decr (key): string minus 1 operation named key
Decrby (key, integer): string named key reduces integer
Append (key, value): the value of the string named key is appended to value
Substr (key, start, end): returns the substring of the value of string with the name key
6) List
Rpush (key, value): add an element with the value value to the end of the list named key
Lpush (key, value): add an element with a value 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 the list named key
Ltrim (key, start, end): intercept list named key
Lindex (key, index): returns the element of the index location in the list named key
Lset (key, index, value): assign a value to the element of index position in the list named key
Lrem (key, count, value): delete the element whose value is value in the list of count key
Lpop (key): returns and deletes the first element in the list named key
Rpop (key): returns and deletes 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 deletes the trailing element of the list named srckey and adds it to the header of the list named dstkey
7) Set
Sadd (key, member): add the element member to the set named key
Srem (key, member): deletes the element member in the set named key
Spop (key): randomly returns and deletes an element in the set named key
Smove (srckey, dstkey, member): move to a collection element
Scard (key): returns the cardinality of the set named key
Sismember (key, member): whether member is an element of set named key
Sinter (key1, key2, … Key N): find the intersection
Sinterstore (dstkey, (keys)): find the intersection and save the intersection to the collection of dstkey
Sunion (key1, (keys)): Union set
Sunionstore (dstkey, (keys)): join and save the union to the collection of dstkey
Sdiff (key1, (keys)): subtraction set
Sdiffstore (dstkey, (keys)): take the difference set and save the difference set to the collection of dstkey
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): add the element field to the hash named key
Hget (key, field): returns the value corresponding to field in the hash named key
Hmget (key, (fields)): returns the value corresponding to field I in the hash named key
Hmset (key, (fields)): add the element field to the hash named key
Hincrby (key, field, integer): add integer to the value of field in the hash named key
Hexists (key, field): whether there is a domain with the key field in the hash named key
Hdel (key, field): delete the domain with the key field in the hash named key
Hlen (key): returns the number of elements in a hash named key
Hkeys (key): returns all keys in the hash with the name key
Hvals (key): returns the value of all keys in the hash named key
Hgetall (key): returns all keys (field) and their corresponding value in the hash named key
Redis advanced application
1. Security
After setting up the client connection, a password is required before any operation is specified. An external user can access 150W times in a second. The specific operation password modification setting the requirepass attribute in redis.conf gives the password. Of course, what I give here is primos.
After that, if you want to operate, you can log in and authorize it to use:
Sudo / opt/java/redis/bin/redis-cli-a primos
Or after entering the auth primos, you can operate at will.
2. Master-slave replication
When I did this, I prepared two virtual machines. The ip are 192.168.15.128 and 192.168.15.133 respectively.
Master-slave replication allows multiple slave server to have the same database replica as master server
The specific configuration is to configure slave on slave.
Slaveof 192.168.15.128 6379
Masterauth primos
If there is no master-slave synchronization, then check to see if it is a firewall problem. I use ufw. Just set up sudo ufw allow 6379.
At this time, you can check the specific situation through info.
3. Transaction processing
Redis's support for transactions is relatively simple. Redis can only ensure that commands in a transaction initiated by client can be executed continuously without inserting commands from other client. When a client issues a multi command in a connection, the connection enters the context of a transaction, and the subsequent command of the connection is not executed immediately, but is first placed in a queue. When the exec command is executed, redis executes all the commands in the queue sequentially.
For example, I have an example below.
Set age 100
Multi
Set age 10
Set age 20
Exec
Get age-- this content should be 20.
Multi
Set age 20
Set age 10
Exec
Get age-the content at this time becomes 10, which fully reflects the way it is executed in queue order.
Discard cancels all transactions, that is, transaction rollback
However, when there are individual errors in the execution of the redis transaction, the transaction will not be rolled back, the content that is not wrong will be executed, and the content of the error will be abandoned directly. At present, the latest 2.6.7 also has this problem.
Optimistic lock
Watch key if there is no change in watch's key, then outdate's transaction cannot be executed.
4. Persistence mechanism
Redis is an in-memory database that supports persistence
Snapshotting snapshot mode, the default storage method, which is written to the binary file of dump.rdb by default. You can configure redis to take snapshots automatically if more than m key are modified in n seconds.
In append-only file aof mode, when using aof, redis appends each function to the file, and when redis restarts, it reexecutes the saved life in the file.
Make it in memory.
5. Publish and subscribe message sbusribe publish operation, which is actually similar to the message publishing under linux.
6. Use of virtual memory
Can configure vm function, save path, maximum memory online, number of pages, page size, maximum worker thread
Temporarily modify the ip address ifconfig eth0 192.168.15.129
Redis-cli parameter
Usage: redis-cli [OPTIONS] [cmd [arg [arg...]
-h Server hostname (default: 127.0.0.1)
-p Server port (default: 6379)
-s Server socket (overrides hostname and port)
-a Password to use when connecting to the server
-r Execute 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 for replies (default when STDOUT is not a tty)
-- latency Enter a special mode continuously sampling latency
-- slave Simulate a slave showing commands received from the master
-- pipe Transfer raw Redis protocol from stdin to server
-- bigkeys Sample Redis keys looking for bigkeys
-- eval Send an EVAL command using the Lua script at
-- help Output this help and exit
-- version Output version and exit
Examples:
Cat / etc/passwd | redis-cli-x set mypasswd
Redis-cli get mypasswd
Redis-cli-r 100 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) check the number of keys
Keys * / / View all keys
Keys prefix_* / / View all keys prefixed with "prefix_"
2) clear the database
Flushdb / / clear all keys of the current database
Flushall / / clear all keys for all databases
This is the end of this article on "what are the common commands in Redis?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.