Skip to content

Commit 4c2a637

Browse files
authored
Merge pull request #3190 from MartinSA04/cache_key_fix
fix cache key issue
2 parents 6bb0463 + b81eaf6 commit 4c2a637

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
88

99
- [#3080](https://github.com/plotly/dash/pull/3080) Fix docstring generation for components using single-line or nonstandard-indent leading comments
1010
- [#3103](https://github.com/plotly/dash/pull/3103) Fix Graph component becomes unresponsive if an invalid figure is passed
11+
- [#3190](https://github.com/plotly/dash/pull/3190) Fix issue with cache key generation by adding option to include triggered inputs. Fixes [#3189](https://github.com/plotly/dash/issues/3189)
1112

1213
## [2.18.2] - 2024-11-04
1314

dash/_callback.py

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def callback(
7070
cancel=None,
7171
manager=None,
7272
cache_args_to_ignore=None,
73+
cache_ignore_triggered=True,
7374
on_error: Optional[Callable[[Exception], Any]] = None,
7475
**_kwargs,
7576
):
@@ -139,6 +140,9 @@ def callback(
139140
with keyword arguments (Input/State provided in a dict),
140141
this should be a list of argument names as strings. Otherwise,
141142
this should be a list of argument indices as integers.
143+
:param cache_ignore_triggered:
144+
Whether to ignore which inputs triggered the callback when creating
145+
the cache.
142146
:param interval:
143147
Time to wait between the long callback update requests.
144148
:param on_error:
@@ -185,6 +189,8 @@ def callback(
185189
if cache_args_to_ignore:
186190
long_spec["cache_args_to_ignore"] = cache_args_to_ignore
187191

192+
long_spec["cache_ignore_triggered"] = cache_ignore_triggered
193+
188194
return register_callback(
189195
callback_list,
190196
callback_map,
@@ -393,11 +399,16 @@ def add_context(*args, **kwargs):
393399
job_id = flask.request.args.get("job")
394400
old_job = flask.request.args.getlist("oldJob")
395401

402+
cache_ignore_triggered = long.get("cache_ignore_triggered", True)
403+
396404
current_key = callback_manager.build_cache_key(
397405
func,
398406
# Inputs provided as dict is kwargs.
399407
func_args if func_args else func_kwargs,
400408
long.get("cache_args_to_ignore", []),
409+
None
410+
if cache_ignore_triggered
411+
else callback_ctx.get("triggered_inputs", []),
401412
)
402413

403414
if old_job:

dash/long_callback/managers/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_result(self, key, job):
5454
def get_updated_props(self, key):
5555
raise NotImplementedError
5656

57-
def build_cache_key(self, fn, args, cache_args_to_ignore):
57+
def build_cache_key(self, fn, args, cache_args_to_ignore, triggered):
5858
fn_source = inspect.getsource(fn)
5959

6060
if not isinstance(cache_args_to_ignore, (list, tuple)):
@@ -68,7 +68,7 @@ def build_cache_key(self, fn, args, cache_args_to_ignore):
6868
arg for i, arg in enumerate(args) if i not in cache_args_to_ignore
6969
]
7070

71-
hash_dict = dict(args=args, fn_source=fn_source)
71+
hash_dict = dict(args=args, fn_source=fn_source, triggered=triggered)
7272

7373
if self.cache_by is not None:
7474
# Caching enabled

dash/long_callback/managers/celery_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, celery_app, cache_by=None, expire=None):
2626
:param cache_by:
2727
A list of zero-argument functions. When provided, caching is enabled and
2828
the return values of these functions are combined with the callback
29-
function's input arguments and source code to generate cache keys.
29+
function's input arguments, triggered inputs and source code to generate cache keys.
3030
:param expire:
3131
If provided, a cache entry will be removed when it has not been accessed
3232
for ``expire`` seconds. If not provided, the lifetime of cache entries

dash/long_callback/managers/diskcache_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, cache=None, cache_by=None, expire=None):
2525
:param cache_by:
2626
A list of zero-argument functions. When provided, caching is enabled and
2727
the return values of these functions are combined with the callback
28-
function's input arguments and source code to generate cache keys.
28+
function's input arguments, triggered inputs and source code to generate cache keys.
2929
:param expire:
3030
If provided, a cache entry will be removed when it has not been accessed
3131
for ``expire`` seconds. If not provided, the lifetime of cache entries

0 commit comments

Comments
 (0)