python redis模塊的常見的幾個類 Redis 、StricRedis和ConnectionPool

平常寫代碼過程當中,常常須要鏈接redis進行操做。下面我就介紹下python操做redis模塊redis中的幾個常見類,包括redis鏈接池。html

 

1、StrictRedis 類python

請看代碼:。linux

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")
7  
8  r2 = redis.StrictRedis(unix_socket_path="/tmp/redis.socket")  # 建立StrictRedis對象
9  r2.lpush("city", "hangzhou")     # 開始操做redis(利用lpush方法向city中加入"hangzhou")

 

 

代碼解析:redis

line 3 :引入redis庫。若是未安裝redis庫,請先安裝該庫。安裝方法這裏就不贅述了。vim

line 5: 建立StricRedis對象,傳入的參數是host 和port,分別爲redis主機的ip和port。 固然也能夠建立Redis對象。redis庫中Redis類和StricRedis類均可以操做redis,只不過二者有區別,區別這裏不做介紹服務器

line 6: 利用StricRedis中的方法操做redis,很是方便負載均衡

line 8~9:經過socket鏈接redissocket

 

 

2、Redis類tcp

請看代碼:函數

1 #!/usr/bin/env python
2 #  -*- coding:utf-8 -*-
3 import redis              # 引入python的redis庫
4 
5 r = redis.Redis(unix_socket_path="/tmp/redis.socket")  # 建立StrictRedis對象
6 r.lpush("city", "beijing")     # 開始操做redis(利用lpush方法向city中加入"hangzhou")

代碼解析:Redis類和StricRedis類使用方式基本是同樣的,可是不推薦使用Redis類,至於緣由,請自行查閱。

 

3、ConnectionPool 類

  

1 redis_pool = redis.ConnectionPool(connection_class=redis.StrictRedis,
2                                   unix_socket_path="/dev/shm/cache.socket", max_connections=5)
3 conn = redis_pool.make_connection()
4 conn.lpush("city", "shenzhen")

代碼解析:

line1: 建立redis鏈接池,指定鏈接使用的類時StricRedis, 而且經過socket方式鏈接,最大鏈接數是5.

line3:  建立鏈接

line3: 操做redis

 

以上三個類,也能夠自定義一些其餘參數,具體請參照源碼或者官方文檔。

 

 

題外話:關於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了。

相關文章
相關標籤/搜索