MODELS_NAMES = [ "defectenhancementtask", "component", "needsdiagnosis", "regression", "stepstoreproduce", "spambug", "testlabelselect", "testgroupselect", ] DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600 # A week redis = Redis.from_url(os.environ.get("REDIS_URL", "redis://localhost/0")) MODEL_CACHE: ReadthroughTTLCache[str, Model] = ReadthroughTTLCache( timedelta(hours=1), lambda m: Model.load(f"{m}model")) MODEL_CACHE.start_ttl_thread() cctx = zstandard.ZstdCompressor(level=10) def setkey(key: str, value: bytes, compress: bool = False) -> None: LOGGER.debug(f"Storing data at {key}: {value!r}") if compress: value = cctx.compress(value) redis.set(key, value) redis.expire(key, DEFAULT_EXPIRATION_TTL) def classify_bug(model_name: str, bug_ids: Sequence[int], bugzilla_token: str) -> str:
LOGGER = logging.getLogger() MODELS_NAMES = [ "defectenhancementtask", "component", "regression", "stepstoreproduce", "spambug", "testlabelselect", "testgroupselect", ] DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600 # A week redis = Redis.from_url(os.environ.get("REDIS_URL", "redis://localhost/0")) MODEL_CACHE: ReadthroughTTLCache[str, Model] = ReadthroughTTLCache( timedelta(hours=1), load_model) MODEL_CACHE.start_ttl_thread() def setkey(key, value, expiration=DEFAULT_EXPIRATION_TTL): LOGGER.debug(f"Storing data at {key}: {value}") redis.set(key, value) if expiration > 0: redis.expire(key, expiration) def classify_bug(model_name, bug_ids, bugzilla_token): from bugbug_http.app import JobInfo # This should be called in a process worker so it should be safe to set
def test_cache_ttl_refreshes_after_get(): mockdatetime = MockDatetime(datetime(2019, 4, 1, 10)) cache = ReadthroughTTLCache(timedelta(hours=2), lambda x: "payload") with patch("datetime.datetime", mockdatetime): cache.get("key_a", force_store=True) # after one hour mockdatetime.set_now(datetime(2019, 4, 1, 11)) cache.purge_expired_entries() assert "key_a" in cache assert cache.get("key_a") == "payload" # after three hours mockdatetime.set_now(datetime(2019, 4, 1, 13)) cache.purge_expired_entries() assert "key_a" in cache # after three hours one minute mockdatetime.set_now(datetime(2019, 4, 1, 13, 1)) cache.purge_expired_entries() assert "key_a" not in cache