Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django/core/cache/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ def _cull(self, db, cursor, now, num):
)
deleted_count = cursor.rowcount
remaining_num = num - deleted_count
if remaining_num > self._max_entries:
cull_num = remaining_num // self._cull_frequency
cull_num = remaining_num // self._cull_frequency
if cull_num > 0 and remaining_num > self._max_entries:
cursor.execute(
connection.ops.cache_key_culling_sql() % table, [cull_num]
)
Expand Down
18 changes: 18 additions & 0 deletions tests/cache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,24 @@ def test_no_query_without_check(self):
)
self.assertEqual(num_count_queries, 0)

def test_delete_query_skipped_on_high_cull_frequency(self):
cull_delete_cache = caches["cull"]
old_cull_freq = cull_delete_cache._cull_frequency

# CULL_FREQUENCY > MAX_ENTRIES forces
# (remaining_num // CULL_FREQUENCY) to evaluate to zero (cull_num)
cull_delete_cache._cull_frequency = cull_delete_cache._max_entries * 2
self.addCleanup(setattr, cull_delete_cache, "_cull_frequency", old_cull_freq)

self._perform_cull_test("cull", 50, 49)

with CaptureQueriesContext(connection) as captured_queries:
cull_delete_cache.set("force_cull", "value")

# Only the expiration DELETE query runs; culling is skipped.
num_delete_queries = sum("DELETE" in query["sql"] for query in captured_queries)
self.assertEqual(num_delete_queries, 1)

def test_delete_cursor_rowcount(self):
"""
The rowcount attribute should not be checked on a closed cursor.
Expand Down
Loading