Fixed #31728 -- Fixed cache culling when no key is found for deletion.

DatabaseCache._cull implementation could fail if no key was found to
perform a deletion in the table. This prevented the new cache key/value
from being correctly added.
This commit is contained in:
Guillermo Bonvehí 2020-06-20 04:33:24 -03:00 committed by Mariusz Felisiak
parent 27c09043da
commit f386454d13
2 changed files with 20 additions and 3 deletions

View File

@ -267,9 +267,12 @@ class DatabaseCache(BaseDatabaseCache):
cursor.execute( cursor.execute(
connection.ops.cache_key_culling_sql() % table, connection.ops.cache_key_culling_sql() % table,
[cull_num]) [cull_num])
cursor.execute("DELETE FROM %s " last_cache_key = cursor.fetchone()
"WHERE cache_key < %%s" % table, if last_cache_key:
[cursor.fetchone()[0]]) cursor.execute(
'DELETE FROM %s WHERE cache_key < %%s' % table,
[last_cache_key[0]],
)
def clear(self): def clear(self):
db = router.db_for_write(self.cache_model_class) db = router.db_for_write(self.cache_model_class)

14
tests/cache/tests.py vendored
View File

@ -621,6 +621,20 @@ class BaseCacheTests:
def test_zero_cull(self): def test_zero_cull(self):
self._perform_cull_test('zero_cull', 50, 19) self._perform_cull_test('zero_cull', 50, 19)
def test_cull_delete_when_store_empty(self):
try:
cull_cache = caches['cull']
except InvalidCacheBackendError:
self.skipTest("Culling isn't implemented.")
old_max_entries = cull_cache._max_entries
# Force _cull to delete on first cached record.
cull_cache._max_entries = -1
try:
cull_cache.set('force_cull_delete', 'value', 1000)
self.assertIs(cull_cache.has_key('force_cull_delete'), True)
finally:
cull_cache._max_entries = old_max_entries
def _perform_invalid_key_test(self, key, expected_warning): def _perform_invalid_key_test(self, key, expected_warning):
""" """
All the builtin backends should warn (except memcached that should All the builtin backends should warn (except memcached that should