Redis Introduction
Redis persistence in two ways: semi-persistent, full persistence
Check the default installation directory
Redis-benchmark: performance testing tool, can run on their own book, take a look at (the implementation of the service starts up) how the performance of their own book
Redis-check-aof: fix a problem AOF file, and aof speaking behind rdb
Redis-check-dump file repair problematic dump.rdb
Redis-sentinel (Sentinel) Redis cluster use
Redis-server Redis server startup command
Operator Redis-cli
Check whether the connection is successful Redis
PING PONG input return
Close client may enter or exit ctrl + c
While closing the client and server can use the shutdown command in the client
Close only the server can use Redis-cli shutdown command on the client
Redis single-threaded and multi-channel IO multiplexing
Redis default database 16, similar to the array index starts from 0, the initial default library using 0
Using the command SELECT <the dbid> to switch database, such as: select 8
Unified password management:
All libraries are the same password, or are OK or a Rom (atomicity similar transaction)
Redis single-threaded and multi-channel IO multiplexing
Multiplexing is the use of a plurality of file descriptors to check the thread (the Socket) ready state, such as a function call to select and poll
Incoming multiple file descriptors, if there is a file descriptor is ready, is returned, otherwise blocked until the timeout. After obtaining a ready state
Genuine operations can be performed in the same thread, you can also start a thread execution (such as using a thread pool)
case study:
Serial vs multithreading + lock (memcached) vs + single-threaded multi-channel IO multiplexing (Redis)
Redis data types
String
Set
key+List
Hash
Zset (ordered)
Common Commands
View current database of all keys: keys *
The current database to determine whether there is the key: exists xxx
View current database type in this key: type xxx
To delete a key in the current database: del xxx
A current set of keys in the database of the expiration time, in seconds exprie aaa 10 (named a key button for 10 seconds expire)
View current database of key-value pairs as well as a number of seconds expired (-2 -1 means never expires representative has expired) ttl xxx
View the current number dbsize key database
Empty current library Flushdb
Pass to kill all the libraries Flushall
String type
String is the most basic type Redis, as will be appreciated Memcached exactly the same type, a key corresponding to a value
String type is binary safe means of Redis String may contain any data, such as jpg image or a sequence of objects
String type is the most basic type Redis, a Redis string value can be up to 512M
String basic instructions
get xxx get the key according to the key name
set key value add key-value pairs
append key value given the value added to the end of the original value
strlen key length value acquired
setnx key value is provided only when the key value does not exist Key
incr the key stored in the key by the digital value 1, only the operation of the digital values, if it is empty, a new value of atomicity
DECR key key stored digital value minus 1 atom of
incrby / decrby key steps to decrease key stored numbers, atoms of the defined step
mset key1 value1 key2 value2 .... simultaneously providing one or more key-value pairs
mget key1 value1 key2 value2 .... provided simultaneously acquire the one or more key-value pairs
msetnx key1 value1 key2 value2 simultaneously providing one or more key-value pairs (only in the case where all the given key is not present)
Starting Position Ending Position getrange key obtained worth range, similar to the java subString (after excluding the front subString java package, before the package is also GetRange redis in the package)
SetRange key value starting position covering string value stored in the key value used, starting from the starting position
setex key expiration time value setting key to set the same time the expiration time, in seconds
getset key value set a new value and at the same time to get the old value
Atomicity of Redis
Refers to the so-called atomic operations will not be interrupted thread scheduling mechanism of operation, which, once started, it runs to completion, without any intermediate context swith (switching to another thread)
A single thread can be completed in a single instruction operation may be considered a "atomic operations" because the instruction interrupt can only occur between the
2 in multiple threads, can not be (thread) to interrupt the operation of other processes that atomic operations
Redis single atomic command mainly due to the single-threaded Redis
Thinking:
Whether in C ++ and java i ++ is an atomic operation? (Non-atomic operation, i ++ as a three-step extraction i, into the register, i + 1, and then stored, which can be interrupted)
List
Multi-touch value
Redis in the list is a simple list of strings, according to the order of insertion sort, you can add an element to the head of the table (left) or tails (to the right)
It is doubly linked underlying implementation, the operation of high performance at both ends by the intermediate node performance under operating table index may be poor
Basic Commands
lpush / rpush key value1 value2 insertion of one or more values from the left or right
lpop / rpop key pop a value from the left or right, the key value, the key value of the light death
rpoplpush key1 key2 key1 spit out a value from the right list, the list on the left into the Key2
obtained lrange key start stop element (from left to right) according to the index subscript
lindex key index to obtain elements (left to right) according to the index subscript
llen key to obtain a list of length
linsert key before / after value newvalue newvalue inserted in front of the value of / behind
lrem key n value of n value to delete from the left (left to right) (where n can be 0 (representing the whole deletion) can be a positive integer (from left to right on behalf deleted, deleted above) can be negative integer (from Right to left delete, or that is deleted))
Set
Redis Set List provide external functions and function is similar to a list of special is set to automatically de-duplication, when you need to store a list of data, you do not want to duplicate data set is a good choice, and is set to provide a judgment whether a member in a set of important set of interfaces, which also does not have List
Redis is an unordered collection of the Set of type String, which is the underlying fact of a null value for the hash (a value set of key, value is null) table, so add, delete, search complexity is O (1)
Common Commands
sadd key value1 value2 one or more member added to the collection of key elements which, if the current existing collection of this element, it will be ignored.
Remove all smember key value in the set
sismember determines whether they contain the key value set value, returns 1 if there is no return 0
SMOVE key1 key1 key2 member to the collection member element is moved to the collection key2, SMOVE atomic operation, either succeed together or fail together
scard key returns the number of elements in the set
srem key value to delete an element in the set value
spop key value randomly from the set up a
srandmember key n taken randomly from the set of n values, but not removed from the collection
sinter key1 key2 returns the intersection of two sets of elements
sunion key1 key2 return union of two sets of elements
sdiff key1 key2 returns the difference between two sets of elements set
Hash
Redis hash is a collection of key-value pairs
Redis hash field is a string type and the value of the mapping table
hash particularly suitable for storing objects
Type java inside the Map <String, String>
hset key field value to a key set of key field assignment value
hget key1 field value taken from the set of field key1
hmset key1 field1 value1 field2 value2-volume set of hash values
hexists key filed in to see the hash table key, if stored for a given domain field
hkeys key lists all the field of the hash collection
hvals key lists all of the hash value of the collection
hincrby key field increment value field hash table key field plus the delta increment
hsetnx key field value sets the value of key fields in the hash table is value, which if and only if the domain field is not present
hgetall xxx show the hash table of all field value
note
When you start
redis-cli --raw join --raw can solve the Chinese garbled
token
Redis ordered set Zset ordinary collection set is very similar, is not a set of repeating elements of the string, except that each member of the ordered set is associated with a score (score), the score (score) is used be sorted from highest score assigned the lowest mode of collection members, is the only member of the collection, but the score may be repeated