Add setting to easily be able to disable the response caching

This commit is contained in:
Oliver Falk
2020-03-04 14:30:28 +01:00
parent 476bddb21c
commit b463f9f95a
2 changed files with 22 additions and 18 deletions

View File

@@ -194,5 +194,7 @@ CACHES = {
} }
# This is 5 minutes caching for generated/resized images, # This is 5 minutes caching for generated/resized images,
# so the sites don't hit ivatar so much # so the sites don't hit ivatar so much - it's what's set in the HTTP header
CACHE_IMAGES_MAX_AGE = 5 * 60 CACHE_IMAGES_MAX_AGE = 5 * 60
CACHE_RESPONSE = False

View File

@@ -23,7 +23,7 @@ from pydenticon5 import Pydenticon5
import pagan import pagan
from robohash import Robohash from robohash import Robohash
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE, CACHE_RESPONSE
from ivatar.settings import CACHE_IMAGES_MAX_AGE from ivatar.settings import CACHE_IMAGES_MAX_AGE
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
from . ivataraccount.models import pil_format, file_format from . ivataraccount.models import pil_format, file_format
@@ -57,13 +57,14 @@ def get_size(request, size=DEFAULT_AVATAR_SIZE):
class CachingHttpResponse(HttpResponse): class CachingHttpResponse(HttpResponse):
def __init__(self, uri, content=b'', content_type=None, status=200, reason=None, charset=None): def __init__(self, uri, content=b'', content_type=None, status=200, reason=None, charset=None):
caches['filesystem'].set(uri, { if CACHE_RESPONSE:
'content': content, caches['filesystem'].set(uri, {
'content_type': content_type, 'content': content,
'status': status, 'content_type': content_type,
'reason': reason, 'status': status,
'charset': charset 'reason': reason,
}) 'charset': charset
})
return super().__init__(content, content_type, status, reason, charset) return super().__init__(content, content_type, status, reason, charset)
class AvatarImageView(TemplateView): class AvatarImageView(TemplateView):
@@ -92,15 +93,16 @@ class AvatarImageView(TemplateView):
uri = request.build_absolute_uri() uri = request.build_absolute_uri()
# Check the cache first # Check the cache first
centry = caches['filesystem'].get(uri) if CACHE_RESPONSE:
if centry: centry = caches['filesystem'].get(uri)
# For DEBUG purpose only print('Cached entry for %s' % uri) if centry:
return HttpResponse( # For DEBUG purpose only print('Cached entry for %s' % uri)
centry['content'], return HttpResponse(
content_type=centry['content_type'], centry['content'],
status=centry['status'], content_type=centry['content_type'],
reason = centry['reason'], status=centry['status'],
charset = centry['charset']) reason = centry['reason'],
charset = centry['charset'])
# In case no digest at all is provided, return to home page # In case no digest at all is provided, return to home page
if not 'digest' in kwargs: if not 'digest' in kwargs: