Conventional data operation redis

Introduction

redis is a key-value storage systems, and the like Memcached, which supports the stored value relatively more types, including string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type). Like with memcached, in order to ensure efficiency, the data is cached in memory. Redis difference is periodically updated in the data written to disk or to modify the operation of writing additional log file, and on this basis realize the master-slave (master-slave) Synchronization

Operation key

Redis is a key-value databases, Redis Redis key for managing keys, the basic syntax is
the COMMAND KEY_NAME
192.168.23.100:6379> the SET the Test zhaiky
the OK
192.168.23.100:6379> GET the Test
"zhaiky"
192.168.23.100:6379> Test del
(Integer). 1
192.168.23.100:6379> GET Test
(nil)
192.168.23.100:6379> 

Redis keys and command described in
1) del key to delete the key command is present in the key
2) of the dump key sequence given key, and return the serialized value
3) exists key to check whether the given key is present
4) expire key seconds for a given expiration date set key
5) acts expire key timestamp EXPIREAT eXPIRE and the like, are used to set the expiration time key. Except that the time parameter EXPIREAT accepted command is a UNIX timestamp (UNIX timestamp)
. 6) The expire milliseconds key expiration time of one hundred million key set in milliseconds.
7) expire key milliseconds-timestamp key set an expiration time stamp (unix timestamp) milliseconds
8) keys pattern to find all key match a given pattern (pattern), for example, return all the keys * key
. 9) Move the key DB moving the current key database to a given database among db
10) expiration time of the persist key removal key, key will endure
11) pttl key return key milliseconds remaining expiration time
12) ttl key in seconds , returned to the remaining lifetime of a given key (the TTL, time to Live)
13 is) randomkey returns a random key from the current database
14) rename key newkey modify the name of the key
15) renamenx key newkey newkey only when the absence of the key renamed newkey 
16) Return Type type key stored key value

Redis data types

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: an ordered set).
1) String (String)
String Redis is the most basic data type, a key corresponding to a value
192.168.23.100:6379> SET Test zhaiky
the OK
192.168.23.100:6379> GET Test
"zhaiky"
192.168.23.100:6379> 

2) Hash (hash)
the hash is a key pair set type field is a string value and a mapping table, hash is particularly suited for storing objects
192.168.23.100:6379> HSET hash1 name zhaiky
(Integer). 1
192.168. 23.100: 6379> HSET hash1 Score 100
(Integer) 1
192.168.23.100:6379> hget hash1 name
"zhaiky"
192.168.23.100:6379> hget hash1 Score
"100"
192.168.23.100:6379>
HSET & hget only once to hash structure inserted inside a key-value pair may be used if a plurality of insert & hmget hmset
192.168.23.100:6379> Sex NaN3 hmset the hash2 name zhaiky
the OK
192.168.23.100:6379> hgetall the hash2
. 1) "name"
2) "zhaiky"
. 3) "Sex"
. 4) "NaN3"
192.168.23.100:6379> Sex hmget the hash2 name
. 1) "zhaiky"
2) "nan"
192.168.23.100:6379>

3)LIST(列表)
192.168.23.100:6379> lpush list1 1
(integer) 1
192.168.23.100:6379> lpush list1 2
(integer) 2
192.168.23.100:6379> lpush list1 3
(integer) 3
192.168.23.100:6379> lrange list1 0 1
1) "3"
2) "2"
192.168.23.100:6379> lrange list1 0 10
1) "3"
2) "2"
3) "1"
192.168.23.100:6379>

4) Set (collection)
Redis is a string Set the type of unordered collection, the collection is achieved through a hash table, so add, delete, search complexity is O (1)
192.168.23.100:6379> Sadd SET1 Redis
(Integer). 1
192.168.23.100:6379> Sadd setl Redis
(Integer) 0
192.168.23.100:6379> Sadd setl MySQL
(Integer). 1
192.168.23.100:6379> smembers setl
. 1) "MySQL"
2) "Redis"
192.168. 23.100: 6379> 
Description
set into the collection insert elements, smembers include elements of the collection
successful insertion of return 1; return 0 when the erroneous insertion, the second insertion mongodb example, due to the already present, so the insertion fails.

5) zset (sorted sete: ordered collection)
zset and is set as the set type String, and it does not allow duplicate elements, and a different set zset zset place is associated with a type of double scores, Redis by fractional elements of the collection sorting elements zset is unique, but the score can be repeated
192.168.23.100:6379> Zadd zset1 Redis. 1
(Integer). 1
192.168.23.100:6379> Zadd zset1 2 Redis
(Integer) 0
192.168.23.100:6379> Zadd MySQL. 3 zset1
(Integer). 1
192.168.23.100:6379> Zadd zset1 2 Oracle
(Integer). 1
192.168.23.100:6379> 100 zrangebyscore zset1 0
. 1) "Oracle"
2) "Redis"
. 3) "MySQL"
192.168.23.100: 6379>

Data backup and recovery

Data backup
192.168.23.100:6379> config dir GET backup directory #
1) "dir"
2) "/ the Data / Redis / Redis / bin"
192.168.23.100:6379> the Save  
the OK
Data Recovery
simply copy the backup file dump.rdb to the installation directory to redis

Database operations

Redis in a total of 16 databases, which are 0 to 15, under normal circumstances, the default number is entered into the database 0, if we are to enter the specified database, you can use the select statement
192.168.23.100:6379> 0 select
the OK
192.168.23.100: 6379> * Keys
. 1) "Test"
2) "the hash2"
. 3) "hash1"
. 4) "zset1"
. 5) "test1"
. 6) "List1"
. 7) "setl"
. 8) "test2"
192.168.23.100:6379> . 1 SELECT
the OK
192.168.23.100:6379 [. 1]> Keys *
(List empty or SET)
192.168.23.100:6379 [. 1]> 
192.168.23.100:6379> number dbsize # key for all the current database
(Integer). 8
192.168. 23.100: 6379> flushdb # key to delete all the current database of
the OK
192.168.23.100:6379>flushall # Clear all key database of all
the OK
192.168.23.100:6379> 
192.168.23.100:6379> info # View performance data

Published 60 original articles · won praise 20 · views 4579

Guess you like

Origin blog.csdn.net/zhaikaiyun/article/details/105006527