on master help sign in
[mod] use cache in _match_language function to speed up searx start time significantly
7e201cbDalf
searx/utils.py+15−6
@@ -47,6 +47,8 @@ blocked_tags = ('script',
47useragents = json.loads(open(os.path.dirname(os.path.realpath(__file__))
47useragents = json.loads(open(os.path.dirname(os.path.realpath(__file__))
48 + "/data/useragents.json", 'r', encoding='utf-8').read())
48 + "/data/useragents.json", 'r', encoding='utf-8').read())
49
49
50+lang_to_lc_cache = dict()
51+
50
52
51def searx_useragent():
53def searx_useragent():
52 return 'searx/{searx_version} {suffix}'.format(
54 return 'searx/{searx_version} {suffix}'.format(
@@ -183,7 +185,7 @@ def get_resources_directory(searx_directory, subdirectory, resources_directory):
183 if not resources_directory:
185 if not resources_directory:
184 resources_directory = os.path.join(searx_directory, subdirectory)
186 resources_directory = os.path.join(searx_directory, subdirectory)
185 if not os.path.isdir(resources_directory):
187 if not os.path.isdir(resources_directory):
186− raise Exception(directory + " is not a directory")
188+ raise Exception(resources_directory + " is not a directory")
187 return resources_directory
189 return resources_directory
188
190
189
191
@@ -314,6 +316,17 @@ def is_valid_lang(lang):
314 return False
316 return False
315
317
316
318
319+def _get_lang_to_lc_dict(lang_list):
320+ key = str(lang_list)
321+ value = lang_to_lc_cache.get(key, None)
322+ if value is None:
323+ value = dict()
324+ for lc in lang_list:
325+ value.setdefault(lc.split('-')[0], lc)
326+ lang_to_lc_cache[key] = value
327+ return value
328+
329+
317# auxiliary function to match lang_code in lang_list
330# auxiliary function to match lang_code in lang_list
318def _match_language(lang_code, lang_list=[], custom_aliases={}):
331def _match_language(lang_code, lang_list=[], custom_aliases={}):
319 # replace language code with a custom alias if necessary
332 # replace language code with a custom alias if necessary
@@ -334,11 +347,7 @@ def _match_language(lang_code, lang_list=[], custom_aliases={}):
334 return new_code
347 return new_code
335
348
336 # try to get the any supported country for this language
349 # try to get the any supported country for this language
337− for lc in lang_list:
350+ return _get_lang_to_lc_dict(lang_list).get(lang_code, None)
338− if lang_code == lc.split('-')[0]:
339− return lc
340−
341− return None
342
351
343
352
344# get the language code from lang_list that best matches locale_code
353# get the language code from lang_list that best matches locale_code