kitware-resonant / django-resonant-utils

Django utilities for data management applications.
Apache License 2.0
2 stars 1 forks source link

Considering add a cached boto storage #27

Open danlamanna opened 3 years ago

danlamanna commented 3 years ago

From https://stackoverflow.com/a/42912118:

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.

danlamanna commented 3 years ago

One concern here is if the app has a database cache enabled, would this use a query for every URL rendered on the page?

Edit: perhaps if using redis as a queue it could easily be used as the default cache here.