New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: jinja #15019
docs: jinja #15019
Conversation
We would appreciate it if you could provide us with more info about this issue/pr! Please do not leave the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking this on, this was long overdue! The section on cache_key_wrapper
could be more precise. In addition, a description needs to be added to the PR.
**Skipping Cache** | ||
|
||
If you have enabled caching, you will need to chain the `{{ cache_key_wrapper() }}` | ||
function to avoid retrieving the | ||
same current_username or current_user_id for different users. | ||
|
||
Here's an example: | ||
|
||
- Skip cache when retrieving current user_id: `{{ cache_key_wrapper(current_user_id()) }}` | ||
- Skip cache when retrieving current username: `{{ cache_key_wrapper(current_username()) }}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache_key_wrapper
is nowadays applied by default to current_user_id
and current_username
(it can be disabled by calling them with add_to_cache_keys=False
). See here for more context:
superset/superset/jinja_context.py
Lines 105 to 131 in efd7007
def current_user_id(self, add_to_cache_keys: bool = True) -> Optional[int]: | |
""" | |
Return the user ID of the user who is currently logged in. | |
:param add_to_cache_keys: Whether the value should be included in the cache key | |
:returns: The user ID | |
""" | |
if hasattr(g, "user") and g.user: | |
if add_to_cache_keys: | |
self.cache_key_wrapper(g.user.get_id()) | |
return g.user.get_id() | |
return None | |
def current_username(self, add_to_cache_keys: bool = True) -> Optional[str]: | |
""" | |
Return the username of the user who is currently logged in. | |
:param add_to_cache_keys: Whether the value should be included in the cache key | |
:returns: The username | |
""" | |
if g.user and hasattr(g.user, "username"): | |
if add_to_cache_keys: | |
self.cache_key_wrapper(g.user.username) | |
return g.user.username | |
return None |
cache_key_wrapper
is therefore only needed when it's necessary to wrap your own custom function return values into the cache key. See here for more context:
superset/superset/jinja_context.py
Lines 133 to 148 in efd7007
def cache_key_wrapper(self, key: Any) -> Any: | |
""" | |
Adds values to a list that is added to the query object used for calculating a | |
cache key. | |
This is needed if the following applies: | |
- Caching is enabled | |
- The query is dynamically generated using a jinja template | |
- A `JINJA_CONTEXT_ADDONS` or similar is used as a filter in the query | |
:param key: Any value that should be considered when calculating the cache key | |
:return: the original value ``key`` passed to the function | |
""" | |
if self.extra_cache_keys is not None: | |
self.extra_cache_keys.append(key) | |
return key |
Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work!
* added Jinja macro documentation * fixed typo! * Update docs/src/pages/docs/installation/sql_templating.mdx Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com> * incorporated Villes feedback on cache keys * fixed section title Co-authored-by: Evan Rusackas <evan@preset.io> Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
* added Jinja macro documentation * fixed typo! * Update docs/src/pages/docs/installation/sql_templating.mdx Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com> * incorporated Villes feedback on cache keys * fixed section title Co-authored-by: Evan Rusackas <evan@preset.io> Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
* added Jinja macro documentation * fixed typo! * Update docs/src/pages/docs/installation/sql_templating.mdx Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com> * incorporated Villes feedback on cache keys * fixed section title Co-authored-by: Evan Rusackas <evan@preset.io> Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
Adding some basic documentation on Jinja templating and how to use it!