Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
My Site object cache
"""Cache of Site related settings
Site IDs and urlconfs are such that are testd on *all* requests thus they need
to be cached.
from django.conf import settings
from django.contrib.sites.models import Site
def get_urlconf(domain=""):
"""Get urlconf (by domain, if omitted the global ROOT_URLCONF)"""
return _CACHE_URLCONFS_BY_DOMAIN.get(domain, settings.ROOT_URLCONF)
def get_site_id(domain=""):
"""Get site ID (by domain, if omitted the global SITE_ID is fetched)"""
return _CACHE_SITE_IDS.get(domain, settings.SITE_ID)
def get_site(domain=""):
"""Get current Site (by domain, if omitted the global site is fetched)
.. note: Site *objects* are not cached, only site ids.
site_id = get_site_id(domain)
if site_id == settings.SITE_ID:
return Site.objects.get_current()
return Site.objects.get(pk=site_id)
def refresh_multidomain_sites(forced=False):
"""Refresh the cache of site IDs
:param forced: Force refresh
if not _CACHE_SITE_IDS or forced:
for s in Site.objects.all():
_CACHE_SITE_IDS[s.domain] =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment