redis的簡介和使用
來自:http://www.cnblogs.com/fangwenjun/p/5061849.html
簡介
redis(Remote Dictionary Server)是一種Nosql技術,它是一個開源的高級kv存儲和數據結構存儲系統,它經常被拿來和Memcached相比較,但是Memcached不提供持久化的數據保存機制而redis可以將數據保存在磁盤中,redis不僅僅是能夠存儲key和value這種簡單的鍵值對,還能存儲例如集合、hash表、列表、字典等。redis在整個運行過程中,數據統統都是存儲在內存中的,因此,性能是相當高的,由于此特性,redis對于內存的要求比較高,它會周期性的將內存中的數據寫入在磁盤中,從而實現數據持久化的訪問能力,但是這種存儲只是保證redis在下次啟動還有數據可以讀取,而不是提供訪問。redis是單線程服務的,只有一個線程。redis還支持主從模式以及支持通過lua腳本去編寫擴展,并且支持高可用和分布式集群解決方案。
Redis特點
- 運行在內存
- 持久化
- 主從(借助于sentinel實現一定意義上的HA)
- Clustering(分布式存儲)
- 數據機構服務器:支持存儲string、list、hash、set、Sorted Set,Bitmap,HyperLoglogs
- 能夠作為隊列使用 </ol>
- 推ter
- tumblr
- github
- stack overflow
- digg
- blizard
- flickr
- RDBMS:Oracle、DB2、Mysql
- NoSQL:MongoDB、Redis、HBase、Memcached
- NewSQL:Aerospike、FounddtionDB、RethinkDB </ol>
- Key-Value NoSql:Memcached、Redis
- Column family NoSQL:Cassandra、HBase
- Documentation NoSQL:MongoDB
- Graph NoSQL:Neo4j </ol>
- 2015年4月1日正式推出,它的特性:
- redis cluster
- 新的"embedded string"
- LRU演算法的改進:預設隨機取5個樣本,插入并排序至一個pool。移除最佳者,如此反復,直到內存用量小于maxmemory的設定。 </ul>
- redis-server
- redis-cli
- redis-benchmark(壓測工具)
- redis-check-dump && redis-check-aof 能夠去檢測文件是否損壞兩種格式RDB/AOF格式 </ol>
-
編譯
wget -c http://download.redis.io/releases/redis-3.0.6.tar.gz tar xvf redis-version.tar.gz cd redis-version make && make install
注意:安裝需要編譯環境,例如gcc等必要工具的支持
- daemonize:是否以后臺daemon方式運行,no為禁止,yes為運行
- pidfile:pid文件位置
- tcp-backlog(如果訪問量非常大,接收緩沖已經滿了,制定一個位置緩沖新請求,tcp協議一般會有backlog)
- bind 監聽地址,默認在127.0.0.1 我們可以指定多個地址,例如bind 127.0.0.1 10.0.1.243
- port:監聽的端口號,默認是6379,可以自己指定其他地址
- unixsocket和unixsocketperm 定義sokcet的pid文件位置和權限如果你的服務器和redis在同一臺服務器上推薦指定socket方式,給予socket的方式在內存中直接交換而不經過tcp協議棧去封裝,
- timeout:請求超時時間,0表示禁用
- loglevel:log信息級別
- logfile:log文件位置
- databases:開啟數據庫的數量,在redis的集群中只支持0庫
-
save * :保存快照的頻率,第一個 表示多長時間,第三個*表示執行多少次寫操作。在一定時間內執行一定數量的寫操作時,自動保存快照。可設置多個條件,例如以下,表示900秒有1個鍵發生變化就執行寫操作,如果在300秒內,有10個鍵發生變化就執行寫操作,如果60秒有1000個鍵發生變化則執行寫操作。
save 900 1 save 300 10 save 60 10000
-
rdbcompression:是否使用壓縮
-
dbfilename:數據快照文件名(只是文件名,不包括目錄)
-
dir:數據快照的保存目錄(這個是目錄)
-
appendonly:是否開啟appendonlylog,開啟的話每次寫操作會記一條log,這會提高數據抗風險能力,但影響效率。
-
appendfsync:appendonlylog如何同步到磁盤(三個選項,分別是每次寫都強制調用fsync、每秒啟用一次fsync、不調用fsync等待系統自己同步)
-
slaveof, 啟動主從,需要指定iP和端口
備注:redis是單線程,但是這并不意味著會成為運行時的瓶頸,每秒能夠支撐50W的并發
</div>哪些公司在使用
數據庫存儲系統分類
常見的數據庫系統有以下幾類:
而NoSQL又分為以下幾類:
Redis 3.0
Redis的組件
最早只有1W行代碼,網址是 http://www.redis.io ,它的組件有:
redis的安裝
redis的安裝是非常簡單的,你甚至都不需要去configure,直接make && makeinstall make也不需要帶過多的參數。
官網:
2.提示缺少tcl,可以通過yum install tcl去安裝
[root@redis redis-3.0.6]# make test cd src && make test make[1]: Entering directory `/root/redis-3.0.6/src' You need tcl 8.5 or newer in order to run the Redis test make[1]: *** [test] Error 1 make[1]: Leaving directory `/root/redis-3.0.6/src' make: *** [test] Error 2
3.解決錯誤之后,我們就可以通過redis-server redis.conf去啟動redis服務端了
[root@redis redis-3.0.6]# redis-server redis.conf 4624:M 20 Dec 11:56:53.375 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 4624 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 4624:M 20 Dec 11:56:53.376 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 4624:M 20 Dec 11:56:53.376 # Server started, Redis version 3.0.6 4624:M 20 Dec 11:56:53.376 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 4624:M 20 Dec 11:56:53.376 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 4624:M 20 Dec 11:56:53.376 * The server is now ready to accept connections on port 6379
從上面的輸出信息中,我們看到redis是運行在TCP的6379端口上的。
注意:默認的redis.conf文件的daemonize參數為no,所以redis不會在后臺運行,這時要測試,我們需要重新開一個終端。修改為yes則為>后臺運行redis。另外配置文件中規定了pid文件,log文件和數據文件的地址,如果有需要先修改,默認log信息定向到stdout
4.redis的配置文件
redis.conf的主要配置參數的含義:
使用redis客戶端
1.我們修改redis的配置文件,將daemonize修改為yes,將bind打開,然后保存,執行如下操作
[root@redis redis-3.0.6]# redis-server /etc/redis.conf [root@redis redis-3.0.6]# redis-cli 127.0.0.1:6379>
注意:默認redis-cli沒有開啟認證機制,所以直接redis-cli就可以進入redis命令行界面
2.redis的幫助系統,我們進入cli后,可以輸入help查看幫助信息
127.0.0.1:6379> help redis-cli 3.0.6 Type: "help @<group>" to get a list of commands in <group> "help <command>" for help on <command> "help <tab>" to get a list of possible help topics "quit" to exit
例如我們要查看String的相關參數可以使用help @String
127.0.0.1:6379> help @String APPEND key value summary: Append a value to a key since: 2.0.0 BITCOUNT key [start end] summary: Count set bits in a string since: 2.6.0 ……
我們也可以使用help [tab]去tab一些選項
redis的常用命令
Strings的命
1.打開數據庫(名稱空間):SELE [num],最多打開16個
127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> SELECT 2 OK
2.設置字符串
127.0.0.1:6379> help set SET key value [EX seconds] [PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 group: string
3.設置key和value
127.0.0.1:6379> SET disto fedora OK 127.0.0.1:6379> GET disto "fedora"
4.鍵值在同一名稱空間中必須唯一
127.0.0.1:6379> SET disto fedora OK 127.0.0.1:6379> GET disto "fedora" 127.0.0.1:6379> SET disto ubuntu OK 127.0.0.1:6379> GET disto "ubuntu" 127.0.0.1:6379>
5.設置自動增長的值
127.0.0.1:6379> SET count 0 OK 127.0.0.1:6379> INCR count (integer) 1 127.0.0.1:6379> INCR count (integer) 2 127.0.0.1:6379> INCR count (integer) 3
6.設置自動減的值
127.0.0.1:6379> DECR count (integer) 2 127.0.0.1:6379> DECR count (integer) 1 127.0.0.1:6379> DECR count (integer) 0 127.0.0.1:6379> DECR count (integer) -1 127.0.0.1:6379> DECR count (integer) -2
7.設置鍵的手動過期或自動過期時間
SET key value [EX seconds] [PX milliseconds] [NX|XX]
EX 過期時間 NX 如果一個鍵不存在才創建
127.0.0.1:6379> SET disto gento NX (nil)
XX表示存在才創建
127.0.0.1:6379> SET foo bar XX (nil)
8.set還支持integer,但是必須為整數
1)設置integer
127.0.0.1:6379> set foo 1 OK 127.0.0.1:6379> get foo "1"
2)非integer會出現錯誤
127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> incr foo (error) ERR value is not an integer or out of range
List的常用命令
1.獲取list的幫助
127.0.0.1:6379> help @list BLPOP key [key ...] timeout summary: Remove and get the first element in a list, or block until one is available since: 2.0.0 BRPOP key [key ...] timeout summary: Remove and get the last element in a list, or block until one is available since: 2.0.0 BRPOPLPUSH source destination timeout summary: Pop a value from a list, push it to another list and return it; or block until one is available since: 2.2.0
2.定義一個list
127.0.0.1:6379> LPUSH l1 mon (integer) 1
3.獲取list的值
127.0.0.1:6379> LINDEX l1 0 "mon" 127.0.0.1:6379>
比如我們在給l1增加一個值
127.0.0.1:6379> LPUSH l1 sun (integer) 2
那么,此時,我們通過LINDEX l1 0獲取的就是新增加的sun,而LINDEX l1 1則是mon
127.0.0.1:6379> LINDEX l1 0 "sun" 127.0.0.1:6379> LINDEX l1 1 "mon"
那么,我們要想從右側增加一個值呢?我們可以使用RPUSH去創建
127.0.0.1:6379> RPUSH l1 tue (integer) 3 127.0.0.1:6379> LINDEX l1 2 "tue"
修改一個值
127.0.0.1:6379> LSET l1 1 fri OK 127.0.0.1:6379> LINDEX l1 1 "fri"
要想從左側彈出一個值用LPOP,要想從右側則用RPOP
127.0.0.1:6379> RPOP l1 "tue" 127.0.0.1:6379> RPOP l1 "fri"
無序集合的操作
1.創建一個集合
127.0.0.1:6379> SADD w1 mon tue wed thu fre sat sun (integer) 7 127.0.0.1:6379> SADD w2 tue thu day (integer) 3
2.求交集
127.0.0.1:6379> SINTER w1 w2 1) "thu" 2) "tue" 127.0.0.1:6379>
2.求并集
127.0.0.1:6379> SUNION w1 w2 1) "thu" 2) "day" 3) "sun" 4) "tue" 5) "fre" 6) "wed" 7) "mon" 8) "sat" 127.0.0.1:6379>
3.彈出元素
127.0.0.1:6379> SPOP w1 "sun"
5.查看元素是否存在
127.0.0.1:6379> SISMEMBER w1 mon (integer) 1 127.0.0.1:6379> SISMEMBER w1 fri (integer) 0
有序集合操作
127.0.0.1:6379> HELP @SORTED_SET
1.添加元素到集合
127.0.0.1:6379> ZADD weekday1 1 mon 2 tue 3 wed (integer) 3
2.獲取元素的總數
127.0.0.1:6379> ZCARD weekday1 (integer) 3
3.查看索引號
127.0.0.1:6379> ZRANK weekday1 tue (integer) 1
4.根據元素獲取SCORE
127.0.0.1:6379> ZSCORE weekday1 mon "1"
5.指明索引范圍獲取值
127.0.0.1:6379> ZRANGE weekday1 0 2 1) "mon" 2) "tue" 3) "wed"
Hashes的操作
1.添加/補充新值一個hashes
127.0.0.1:6379> HSET h1 a mon (integer) 1
2.獲取值
127.0.0.1:6379> HGET h1 a "mon"
3.獲取所有值
127.0.0.1:6379> HKEYS h1 1) "a" 2) "b"
4.獲取元素總數
127.0.0.1:6379> HLEN h1 (integer) 2
5.刪除鍵
127.0.0.1:6379> HDEL h1 a (integer) 1 127.0.0.1:6379> HGET h1 a (nil)