eldarion / django-gapc-storage

a Django storage backend using GCS JSON API
10 stars 9 forks source link

Add option to specify cache headers #5

Closed jacobwegner closed 7 years ago

jacobwegner commented 8 years ago

By default, GCS caches any publicly accessible objects for 3600 seconds.

Especially if a developer is trying to update an existing object (see #4), it may not be desirable to have objects with that long of a cache value.

Either add an option to GoogleCloudStorage to specify cache headers or an additional backend that supports custom cache headers.


GCS_PUBLIC_READ_CACHE_DEFAULT = "public, max-age=3600"

...

class CustomCacheMediaStorage(GoogleCloudStorage):
    """
    Custom subclass of GoogleCloudStorage that allows customization of GCS's
    cache-control header
    """
    cache_control = GCS_PUBLIC_READ_CACHE_DEFAULT

    def _save(self, name, content):
        mimetype, _ = mimetypes.guess_type(os.path.basename(name))
        if mimetype is None:
            mimetype = "application/octet-stream"
        media = MediaIoBaseUpload(content, mimetype)
        req = self.client.objects().insert(
            bucket=self.bucket,
            name=self._prefixed_name(name),
            body={
                "cacheControl": self.cache_control
            },
            media_body=media
        )
        req.execute()
        return name

...

class NoCacheMediaStorage(CustomCacheMediaStorage):
    # never cache
    cache_control = "private, max-age=0"
jacobwegner commented 8 years ago

@brosner any thoughts about how we want to implement this? Perhaps add a GAPC_STORAGE key for cacheControl?

jacobwegner commented 7 years ago

@brosner: would like to revisit your thoughts on this and #4 for the next release

brosner commented 7 years ago

I am in favor of adding a setting to control this behavior. cache_control key in GAPC_STORAGE sounds right to me.