实际上,当你打电话给
redis.Redis()
每次发送redis之类的命令时。set()或其他,它将从其连接池中检索连接,并使用此连接发送此命令并等待回复。当请求完成后,它将连接放回连接池以供重用。因此,您不需要自己管理连接。看看这个
https://github.com/andymccurdy/redis-py
了解更多信息。
import threading
from time import sleep
import time, datetime
import traceback
import CACHE_CONST
import json
import os
import MySQLdb
import redis
r_server = redis.Redis(host=CACHE_CONST.REDIS_SERVER_URL, port=CACHE_CONST.REDIS_SERVER_PORT, db=0, password=CACHE_CONST.REDIS_PASS_PHRASE, socket_connect_timeout=2, socket_timeout=2)
# Static methods to interact with the Redis cache server
class CacheUtil(object):
# Log Errors
@staticmethod
def log_message(msg):
log_file = None
log_file = open (os.path.abspath(CACHE_CONST.REDIS_LOG_FILE), "a")
print(msg)
if (log_file):
message = time.strftime("%d-%m-%Y %H:%M:%S")
message += " :: " + str(msg)
log_file.write(message + "\n")
@staticmethod
def saveToCache(hashName, hashValue):
r_server.hmset(hashName, hashValue)
@staticmethod
def getTrackerDetailsByID(trackerId):
hashName = "tDetails:" + str(trackerId)
try:
if trackerId is not None:
print("getTrackerDetailsByID ")
trackerDetail = r_server.hgetall(hashName)
else:
CacheUtil.log_message("getDetailsByID failed with empty trackerId ")
except:
CacheUtil.log_message("getDetailsByID failed, ll fetch from DB " + str(traceback.format_exc()))
return trackerDetail
更新
# COMMAND EXECUTION AND PROTOCOL PARSING
def execute_command(self, *args, **options):
"Execute a command and return a parsed response"
pool = self.connection_pool
command_name = args[0]
connection = pool.get_connection(command_name, **options)
try:
connection.send_command(*args)
return self.parse_response(connection, command_name, **options)
except (ConnectionError, TimeoutError) as e:
connection.disconnect()
if not connection.retry_on_timeout and isinstance(e, TimeoutError):
raise
connection.send_command(*args)
return self.parse_response(connection, command_name, **options)
finally:
pool.release(connection)
对于使用Redis完成操作后等待连接/关闭连接的时间过长有何想法
这是关于TCP连接终止过程的图像。当客户端(启动器)为服务器(接收器)FIN发送ACK时,它进入Time\u WAIT状态。
最后一次确认时,该连接必须在TIME_WAIT状态下保持MSL的两倍。这让TCP
(它们不使用序列号,也不由TCP重新传输),但因为另一方
将重新传输其FIN(它确实消耗序列号)。实际上,TCP总是会重新传输
直到收到最终确认。
还有一篇关于
the purpose about TIME_WAIT