mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 12:18:45 -05:00
enable caching for signed s3 urls
This commit is contained in:
19
cookbook/helper/CustomStorageClass.py
Normal file
19
cookbook/helper/CustomStorageClass.py
Normal 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
|
||||
Reference in New Issue
Block a user