Skip to content
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

Cache problem fix #2268

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CTFd/api/v1/scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_restx import Namespace, Resource
from sqlalchemy import select

from CTFd.cache import cache, make_cache_key
from CTFd.cache import cache, make_cache_key, make_cache_key_with_args
from CTFd.models import Awards, Solves, Users, db
from CTFd.utils import get_config
from CTFd.utils.dates import isoformat, unix_time_to_utc
Expand Down Expand Up @@ -82,7 +82,7 @@ def get(self):
class ScoreboardDetail(Resource):
@check_account_visibility
@check_score_visibility
@cache.cached(timeout=60, key_prefix=make_cache_key)
@cache.cached(timeout=60, key_prefix=make_cache_key_with_args)
def get(self, count):
response = {}

Expand Down
18 changes: 17 additions & 1 deletion CTFd/cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ def make_cache_key(path=None, key_prefix="view/%s"):
return cache_key


def make_cache_key_with_args(path=None, key_prefix="view/%s", view_args=None):
"""
This function ensures that view arguments are taken into account when generating a cache key
:param path:
:param key_prefix:
:return:
"""
if path is None:
path = request.endpoint
if view_args is None:
view_args = request.view_args
for k, v in view_args.items():
path += f'_{k}_{v}'
return make_cache_key(path, key_prefix)


def clear_config():
from CTFd.utils import _get_config, get_app_config

Expand Down Expand Up @@ -91,7 +107,7 @@ def clear_standings():

# Clear out HTTP request responses
cache.delete(make_cache_key(path=api.name + "." + ScoreboardList.endpoint))
cache.delete(make_cache_key(path=api.name + "." + ScoreboardDetail.endpoint))
cache.delete(make_cache_key_with_args(path=api.name + "." + ScoreboardDetail.endpoint, view_args={"count": 10}))
cache.delete_memoized(ScoreboardList.get)

# Clear out scoreboard templates
Expand Down
6 changes: 3 additions & 3 deletions tests/api/v1/test_scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def test_scoreboard_is_cached():
with login_as_user(app, "user1") as client:
# No cached data
assert app.cache.get("view/api.scoreboard_scoreboard_list") is None
assert app.cache.get("view/api.scoreboard_scoreboard_detail") is None
assert app.cache.get("view/api.scoreboard_scoreboard_detail_count_10") is None

# Load and check cached data
client.get("/api/v1/scoreboard")
assert app.cache.get("view/api.scoreboard_scoreboard_list")
client.get("/api/v1/scoreboard/top/10")
assert app.cache.get("view/api.scoreboard_scoreboard_detail")
assert app.cache.get("view/api.scoreboard_scoreboard_detail_count_10")

# Check scoreboard page
assert (
Expand All @@ -55,7 +55,7 @@ def test_scoreboard_is_cached():
# Empty standings and check that the cached data is gone
clear_standings()
assert app.cache.get("view/api.scoreboard_scoreboard_list") is None
assert app.cache.get("view/api.scoreboard_scoreboard_detail") is None
assert app.cache.get("view/api.scoreboard_scoreboard_detail_count_10") is None
assert (
app.cache.get(make_template_fragment_key("public_scoreboard_table"))
is None
Expand Down