Browse Source

add caching

main
Julia Luna 2 years ago
parent
commit
6219a3bdd8
Signed by: xenua
GPG Key ID: 6A0C04FA9A7D7582
  1. 16
      scopedsites/middleware.py
  2. 2
      setup.py

16
scopedsites/middleware.py

@ -9,7 +9,8 @@ class DomainScopeMiddleware: @@ -9,7 +9,8 @@ class DomainScopeMiddleware:
self.get_response = get_response
def __call__(self, request):
d = Domain.get_from_request(request)
d = Domain.get_from_request(request) # technically does hit the db layer, but should get caught in memcache
# TODO: performance testing
with scope(domain=d):
response = self.get_response(request)
@ -20,11 +21,18 @@ class DomainScopeMiddleware: @@ -20,11 +21,18 @@ class DomainScopeMiddleware:
class DomainAutoCreateMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.cache = set()
def ensure_exists(self, r):
if (host := r.META.get("HTTP_HOST")) in self.cache:
return
def __call__(self, request):
try:
Domain.get_from_request(request)
Domain.get_from_request(r)
self.cache.add(host)
except Http404:
Domain.objects.create(fqdn=request.META.get('HTTP_HOST'))
Domain.objects.create(fqdn=r.META.get('HTTP_HOST'))
def __call__(self, request):
self.ensure_exists(request)
return self.get_response(request)

2
setup.py

@ -3,7 +3,7 @@ from setuptools import setup @@ -3,7 +3,7 @@ from setuptools import setup
setup(
name="django_scopedsites",
packages=["scopedsites", "scopedsites.migrations"],
version="0.1.2",
version="0.1.3",
description="django_scopes x django.contrib.sites",
python_requires=">=3.6",
install_requires=[

Loading…
Cancel
Save