From 903476ee4d82037bce5a8970bb29259d815c32c4 Mon Sep 17 00:00:00 2001 From: Dan LaManna Date: Wed, 11 Sep 2024 10:46:57 -0400 Subject: [PATCH] Improve cache utilization of pagination --- isic/core/pagination.py | 3 ++- isic/ingest/tests/test_api_pagination.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/isic/core/pagination.py b/isic/core/pagination.py index 9ec63d7d..bb3e00c1 100644 --- a/isic/core/pagination.py +++ b/isic/core/pagination.py @@ -125,7 +125,8 @@ def paginate_queryset( # If we have an offset cursor then offset the entire page by that amount. # We also always fetch an extra item in order to determine if there is a # page following on from this one. - results = list(queryset[cursor.offset : cursor.offset + limit + 1]) + # Always fetch the maximum page size to increase cache utilization. + results = list(queryset[cursor.offset : cursor.offset + self.max_page_size + 1]) page = list(results[:limit]) # Determine the position of the final item following the page. diff --git a/isic/ingest/tests/test_api_pagination.py b/isic/ingest/tests/test_api_pagination.py index 928606e1..77c68980 100644 --- a/isic/ingest/tests/test_api_pagination.py +++ b/isic/ingest/tests/test_api_pagination.py @@ -38,3 +38,27 @@ def test_pagination(image_factory, staff_client): assert len(resp.json()["results"]) == 1 assert resp.json()["results"][0]["isic_id"] == images[1].isic_id assert resp.json()["previous"] is None + + +@pytest.fixture() +def _local_memory_cache(settings): + settings.CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + } + } + + +@pytest.mark.django_db() +@pytest.mark.usefixtures("_local_memory_cache") +def test_pagination_caches_correctly(image_factory, client, django_assert_num_queries): + for _ in range(2): + image_factory(public=True) + + # one query for the count and the other for the values + with django_assert_num_queries(2): + resp_1 = client.get("/api/v2/images/", data={"limit": 1}) + resp_2 = client.get("/api/v2/images/", data={"limit": 2}) + + assert resp_1.status_code == 200, resp_1.json() + assert resp_2.status_code == 200, resp_2.json()