It is often necessary to connect to Redis during daily code writing. I'll show you two ways to connect to Redis in Python
Method one, using host and port connections
Please look at the code:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportRedis#Redis Libraries introduced in Python4 5R = Redis. Strictredis (host="192.168.163.229", port=6379)#Create a Strictredis object6R.lpush (" City","Shanghai")#Start operating Redis (add "Shanghai" to city using the Lpush method)
Code parsing:
Line 3: Introduction of Redis Library. If you do not have a Redis library installed, install the library first. Installation method Here is not to repeat.
Line 5: Create the Stricredis object, the incoming parameters are host and port, respectively, the IP and port of the Redis host. Of course, you can also create Redis objects. Redis classes and Stricredis classes can operate Redis in a Redis library, except that there are differences between the two, which are not described here.
Line 6: Using the Stricredis method to operate Redis is very convenient
Method two, using a UNIX socket to connect Redis
Please look at the code:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportRedis#Redis Libraries introduced in Python4 5R = Redis. Strictredis (unix_socket_path="/tmp/redis.socket")#Create a Strictredis object6R.lpush (" City", "Hangzhou")#Start operating Redis (add "Hangzhou" to City using the Lpush method)
Code parsing: Other than when creating objects, the parameters are different.
About Redis socket connections.
By default, when Redis is installed and started with the default profile, the Redis connection is only host:port, so how do I configure the Redis socket connection method?
Modify the Redis.conf file
1 TCP Listen () backlog.2 #3# in-Requests-per-second environments you need a high backloginchOrder4 # To avoid slow clients connections issues. Note that the Linux kernel5# would silently truncate it to the value of/proc/sys/net/core/Somaxconn So6 # Make sure to raise both the value of Somaxconn and Tcp_max_syn_backlog7#inchOrder toGetThe desired effect.8Tcp-backlog5119 Ten # Unix sockets. One # A# Specify the path forThe Unix socket that would be a used to listen for -# Incoming connections. There isNodefault, so Redis would not listen - # on a UNIX socket if not specified. the # -unixsocket/tmp/Redis.sock -Unixsocketperm the - +# Close The connection after a client isIdle forN seconds (0To disable) -Timeout0 + A # TCP KeepAlive. at # -# If Non-zero, use so_keepalive to send TCP ACKs to clientsinchabsence -# of communication. This isUseful forreasons: - # -#1) Detect dead peers. -#2) Take the connection alive fromThe point of view of the network in# equipmentinchThe middle. - # to# on Linux, the specified value (inchSeconds isThe period used to send ACKs. +# Note that to close the connection theDoubleof the time isneeded. -# on and kernels the period depends on the kernel configuration.
Where the modified item is
unixsocket/tmp/
The directory after Unixsocket is the absolute path to the sock file.
After modifying the configuration and restarting Redis, you can connect using the socket. The Linux command line can use Redsi-cli-s/tmp/redis.sock to make a redis connection to the socket.
In a general production environment, Redis is connected using a proxy. So how do I configure the socket for the Twemproxy proxy? Don't worry, listen to me slowly.
Install the TW agent on machines that need to connect to Redis. How do I install twemproxy? Transmission Door
Configure after the installation is complete.
cd/usr/local/twemproxy # go to twemproxy directory mkdir conf # Create CONF directory, default does not have this directory CD confvim Ethan.ymlethan: 0666 hash:fnv1a_64 "{}" Distribution:ketama false up to True servers :192.168 . 163.229:6379:1
Where Ethan is a custom name, as long as the global is unique.
Listen the socket file that is listening for TW.
Hash:hash function, support md5,crc16,crc32,finv1a_32 and so on more than 10 kinds;
Timeout:the timeout value in msec, we wait for to establish a connection to the server or receive a response from a s Erver. By default, we wait indefinitely.
That is, the time-out.
Servers can define multiple Redis servers, with the last digit representing the weight of the load balancer.
After the configuration is complete, start Twemproxy:
/usr/local/twemproxy/sbin/nutcracker-c/usr/local/twemproxy/conf/ethan.yml &
If there is no error, the socket is configured to complete.
At this point, you can connect to Redis via file/tmp/ethan.socket.
Two ways Python connects to Redis