enable caching for signed s3 urls

This commit is contained in:
vabene1111
2021-06-05 13:43:10 +02:00
parent 388ef32475
commit 4a8b50aeba
4 changed files with 23 additions and 2 deletions

View File

@@ -58,6 +58,7 @@ GUNICORN_MEDIA=0
# S3_SECRET_ACCESS_KEY=
# S3_BUCKET_NAME=
# S3_QUERYSTRING_AUTH=1 # default true, set to 0 to serve media from a public bucket without signed urls
# AWS_QUERYSTRING_EXPIRE=3600 # number of seconds querystring are valid for
# S3_ENDPOINT_URL= # when using a custom endpoint like minio
# Email Settings, see https://docs.djangoproject.com/en/3.2/ref/settings/#email-host

View File

@@ -0,0 +1,19 @@
import hashlib
from django.conf import settings
from django.core.cache import cache
from storages.backends.s3boto3 import S3Boto3Storage
class CachedS3Boto3Storage(S3Boto3Storage):
def url(self, name, **kwargs):
key = hashlib.md5(f'recipes_media_urls_{name}'.encode('utf-8')).hexdigest()
if result := cache.get(key):
return result
result = super(CachedS3Boto3Storage, self).url(name, **kwargs)
timeout = int(settings.AWS_QUERYSTRING_EXPIRE * .95)
cache.set(key, result, timeout)
return result

View File

@@ -27,7 +27,7 @@ def search_recipes(request, queryset, params):
last_viewed_recipes = ViewLog.objects.filter(created_by=request.user, space=request.space,
created_at__gte=datetime.now() - timedelta(days=14)).order_by('pk').values_list('recipe__pk', flat=True).distinct()
return queryset.filter(pk__in=last_viewed_recipes[len(last_viewed_recipes)-search_last_viewed:])
return queryset.filter(pk__in=last_viewed_recipes[len(last_viewed_recipes) - min(len(last_viewed_recipes), search_last_viewed):])
queryset = queryset.annotate(
new_recipe=Case(When(created_at__gte=(datetime.now() - timedelta(days=7)), then=Value(100)),

View File

@@ -300,12 +300,13 @@ STATIC_URL = os.getenv('STATIC_URL', '/static/')
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
if os.getenv('S3_ACCESS_KEY', ''):
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'cookbook.helper.CustomStorageClass.CachedS3Boto3Storage'
AWS_ACCESS_KEY_ID = os.getenv('S3_ACCESS_KEY', '')
AWS_SECRET_ACCESS_KEY = os.getenv('S3_SECRET_ACCESS_KEY', '')
AWS_STORAGE_BUCKET_NAME = os.getenv('S3_BUCKET_NAME', '')
AWS_QUERYSTRING_AUTH = bool(int(os.getenv('S3_QUERYSTRING_AUTH', True)))
AWS_QUERYSTRING_EXPIRE = int(os.getenv('S3_QUERYSTRING_EXPIRE', 3600))
if os.getenv('S3_ENDPOINT_URL', ''):
AWS_S3_ENDPOINT_URL = os.getenv('S3_ENDPOINT_URL', '')