http://blog.nosqlfan.com/html/3139.html?ref=rediszthtml
雖然Redis已經很火了,相信仍是有不少同窗對Redis只是有所聽聞或者瞭解並不全面,下面是一個比較系統的Redis介紹,對Redis的特性及各類數據類型及操做進行了介紹。是一個很不錯的Redis入門教程。git
REmote DIctionary Server(Redis) 是一個由Salvatore Sanfilippo寫的key-value存儲系統。Redis提供了一些豐富的數據結構,包括 lists, sets, ordered sets 以及 hashes ,固然還有和Memcached同樣的 strings結構.Redis固然還包括了對這些數據結構的豐富操做。redis
Redis能存儲二進制安全的字符串,最大長度爲1GBsql
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> GET name "John Doe"
String類型還支持批量的讀寫操做api
redis 127.0.0.1:6379> MSET age 30 sex "male" OK redis 127.0.0.1:6379> MGET age sex 1) "30" 2) "male"
String類型其實也能夠用來存儲數字,並支持對數字的加減操做。安全
redis 127.0.0.1:6379> INCR age (integer) 31 redis 127.0.0.1:6379> INCRBY age 4 (integer) 35 redis 127.0.0.1:6379> GET age "35" redis 127.0.0.1:6379> DECR age (integer) 34 redis 127.0.0.1:6379> DECRBY age 4 (integer) 30 redis 127.0.0.1:6379> GET age "30"
String類型還支持對其部分的修改和獲取操做數據結構
redis 127.0.0.1:6379> APPEND name " Mr." (integer) 12 redis 127.0.0.1:6379> GET name "John Doe Mr." redis 127.0.0.1:6379> STRLEN name (integer) 12 redis 127.0.0.1:6379> SUBSTR name 0 3 "John"
Redis可以將數據存儲成一個鏈表,並能對這個鏈表進行豐富的操做app
redis 127.0.0.1:6379> LPUSH students "John Doe" (integer) 1 redis 127.0.0.1:6379> LPUSH students "Captain Kirk" (integer) 2 redis 127.0.0.1:6379> LPUSH students "Sheldon Cooper" (integer) 3 redis 127.0.0.1:6379> LLEN students (integer) 3 redis 127.0.0.1:6379> LRANGE students 0 2 1) "Sheldon Cooper" 2) "Captain Kirk" 3) "John Doe" redis 127.0.0.1:6379> LPOP students "Sheldon Cooper" redis 127.0.0.1:6379> LLEN students (integer) 2 redis 127.0.0.1:6379> LRANGE students 0 1 1) "Captain Kirk" 2) "John Doe" redis 127.0.0.1:6379> LREM students 1 "John Doe" (integer) 1 redis 127.0.0.1:6379> LLEN students (integer) 1 redis 127.0.0.1:6379> LRANGE students 0 0 1) "Captain Kirk"
Redis也支持不少修改操做nosql
redis 127.0.0.1:6379> LINSERT students BEFORE "Captain Kirk" "Dexter Morgan" (integer) 3 redis 127.0.0.1:6379> LRANGE students 0 2 1) "Dexter Morgan" 2) "Captain Kirk" 3) "John Doe" redis 127.0.0.1:6379> LPUSH students "Peter Parker" (integer) 4 redis 127.0.0.1:6379> LRANGE students 0 3 1) "Peter Parker" 2) "Dexter Morgan" 3) "Captain Kirk" 4) "John Doe" redis 127.0.0.1:6379> LTRIM students 1 3 OK redis 127.0.0.1:6379> LLEN students (integer) 3 redis 127.0.0.1:6379> LRANGE students 0 2 1) "Dexter Morgan" 2) "Captain Kirk" 3) "John Doe" redis 127.0.0.1:6379> LREM students 1 "John Doe" (integer) 1 redis 127.0.0.1:6379> LLEN students (integer) 1 redis 127.0.0.1:6379> LRANGE students 0 1 1) "Captain Kirk"
Redis可以將一系列不重複的值存儲成一個集合oop
redis 127.0.0.1:6379> SADD birds crow (integer) 1 redis 127.0.0.1:6379> SADD birds pigeon (integer) 1 redis 127.0.0.1:6379> SADD birds bat (integer) 1 redis 127.0.0.1:6379> SADD mammals dog (integer) 1 redis 127.0.0.1:6379> SADD mammals cat (integer) 1 redis 127.0.0.1:6379> SADD mammals bat (integer) 1 redis 127.0.0.1:6379> SMEMBERS birds 1) "bat" 2) "crow" 3) "pigeon" redis 127.0.0.1:6379> SMEMBERS mammals 1) "bat" 2) "cat" 3) "dog"
Sets結構也支持相應的修改操做
redis 127.0.0.1:6379> SREM mammals cat (integer) 1 redis 127.0.0.1:6379> SMEMBERS mammals 1) "bat" 2) "dog" redis 127.0.0.1:6379> SADD mammals human (integer) 1 redis 127.0.0.1:6379> SMEMBERS mammals 1) "bat" 2) "human" 3) "dog"
Redis還支持對集合的子交併補等操做
redis 127.0.0.1:6379> SINTER birds mammals 1) "bat" redis 127.0.0.1:6379> SUNION birds mammals 1) "crow" 2) "bat" 3) "human" 4) "pigeon" 5) "dog" redis 127.0.0.1:6379> SDIFF birds mammals 1) "crow" 2) "pigeon"
Sorted Sets和Sets結構類似,不一樣的是存在Sorted Sets中的數據會有一個score屬性,並會在寫入時就按這個score排好序。
redis 127.0.0.1:6379> ZADD days 0 mon (integer) 1 redis 127.0.0.1:6379> ZADD days 1 tue (integer) 1 redis 127.0.0.1:6379> ZADD days 2 wed (integer) 1 redis 127.0.0.1:6379> ZADD days 3 thu (integer) 1 redis 127.0.0.1:6379> ZADD days 4 fri (integer) 1 redis 127.0.0.1:6379> ZADD days 5 sat (integer) 1 redis 127.0.0.1:6379> ZADD days 6 sun (integer) 1 redis 127.0.0.1:6379> ZCARD days (integer) 7 redis 127.0.0.1:6379> ZRANGE days 0 6 1) "mon" 2) "tue" 3) "wed" 4) "thu" 5) "fri" 6) "sat" 7) "sun" redis 127.0.0.1:6379> ZSCORE days sat "5" redis 127.0.0.1:6379> ZCOUNT days 3 6 (integer) 4 redis 127.0.0.1:6379> ZRANGEBYSCORE days 3 6 1) "thu" 2) "fri" 3) "sat" 4) "sun"
Redis可以存儲key對多個屬性的數據(好比user1.uname user1.passwd)
redis 127.0.0.1:6379> HKEYS student 1) "name" 2) "age" 3) "sex" redis 127.0.0.1:6379> HVALS student 1) "Ganesh" 2) "30" 3) "Male" redis 127.0.0.1:6379> HGETALL student 1) "name" 2) "Ganesh" 3) "age" 4) "30" 5) "sex" 6) "Male" redis 127.0.0.1:6379> HDEL student sex (integer) 1 redis 127.0.0.1:6379> HGETALL student 1) "name" 2) "Ganesh" 3) "age" 4) "30"
Hash數據結構可以批量修改和獲取
redis 127.0.0.1:6379> HMSET kid name Akshi age 2 sex Female OK redis 127.0.0.1:6379> HMGET kid name age sex 1) "Akshi" 2) "2" 3) "Female"
Redis支持這樣一種特性,你能夠將數據推到某個信息管道中,而後其它人能夠經過訂閱這些管道來獲取推送過來的信息。
用一個客戶端訂閱管道
redis 127.0.0.1:6379> SUBSCRIBE channelone Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channelone" 3) (integer) 1
另外一個客戶端往這個管道推送信息
redis 127.0.0.1:6379> PUBLISH channelone hello (integer) 1 redis 127.0.0.1:6379> PUBLISH channelone world (integer) 1
而後第一個客戶端就能獲取到推送的信息
redis 127.0.0.1:6379> SUBSCRIBE channelone Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channelone" 3) (integer) 1 1) "message" 2) "channelone" 3) "hello" 1) "message" 2) "channelone" 3) "world"
用下面的命令訂閱全部channel開頭的信息通道
redis 127.0.0.1:6379> PSUBSCRIBE channel* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "channel*" 3) (integer) 1
在另外一個客戶端對兩個推送信息
redis 127.0.0.1:6379> PUBLISH channelone hello (integer) 1 redis 127.0.0.1:6379> PUBLISH channeltwo world (integer) 1
而後在第一個客戶端就能收到推送的信息
redis 127.0.0.1:6379> PSUBSCRIBE channel* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "channel*" 3) (integer) 1 1) "pmessage" 2) "channel*" 3) "channelone" 4) "hello" 1) "pmessage" 2) "channel*" 3) "channeltwo" 4) "world"
Redis支持按key設置過時時間,過時後值將被刪除(在客戶端看來是補刪除了的)
用TTL命令能夠獲取某個key值的過時時間(-1表示永不過時)
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> TTL name (integer) -1
下面命令先用EXISTS命令查看key值是否存在,而後設置了5秒的過時時間
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> EXISTS name (integer) 1 redis 127.0.0.1:6379> EXPIRE name 5 (integer) 1
5秒後再查看
redis 127.0.0.1:6379> EXISTS name (integer) 0 redis 127.0.0.1:6379> GET name (nil)
這個值已經沒有了。
上在是直接設置多少秒後過時,你也能夠設置在某個時間點過時,下面例子是設置2011-09-24 00:40:00過時。
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> EXPIREAT name 1316805000 (integer) 1 redis 127.0.0.1:6379> EXISTS name (integer) 0
Redis自己支持一些簡單的組合型的命令,好比以NX結尾命令都是判斷在這個值沒有時才進行某個命令。
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> SETNX name "Dexter Morgan" (integer) 0 redis 127.0.0.1:6379> GET name "John Doe"
redis 127.0.0.1:6379> GETSET name "Dexter Morgan" "John Doe" redis 127.0.0.1:6379> GET name "Dexter Morgan"
固然,Redis還支持自定義的命令組合,經過MULTI和EXEC,將幾個命令組合起來執行
redis 127.0.0.1:6379> SET counter 0 OK redis 127.0.0.1:6379> MULTI OK redis 127.0.0.1:6379> INCR counter QUEUED redis 127.0.0.1:6379> INCR counter QUEUED redis 127.0.0.1:6379> INCR counter QUEUED redis 127.0.0.1:6379> EXEC 1) (integer) 1 2) (integer) 2 3) (integer) 3 redis 127.0.0.1:6379> GET counter "3"
你還能夠用DICARD命令來中斷執行中的命令序列
redis 127.0.0.1:6379> SET newcounter 0 OK redis 127.0.0.1:6379> MULTI OK redis 127.0.0.1:6379> INCR newcounter QUEUED redis 127.0.0.1:6379> INCR newcounter QUEUED redis 127.0.0.1:6379> INCR newcounter QUEUED redis 127.0.0.1:6379> DISCARD OK redis 127.0.0.1:6379> GET newcounter "0"
Redis的全部數據都存儲在內存中,可是他也提供對這些數據的持久化。
數據快照的原理是將整個Redis中存的全部數據遍歷一遍存到一個擴展名爲rdb的數據文件中。經過SAVE命令能夠調用這個過程。
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> SAVE OK redis 127.0.0.1:6379> SET name "Sheldon Cooper" OK redis 127.0.0.1:6379> BGSAVE Background saving started
若是你是使用的brew在Mac OSX上安全的Redis,那麼rdb文件會存在以下路徑
/usr/local/var/db/redis/dump.rdb
Redis還支持一種追加式的操做日誌記錄,叫append only file,其日誌文件以aof結局,咱們通常各爲aof文件。要開啓aof日誌的記錄,你須要在配置文件中進行以下設置:
appendonly yes
這時候你全部的操做都會記錄在aof日誌文件中
redis 127.0.0.1:6379> GET name (nil) redis 127.0.0.1:6379> SET name "Ganesh Gunasegaran" OK redis 127.0.0.1:6379> EXIT → cat /usr/local/var/db/redis/appendonly.aof *2 $6 SELECT $1 0 *3 $3 SET $4 name $18 Ganesh Gunasegaran
Redis支持多個DB,默認是16個,你能夠設置將數據存在哪個DB中,不一樣DB間的數據具備隔離性。也能夠在多個DB間移動數據。
redis 127.0.0.1:6379> SELECT 0 OK redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> SELECT 1 OK redis 127.0.0.1:6379[1]> GET name (nil) redis 127.0.0.1:6379[1]> SELECT 0 OK redis 127.0.0.1:6379> MOVE name 1 (integer) 1 redis 127.0.0.1:6379> SELECT 1 OK redis 127.0.0.1:6379[1]> GET name "John Doe"
Redis還能進行一些以下操做,獲取一些運行信息
redis 127.0.0.1:6379[1]> DBSIZE (integer) 1 redis 127.0.0.1:6379[1]> INFO redis_version:2.2.13 redis_git_sha1:00000000 redis_git_dirty:0 arch_bits:64 multiplexing_api:kqueue ......
Redis還支持對某個DB數據進行清除(固然清空全部數據的操做也是支持的)
redis 127.0.0.1:6379> SET name "John Doe" OK redis 127.0.0.1:6379> DBSIZE (integer) 1 redis 127.0.0.1:6379> SELECT 1 OK redis 127.0.0.1:6379[1]> SET name "Sheldon Cooper" OK redis 127.0.0.1:6379[1]> DBSIZE (integer) 1 redis 127.0.0.1:6379[1]> SELECT 0 OK redis 127.0.0.1:6379> FLUSHDB OK redis 127.0.0.1:6379> DBSIZE (integer) 0 redis 127.0.0.1:6379> SELECT 1 OK redis 127.0.0.1:6379[1]> DBSIZE (integer) 1 redis 127.0.0.1:6379[1]> FLUSHALL OK redis 127.0.0.1:6379[1]> DBSIZE (integer) 0
Redis的客戶端很豐富,幾乎全部流行的語言都有其客戶端,這裏就再也不贅述,有興趣的同窗能夠上Redis的Clients頁面去查找。