def test_delete_many(self):
# Multiple keys can be deleted using delete_many
cache = self.cache
cache.set("key1", "spam")
cache.set("key2", "eggs")
cache.set("key3", "ham")
cache.delete_many(["key1", "key2"])
self.assertIsNone(cache.get("key1"))
self.assertIsNone(cache.get("key2"))
self.assertEqual(cache.get("key3"), "ham")
python类delete_many()的实例源码
def _clean_many(prefix):
from django.core.cache import cache
keys = []
if settings.USE_I18N:
for lang in [language[0] for language in settings.LANGUAGES]:
keys.append("%s-%s" %(prefix, lang))
else:
keys = ["%s-%s" %(prefix, settings.LANGUAGE_CODE)]
cache.delete_many(keys)
def test_delete_many(self):
await self.set('test', 'test-1')
await self.set('test2', 'test-2')
await cache.delete_many(('test-1', 'test-2'))
self.assertIsNone(await self.get('test-1'))
self.assertIsNone(await self.get('test-2'))
def invalidate_object_cache(cls, instance):
# TODO: Think about raise condition
# if new invalidation created when invalidating
# what will happen?
keys = cls.get_invalidation_keys_and_delete(instance)
cache.delete_many(keys)
def clear_all_profile_contexts(sender, instance, **kwargs):
# TODO(lucas): Review to improve performance
# Instead of clearing out all of the profile contexts, we could just clear out
# the profile contexts associated with the investors related to this revenue report
cache_keys = ['profile_context-{pk}'.format(pk=up.pk) for up in UserProfile.objects.all()]
cache.delete_many(cache_keys)
def invalidate_contest(contest: Contest):
contest_users = contest.participants_ids
cache.delete_many(list(map(lambda x: PARTICIPANT_RANK_DETAIL.format(contest=contest.pk, user=x), contest_users)))
cache.delete_many(
list(map(lambda x: PARTICIPANT_RANK_DETAIL_PRIVATE.format(contest=contest.pk, user=x), contest_users)))
cache.delete(PARTICIPANT_RANK_LIST.format(contest=contest.pk))
cache.delete(CONTEST_FIRST_YES.format(contest=contest.pk))
def invalidate_user(user_id, contest_id=0):
cache.delete_many([USER_TOTAL_COUNT.format(user=user_id, contest=contest_id),
USER_TOTAL_LIST.format(user=user_id, contest=contest_id),
USER_AC_COUNT.format(user=user_id, contest=contest_id),
USER_AC_DIFF_COUNT.format(user=user_id, contest=contest_id),
USER_AC_LIST.format(user=user_id, contest=contest_id)])