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,6 +57,7 @@ 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):
if CACHE_RESPONSE:
caches['filesystem'].set(uri, { caches['filesystem'].set(uri, {
'content': content, 'content': content,
'content_type': content_type, 'content_type': content_type,
@@ -92,6 +93,7 @@ class AvatarImageView(TemplateView):
uri = request.build_absolute_uri() uri = request.build_absolute_uri()
# Check the cache first # Check the cache first
if CACHE_RESPONSE:
centry = caches['filesystem'].get(uri) centry = caches['filesystem'].get(uri)
if centry: if centry:
# For DEBUG purpose only print('Cached entry for %s' % uri) # For DEBUG purpose only print('Cached entry for %s' % uri)