Skip to content

Commit

Permalink
Improve cache utilization of pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
danlamanna committed Sep 11, 2024
1 parent 5c16a2c commit 903476e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion isic/core/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions isic/ingest/tests/test_api_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 903476e

Please sign in to comment.