import hashlib
from django.conf import settings
from django.core.cache import cache
from storages.backends.s3boto3 import S3Boto3Storage
class CachedS3Boto3Storage(S3Boto3Storage):
""" adds caching for temporary urls """
def url(self, name):
# Add a prefix to avoid conflicts with any other apps
key = hashlib.md5("CachedS3Boto3Storage_%s" % name).hexdigest()
result = cache.get(key)
if result:
return result
# No cached value exists, follow the usual logic
result = super(CachedS3Boto3Storage, self).url(name)
# Cache the result for 3/4 of the temp_url's lifetime.
try:
timeout = settings.AWS_QUERYSTRING_EXPIRE
except:
timeout = 3600
timeout = int(timeout*.75)
cache.set(key, result, timeout)
return result
When viewing galleries of files, every page load uses a newly signed URL so it can't be cached. This makes navigating page to page a bit laggier. This storage would return the same signed url for 3/4 of the lifetime of the signature.
From https://stackoverflow.com/a/42912118:
When viewing galleries of files, every page load uses a newly signed URL so it can't be cached. This makes navigating page to page a bit laggier. This storage would return the same signed url for 3/4 of the lifetime of the signature.