我正在使用以下代码连接 Redis 服务器。我看到巨大的连接处于 TIME_WAIT 状态。有什么问题?
root@ubuntu:~$ netstat | grep :6479 | grep TIME_WAIT |wc -l
9061
root@ubuntu:~$ netstat | grep :6479 | grep ESTABLISHED |wc -l
7
一旦使用下面的代码对 Redis 服务器完成操作,我想关闭连接。但我得到了这个错误。
@staticmethod
def disconnectRedisConnection(r_server):
if r_server is not None and r_server:
r_server.connection.disconnect()
我收到以下错误,
r_server.connection.disconnect()
AttributeError: 'Redis' object has no attribute 'connection'
对巨大的 TIME_WAIT 连接/使用 Redis 完成操作后关闭连接有任何想法吗?代码:
import threading
from time import sleep
import time, datetime
import traceback
import CACHE_CONST
import json
import os
import MySQLdb
import redis
# 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 = CacheUtil.getRedisConnection()
r_server.hmset(hashName, hashValue)
CacheUtil.disconnectRedisConnection(r_server)
@staticmethod
def getTrackerDetailsByID(trackerId):
trackerDetail = None
r_server = None
hashName = "tDetails:" + str(trackerId)
try:
if trackerId is not None:
print("getTrackerDetailsByID ")
r_server = CacheUtil.getRedisConnection()
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()))
finally:
CacheUtil.disconnectRedisConnection(r_server)
return trackerDetail
@staticmethod
def getRedisConnection():
print("Get Redis Connection on Util ")
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)
return r_server;
@staticmethod
def disconnectRedisConnection(r_server):
if r_server is not None and r_server:
r_server.connection.disconnect()