回到頂部(go to top)html
C語言編寫的高性能鍵值對數據,支持的鍵值數據類型:python
Redis的應用場景:c++
1 環境準備 #官網下載 redis 3.2.5版本 wget http://download.redis.io/releases/redis-4.0.1.tar.gz #安裝 C 編譯環境 yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc gcc-c++ 2 安裝 解壓安裝包後,進入文件目錄編譯,編譯結束時,會提示 Hint: It's a good idea to run 'make test' ,建議在安裝前先測試預安裝下,make test預安裝後,遇到錯誤:You need tcl 8.5 or newer in order to run the Redis test ,缺失安裝包tcl,因此須要先安裝這個 安裝包後再次運行 make test,正常後再進行redis安裝。 詳細步驟以下: #解壓二進制包 tar -zvxf /opt/redis-3.2.5 #進入到文件目錄 cd redis-3.2.5 #編譯 make #測試安裝(稍微耗費點時間) make test #可能會提醒須要安裝最新版的tcl #yum install tcl #指定路徑安裝 make PREFIX=/usr/local/redis install
安裝結束後,進入到安裝路徑中,github
[root@bogon bin]# ls -lh /usr/local/redis/bin/web
total 22Mredis
-rwxr-xr-x. 1 root root 2.4M Aug 13 18:40 redis-benchmark算法
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-check-aofsql
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-check-rdbmongodb
-rwxr-xr-x. 1 root root 2.5M Aug 13 18:40 redis-cli
lrwxrwxrwx. 1 root root 12 Aug 13 18:40 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-server
這幾個指令用途分別是:
1 #拷貝conf文件到/etc目錄 2 cp /opt/redis/redis-4.0.1/redis.conf /etc/redis.conf 3 4 5 #redis.conf 參數說明 6 7 ################################## NETWORK ##################################### 8 9 #綁定的主機地址 10 bind 127.0.0.1 11 12 #保護模式,是否容許 沒有認證配置的主機或接口鏈接redis,默認是啓動保護模式,則不容許這種狀況 13 protected-mode yes 14 15 #指定redis的監聽端口,默認端口是6379,做者在本身的一篇博文中解釋了爲何選用6379做爲默認端口,由於6379在手機按鍵上MERZ對應的號碼,而MERZ取自意大利歌女Alessia Merz的名字,嗯,你開發的,你說了算。 16 port 6379 17 18 # In high requests-per-second environments you need an high backlog in order 19 # to avoid slow clients connections issues. Note that the Linux kernel 20 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 21 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 22 # in order to get the desired effect. 23 24 tcp-backlog 511 25 26 #客戶端連接多長時間後關閉連接,單位是秒,指定爲0,則表示關閉該功能 27 timeout 0 28 29 # A reasonable value for this option is 300 seconds, which is the new 30 # Redis default starting with Redis 3.2.1. 31 tcp-keepalive 300 32 33 ################################# GENERAL ##################################### 34 35 #Redis默認不是以守護進程的方式運行,能夠經過該配置項修改,使用yes啓用守護進程 36 daemonize yes 37 38 # If you run Redis from upstart or systemd, Redis can interact with your 39 # supervision tree. Options: 40 # supervised no - no supervision interaction 41 # supervised upstart - signal upstart by putting Redis into SIGSTOP mode 42 # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET 43 # supervised auto - detect upstart or systemd method based on 44 # UPSTART_JOB or NOTIFY_SOCKET environment variables 45 # Note: these supervision methods only signal "process is ready." 46 # They do not enable continuous liveness pings back to your supervisor. 47 supervised no 48
1 #服務端啓動 2 [root@bogon redis-4.0.1]# cd /usr/local/redis/ 3 [root@bogon redis]# ./bin/redis-server /etc/redis.conf 4 74537:C 13 Aug 18:53:30.774 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5 74537:C 13 Aug 18:53:30.774 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=74537, just started 6 74537:C 13 Aug 18:53:30.774 # Configuration loaded 7 [root@bogon redis]# ps axu | grep redis 8 root 74538 0.6 0.2 145248 2168 ? Ssl 18:53 0:00 ./bin/redis-server 127.0.0.1:6379 9 root 74665 0.0 0.0 112648 968 pts/4 S+ 18:53 0:00 grep --color=auto redis 10 11 #客戶端啓動 12 redis-cli [-h 127.0.0.1] [-p 6379] 13 127.0.0.1:6379> ping 14 PONG 15 16 #存儲鍵值對 17 127.0.0.1:6379> set name imooc 18 OK 19 20 #獲取name對應的value 21 127.0.0.1:6379> get name 22 "imooc" 23 24 #獲取全部keys 25 127.0.0.1:6379> keys * 26 1) "name" 27 28 #刪除keys 29 127.0.0.1:6379> del name 30 (integer) 1 31 127.0.0.1:6379> get name 32 (nil) 33 34 #關閉服務端 35 redis-cli shutdow
redis有支持很是多種語言編寫的客戶端,能夠從官網查看 https://redis.io/clients ,redis-py是redis官方網站首選的python客戶端開發包,本人只會點點python,因此從這個入門。
redis-py的github地址:https://github.com/andymccurdy/redis-py
鍵值對種key的注意事項:
string中的一個key對應一個value,values最長可達512Mb。
string經常使用命令:
hash能夠存儲多個鍵值對之間的映射,它就像是一個迷你型的redis。
lish的順序是按照插入的順序,能夠在頭部跟尾部插入數據,若是是在list的兩頭進行操做,那麼效率是很高的,可是若是在list中,則會耗費必定時間。
list的類型:
list經常使用的命令:
和list類型不一樣的是,set集合中不容許出現重複的元素,set最大能夠包含的元素是 4294967295 。注意,set中是沒有順序的。
用於維護用戶對象的惟一性,以及處理數據對象之間的關聯關係,能夠進行並集交集差集運算。好比購買A產品的用戶ID,放在一個set中,購買另一個B產品的用戶ID,放在另一個set中,這樣就很方便計算同時購買兩個產品的用戶等。
list經常使用指令:
sorted set跟set是比較相似的,集合中不容許出現重複的元素,那麼有啥區別呢?sorted set有順序,從小到大排序,更新操做很是快,訪問數據也很是高效。
應用場景:遊戲排名、微博熱點
經常使用命令:
redis最多支持16個數據,下標0-15表示第幾個數據庫。默認是在0號數據。切換數據庫能夠經過select dbnumber 來切換,也能夠經過move 來移動key從當前數據到指定的數據庫。
事務的指令:multi、exec、discard。redis中,若是某個命令執行失敗,後面的命令還會繼續執行。multi,開啓事務,這個指令後的指令默認爲在同一個事務內,exec等同於提交,discard等同於回滾。
redis的高性能是由於數據都在內存中,若是數據庫重啓,則全部數據都會丟失,那麼如何進行數據持久化呢?