Add lru_cache(None) to speed up function call

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2023-09-03 15:13:24 +08:00
parent 42d9fb1be5
commit 2c30ecb243
2 changed files with 10 additions and 6 deletions
+7 -6
View File
@@ -13,6 +13,7 @@ import re
import signal
import logging
import threading
import functools
import subprocess
logger = logging.getLogger(__name__)
@@ -82,6 +83,7 @@ class TorRelayStarter(AsyncSubprocessMessage):
logger.info(f'{TorRelay.name()} bootstrapped {percentage}%')
@functools.lru_cache(None)
def getTorRelayVersion():
try:
result = subprocess.run(
@@ -97,13 +99,12 @@ def getTorRelayVersion():
except Exception:
# Any non-exit exceptions
return '0.0.0'
CACHED_VERSION = getTorRelayVersion()
return TorRelay.VERSION_NOT_FOUND
class TorRelay(Core):
VERSION_NOT_FOUND = '0.0.0'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -111,7 +112,7 @@ class TorRelay(Core):
@staticmethod
def checkIfExists():
return CACHED_VERSION != '0.0.0'
return TorRelay.version() != TorRelay.VERSION_NOT_FOUND
@staticmethod
def name():
@@ -119,7 +120,7 @@ class TorRelay(Core):
@staticmethod
def version():
return CACHED_VERSION
return getTorRelayVersion()
@property
def bootstrapPercentage(self):
+3
View File
@@ -14,6 +14,7 @@ import os
import copy
import ujson
import queue
import ctypes
import logging
import pybase64
import threading
@@ -296,6 +297,7 @@ bootstrapIcon = functools.partial(icon, 'bootstrap')
bootstrapIconWhite = functools.partial(icon, 'bootstrap/white')
@functools.lru_cache(None)
def protocolRepr(protocol):
if protocol.lower() == 'vmess':
return Protocol.VMess
@@ -313,6 +315,7 @@ def getAbsolutePath(path):
return path if os.path.isabs(path) else str(ROOT_DIR / path)
@functools.lru_cache(None)
def getUbuntuRelease():
try:
result = subprocess.run(