cache_manager.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 flask import Flask
  18. from flask_caching import Cache
  19. from superset.typing import CacheConfig
  20. class CacheManager:
  21. def __init__(self) -> None:
  22. super().__init__()
  23. self._tables_cache = None
  24. self._cache = None
  25. def init_app(self, app: Flask) -> None:
  26. self._cache = self._setup_cache(app, app.config["CACHE_CONFIG"])
  27. self._tables_cache = self._setup_cache(
  28. app, app.config["TABLE_NAMES_CACHE_CONFIG"]
  29. )
  30. @staticmethod
  31. def _setup_cache(app: Flask, cache_config: CacheConfig) -> Cache:
  32. """Setup the flask-cache on a flask app"""
  33. if isinstance(cache_config, dict):
  34. return Cache(app, config=cache_config)
  35. # Accepts a custom cache initialization function, returning an object compatible
  36. # with Flask-Caching API.
  37. return cache_config(app)
  38. @property
  39. def tables_cache(self) -> Cache:
  40. return self._tables_cache
  41. @property
  42. def cache(self) -> Cache:
  43. return self._cache