villebro commented on a change in pull request #18976:
URL: https://github.com/apache/superset/pull/18976#discussion_r817583020
##########
File path: superset/utils/cache_manager.py
##########
@@ -14,73 +14,78 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+import logging
+import math
+
from flask import Flask
+from flask_babel import gettext as _
from flask_caching import Cache
+from superset.typing import CacheConfig
+
+logger = logging.getLogger(__name__)
+
class CacheManager:
def __init__(self) -> None:
super().__init__()
- self._cache = Cache()
- self._data_cache = Cache()
- self._thumbnail_cache = Cache()
- self._filter_state_cache = Cache()
- self._explore_form_data_cache = Cache()
+ self._default_cache_config: CacheConfig = {}
+ self.cache = Cache()
+ self.data_cache = Cache()
+ self.thumbnail_cache = Cache()
+ self.filter_state_cache = Cache()
+ self.explore_form_data_cache = Cache()
+
+ def _init_cache(
+ self, app: Flask, cache: Cache, cache_config_key: str, required: bool
= False
+ ) -> None:
+ config = {**self._default_cache_config, **app.config[cache_config_key]}
+ if required and config["CACHE_TYPE"] in ("null", "NullCache"):
+ raise Exception(
+ _(
+ "The CACHE_TYPE `%(cache_type)s` for
`%(cache_config_key)s` is not "
+ "supported. It is recommended to use `RedisCache`,
`MemcachedCache` "
+ "or another dedicated caching backend for production
deployments",
+ cache_type=config["CACHE_TYPE"],
+ cache_config_key=cache_config_key,
+ ),
+ )
+ cache.init_app(app, config)
def init_app(self, app: Flask) -> None:
- self._cache.init_app(
- app,
- {
- "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"],
- **app.config["CACHE_CONFIG"],
- },
- )
- self._data_cache.init_app(
- app,
- {
- "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"],
- **app.config["DATA_CACHE_CONFIG"],
- },
- )
- self._thumbnail_cache.init_app(
- app,
- {
- "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"],
- **app.config["THUMBNAIL_CACHE_CONFIG"],
- },
- )
- self._filter_state_cache.init_app(
- app,
- {
- "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"],
- **app.config["FILTER_STATE_CACHE_CONFIG"],
- },
+ if app.debug:
+ self._default_cache_config = {
+ "CACHE_TYPE": "SimpleCache",
+ "CACHE_THRESHOLD": math.inf,
+ }
+ else:
+ self._default_cache_config = {}
+
+ default_timeout = app.config.get("CACHE_DEFAULT_TIMEOUT")
+ if default_timeout is not None:
+ self._default_cache_config["CACHE_DEFAULT_TIMEOUT"] =
default_timeout
+ logger.warning(
+ _(
+ "The global config flag `CACHE_DEFAULT_TIMEOUT` has been "
+ "deprecated and will be removed in Superset 2.0. Please
set "
+ "default cache options in the `DEFAULT_CACHE_CONFIG`
parameter"
+ ),
+ )
+ self._default_cache_config = {
+ **self._default_cache_config,
+ **app.config["DEFAULT_CACHE_CONFIG"],
+ }
+
+ self._init_cache(app, self.cache, "CACHE_CONFIG")
+ self._init_cache(app, self.data_cache, "DATA_CACHE_CONFIG")
+ self._init_cache(app, self.thumbnail_cache, "THUMBNAIL_CACHE_CONFIG")
+ self._init_cache(
+ app, self.filter_state_cache, "FILTER_STATE_CACHE_CONFIG",
required=True
)
- self._explore_form_data_cache.init_app(
+ self._init_cache(
app,
- {
- "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"],
- **app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"],
- },
+ self.explore_form_data_cache,
+ "EXPLORE_FORM_DATA_CACHE_CONFIG",
+ required=True,
)
-
- @property
- def data_cache(self) -> Cache:
- return self._data_cache
-
- @property
- def cache(self) -> Cache:
- return self._cache
-
- @property
- def thumbnail_cache(self) -> Cache:
- return self._thumbnail_cache
-
- @property
- def filter_state_cache(self) -> Cache:
- return self._filter_state_cache
-
- @property
- def explore_form_data_cache(self) -> Cache:
- return self._explore_form_data_cache
Review comment:
This seemed very Java getter/setter-like with little utility (I assume a
remnant from old times)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]