Skip to content

Commit 75effae

Browse files
committed
Clarify
1 parent 8ce2503 commit 75effae

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/sentry/utils/function_cache.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,20 @@ def build_batched_cache_func[
202202
"""
203203

204204
def inner(args: list[Key]) -> list[R]:
205-
result = []
206-
missing_indexes = []
207-
for i, arg in enumerate(args):
208-
cache_key = cache_key_for_batched_func(func_to_cache, arg)
209-
cached_val = cache.get(cache_key, None)
210-
if cached_val is None:
211-
missing_indexes.append(i)
212-
result.append(cached_val)
213-
if missing_indexes:
214-
timeout = cache_ttl.total_seconds()
215-
missing_keys = [args[i] for i in missing_indexes]
216-
missing_vals = func_to_cache(missing_keys)
217-
if len(missing_vals) != len(missing_keys):
205+
cache_keys = [cache_key_for_batched_func(func_to_cache, arg) for arg in args]
206+
values = cache.get_many(cache_keys)
207+
missing_keys = [key for key in cache_keys if key not in values]
208+
if missing_keys:
209+
key_to_arg = {key: arg for arg, key in zip(args, cache_keys)}
210+
missing_args = [key_to_arg[key] for key in missing_keys]
211+
missing_vals = func_to_cache(missing_args)
212+
if len(missing_vals) != len(missing_args):
218213
raise ValueError(
219214
f"Function {func_to_cache.__qualname__} returned {len(missing_vals)} results for {len(missing_keys)} keys"
220215
)
221-
for out_idx, key, val in zip(missing_indexes, missing_keys, missing_vals):
222-
result[out_idx] = val
223-
cache_key = cache_key_for_batched_func(func_to_cache, key)
224-
cache.set(cache_key, val, timeout=timeout)
225-
return result
216+
fetched = dict(zip(missing_keys, missing_vals))
217+
cache.set_many(fetched, timeout=cache_ttl.total_seconds())
218+
values.update(fetched)
219+
return [values[key] for key in cache_keys]
226220

227221
return inner

src/sentry/workflow_engine/processors/data_condition_group.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ def get_data_conditions_for_groups(
4949
by_id = DefaultDict[int, list[DataCondition]](list)
5050
for cond in DataCondition.objects.filter(condition_group_id__in=data_condition_group_ids):
5151
by_id[cond.condition_group_id].append(cond)
52-
result = [by_id[dcg] for dcg in data_condition_group_ids]
53-
assert len(result) == len(data_condition_group_ids)
54-
return result
52+
return [by_id[dcg] for dcg in data_condition_group_ids]
5553

5654

5755
def batch_get_slow_conditions(

tests/sentry/utils/test_function_cache.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ def batch_arg_extractor(instance: CacheModel) -> str:
131131
mock_test_func
132132
)
133133

134+
# Empty list should work fine.
135+
assert decorated_test_func([]) == []
136+
134137
# First call should hit the function
135138
self.assert_called_with_count(mock_test_func, ["test"], 0)
136139
assert decorated_test_func(["test"]) == [0]
@@ -140,6 +143,10 @@ def batch_arg_extractor(instance: CacheModel) -> str:
140143
assert decorated_test_func(["test"]) == [0]
141144
self.assert_called_with_count(mock_test_func, ["test"], 1)
142145

146+
# Duplicate key should work fine.
147+
assert decorated_test_func(["test", "test"]) == [0, 0]
148+
self.assert_called_with_count(mock_test_func, ["test"], 1)
149+
143150
# Create a model, should invalidate cache
144151
CacheModel.objects.create(some_field="test")
145152
assert decorated_test_func(["test"]) == [1]

0 commit comments

Comments
 (0)