init_cache.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import hashlib
  19. from tempfile import gettempdir
  20. from flask_caching import Cache
  21. from airflow.configuration import conf
  22. from airflow.exceptions import AirflowConfigException
  23. HASH_METHOD_MAPPING = {
  24. "md5": hashlib.md5,
  25. "sha1": hashlib.sha1,
  26. "sha224": hashlib.sha224,
  27. "sha256": hashlib.sha256,
  28. "sha384": hashlib.sha384,
  29. "sha512": hashlib.sha512,
  30. }
  31. def init_cache(app):
  32. webserver_caching_hash_method = conf.get(
  33. section="webserver", key="CACHING_HASH_METHOD", fallback="md5"
  34. ).casefold()
  35. cache_config = {"CACHE_TYPE": "flask_caching.backends.filesystem", "CACHE_DIR": gettempdir()}
  36. mapped_hash_method = HASH_METHOD_MAPPING.get(webserver_caching_hash_method)
  37. if mapped_hash_method is None:
  38. raise AirflowConfigException(
  39. f"Unsupported webserver caching hash method: `{webserver_caching_hash_method}`."
  40. )
  41. cache_config["CACHE_OPTIONS"] = {"hash_method": mapped_hash_method}
  42. Cache(app=app, config=cache_config)