cache encrypt_table to reslove perfermance issuse on poor device

This commit is contained in:
Brook 2013-04-19 12:32:14 +08:00
parent b8cb1a3e97
commit 0b80b786c4

View file

@ -43,6 +43,7 @@ import hashlib
import os import os
import logging import logging
import getopt import getopt
import time, zlib
def get_table(key): def get_table(key):
m = hashlib.md5() m = hashlib.md5()
@ -152,6 +153,16 @@ class Socks5Server(SocketServer.StreamRequestHandler):
except socket.error, e: except socket.error, e:
logging.warn(e) logging.warn(e)
def get_cached_table(key):
cacheFile = str(zlib.crc32(key))
if os.path.exists(cacheFile):
with open(cacheFile, 'r') as f:
table = f.read()
else:
table = ''.join(get_table(KEY))
with open(cacheFile, 'w') as f:
f.write(table)
return table
if __name__ == '__main__': if __name__ == '__main__':
os.chdir(os.path.dirname(__file__) or '.') os.chdir(os.path.dirname(__file__) or '.')
@ -182,7 +193,10 @@ if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
encrypt_table = ''.join(get_table(KEY)) start = time.time()
encrypt_table = get_cached_table(KEY)
logging.info('Elapsed %.2fs ' % (time.time() - start))
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
try: try:
server = ThreadingTCPServer(('', PORT), Socks5Server) server = ThreadingTCPServer(('', PORT), Socks5Server)