Cache sorting of countries

This commit is contained in:
Raphael Michel
2020-04-17 13:21:13 +02:00
parent e70f593a94
commit b6d2f67c7c
3 changed files with 41 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
from django.core.cache import cache
from django.utils.translation import get_language
from django_countries import Countries
class CachedCountries(Countries):
_cached_lists = {}
def __iter__(self):
"""
Iterate through countries, sorted by name, but cache the results based on the locale.
django-countries performs a unicode-aware sorting based on pyuca which is incredibly
slow.
"""
cache_key = "countries:all:{}".format(get_language())
if cache_key in self._cached_lists:
yield from self._cached_lists[cache_key]
return
val = cache.get(cache_key)
if val:
self._cached_lists[cache_key] = val
yield from val
return
val = list(super().__iter__())
self._cached_lists[cache_key] = val
cache.set(cache_key, val, 3600 * 24 * 30)
yield from val