Coverage for cookbook/helper/CustomStorageClass.py: 46%

13 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1import hashlib 

2 

3from django.conf import settings 

4from django.core.cache import cache 

5from storages.backends.s3boto3 import S3Boto3Storage 

6 

7 

8class CachedS3Boto3Storage(S3Boto3Storage): 

9 def url(self, name, **kwargs): 

10 key = hashlib.md5(f'recipes_media_urls_{name}'.encode('utf-8')).hexdigest() 

11 if result := cache.get(key): 

12 return result 

13 

14 result = super(CachedS3Boto3Storage, self).url(name, **kwargs) 

15 

16 timeout = int(settings.AWS_QUERYSTRING_EXPIRE * .95) 

17 cache.set(key, result, timeout) 

18 

19 return result