python redis模块的常见的几个类 Redis 、StricRedis和ConnectionPool

日常写代码过程中,经常需要连接redis进行操作。下面我就介绍下python操作redis模块redis中的几个常见类,包括redis连接池。

一、StrictRedis 类

请看代码:。

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")
 

代码解析:

line 3 :引入redis库。如果未安装redis库,请先安装该库。安装方法这里就不赘述了。

line 5: 创建StricRedis对象,传入的参数是host 和port,分别为redis主机的ip和port。 当然也可以创建Redis对象。redis库中Redis类和StricRedis类都可以操作redis,只不过两者有区别,区别这里不作介绍

line 6: 利用StricRedis中的方法操作redis,非常方便

line 8~9:通过socket连接redis

二、Redis类

请看代码:

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类,至于原因,请自行查阅。

三、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了。

原文地址:https://www.cnblogs.com/hzpythoner/p/7699595.html