python連接redis的兩種方法
日常寫代碼過程中,經常需要連接redis進行操作。下面我就介紹下python連接redis的兩種方法
方法一、使用host 和 port 連接
請看代碼:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import redis # 引入python的redis庫 4 5 r = redis.StrictRedis(host="192.168.163.229", port=6379) # 創建StrictRedis對象 6 r.lpush("city", "shanghai") # 開始操作redis(利用lpush方法向city中加入"shanghai")
代碼解析:
line 3 :引入redis庫。如果未安裝redis庫,請先安裝該庫。安裝方法這裏就不贅述了。
line 5: 創建StricRedis對象,傳入的參數是host 和port,分別為redis主機的ip和port。 當然也可以創建Redis對象。redis庫中Redis類和StricRedis類都可以操作redis,只不過兩者有區別,區別這裏不作介紹
line 6: 利用StricRedis中的方法操作redis,非常方便
方法二、使用unix socket連接redis
請看代碼:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*-3 import redis # 引入python的redis庫 4 5 r = redis.StrictRedis(unix_socket_path="/tmp/redis.socket") # 創建StrictRedis對象 6 r.lpush("city", "hangzhou") # 開始操作redis(利用lpush方法向city中加入"hangzhou")
代碼解析:除了創建對象時的參數不一樣,其他一樣。
關於redis socket連接。
默認情況下,當安裝好了redis,並且以默認的配置文件啟動,則redis的連接方式只有host:port,那麽如何配置redis的socket連接方式呢?
修改redis.conf文件
1 TCP listen() backlog. 2 # 3 # In high requests-per-second environments you need an high backlog in order 4 # to avoid slow clients connections issues. Note that the Linux kernel 5 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 6 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 7 # in order to get the desired effect. 8 tcp-backlog 511 9 10 # Unix socket. 11 # 12 # Specify the path for the Unix socket that will be used to listen for 13 # incoming connections. There is no default, so Redis will not listen 14 # on a unix socket when not specified. 15 # 16 unixsocket /tmp/redis.sock 17 unixsocketperm 700 18 19 # Close the connection after a client is idle for N seconds (0 to disable) 20 timeout 0 21 22 # TCP keepalive. 23 # 24 # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence 25 # of communication. This is useful for two reasons: 26 # 27 # 1) Detect dead peers. 28 # 2) Take the connection alive from the point of view of network 29 # equipment in the middle. 30 # 31 # On Linux, the specified value (in seconds) is the period used to send ACKs. 32 # Note that to close the connection the double of the time is needed. 33 # On other kernels the period depends on the kernel configuration.
其中修改的項是
unixsocket /tmp/redis.sock unixsocketperm 700
其中unixsocket 後面的目錄為sock文件的絕對路徑。
修改完配置,重啟redis,就可以使用socket方式進行連接了。linux命令行可以使用redsi-cli -s /tmp/redis.sock 進行socket方式的redis連接。
一般生產環境,redis是使用代理進行連接的。因此如何配置twemproxy代理的socket呢?別急,聽我慢慢道來
在需要連接redis的機器安裝tw代理。如何安裝twemproxy?傳送門
安裝完成之後進行配置。
cd /usr/local/twemproxy # 進入twemproxy目錄 mkdir conf # 創建conf目錄,默認沒有該目錄 cd conf vim ethan.yml ethan: listen: /tmp/ethan.socket 0666 hash: fnv1a_64 hash_tag: "{}" distribution: ketama auto_eject_hosts: false timeout: 400 redis: true servers: - 192.168.163.229:6379:1
其中ethan 為自定義的名稱,只要全局唯一即可。
listen 為tw偵聽的socket文件。
hash: hash函數,支持md5,crc16,crc32,finv1a_32等十多種;
timeout: The timeout value in msec that we wait for to establish a connection to the server or receive a response from a server. By default, we wait indefinitely.
即超時時間。
servers 可以定義多個redis服務器,最後面的數字表示負載均衡的權重。
配置完成之後,啟動twemproxy:
/usr/local/twemproxy/sbin/nutcracker -c /usr/local/twemproxy/conf/ethan.yml &
如果沒有報錯,證明socket已經配置完成。
此時就可以通過文件/tmp/ethan.socket連接redis了。
python連接redis的兩種方法