Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Considering add a cached boto storage #27

Open
danlamanna opened this issue Jan 29, 2021 · 1 comment
Open

Considering add a cached boto storage #27

danlamanna opened this issue Jan 29, 2021 · 1 comment

Comments

@danlamanna
Copy link
Contributor

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
Copy link
Contributor Author

danlamanna commented Mar 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant