mirror of
https://github.com/barkeser2002/offline-db.git
synced 2026-09-25 05:20:06 +03:00
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Senpai-YoloBot <41898282+github-actions[bot]@users.noreply.github.com>
26 lines
1.2 KiB
Python
26 lines
1.2 KiB
Python
import re
|
|
|
|
with open('users/badge_system.py', 'r') as f:
|
|
content = f.read()
|
|
|
|
# Instead of passing the empty dict `strategy_cache` around during `check_badges(user)`, we can pre-populate it!
|
|
# Wait, actually, the problem is that multiple functions use `.objects.` calls inside `badge_system.py`.
|
|
|
|
# I should patch `users/services.py` directly to pass the `cache=strategy_cache` and let `badge_system.py` just use it.
|
|
# Actually, the instructions say "N+1 Query in users/services.py check_badges()", and point out:
|
|
# "Clear N+1 issue where DB queries are triggered inside strategy checks during the loop."
|
|
|
|
# Currently in `check_badges`:
|
|
# strategy_cache = {}
|
|
# for strategy in GENERAL_BADGE_STRATEGIES:
|
|
# strategy.check(user, awarded_slugs, all_badges, new_badges, cache=strategy_cache)
|
|
|
|
# And inside the strategies, things are lazy loaded.
|
|
# But `WatchLog.objects.filter(user=user)...` is being called multiple times across multiple strategies.
|
|
# If I pre-fetch these and put them in `strategy_cache`, the strategies can use them directly!
|
|
|
|
# BUT!
|
|
# "N+1 Query in users/services.py check_badges()"
|
|
# "DB queries are triggered inside strategy checks during the loop."
|
|
# The goal is to optimize `check_badges` and `check_chat_badges` in `services.py`.
|