Cache models in the HTTP service workers only if they are used more than once in two hours

Part of #1342
This commit is contained in:
Marco Castelluccio 2020-03-01 16:18:42 +01:00
Родитель 4695acfad9
Коммит ececf8a58a
1 изменённых файлов: 12 добавлений и 3 удалений

Просмотреть файл

@ -6,10 +6,12 @@
import json import json
import logging import logging
import os import os
from datetime import datetime
from typing import Dict from typing import Dict
from urllib.request import urlretrieve from urllib.request import urlretrieve
import requests import requests
from dateutil.relativedelta import relativedelta
from redis import Redis from redis import Redis
from bugbug import bugzilla, get_bugbug_version from bugbug import bugzilla, get_bugbug_version
@ -34,6 +36,7 @@ BASE_URL = "https://community-tc.services.mozilla.com/api/index/v1/task/project.
DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600 # A week DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600 # A week
MODEL_LAST_LOADED: Dict[str, datetime] = {}
MODEL_CACHE: Dict[str, Model] = {} MODEL_CACHE: Dict[str, Model] = {}
@ -60,10 +63,16 @@ def get_model(model_name):
else: else:
raise raise
# Cache the model only if it was last used less than two hours ago.
if model_name in MODEL_LAST_LOADED and MODEL_LAST_LOADED[
model_name
] > datetime.now() - relativedelta(hours=2):
MODEL_CACHE[model_name] = model MODEL_CACHE[model_name] = model
return model else:
model = MODEL_CACHE[model_name]
return MODEL_CACHE[model_name] MODEL_LAST_LOADED[model_name] = datetime.now()
return model
def preload_models(): def preload_models():