当文件从 VirtualPathProvider 作为 Stream 返回时维护文件的缓存控制属性

发布于 2024-12-17 12:41:24 字数 860 浏览 1 评论 0原文

我已经实现了 VirtualPathProvider,以从 Azure CDN 返回 Azure 网站的主题文件(图像、css)。除了一件事之外它工作得很好:来自 CDN 的文件都将其缓存控制属性设置为“私有”,因此永远不会被缓存。

实际的 blob 的属性设置正确,如果我通过直接 URL(即不通过 VPP)访问它,则缓存控制是正确的。

问题似乎出在 VirtualFile 类的 Open() 方法中,我必须实现该方法才能将文件作为流返回?

public override Stream Open()
    {
        CloudBlobClient client = new CloudBlobClient(cdnURL);
        CloudBlob blob = client.GetBlobReference(blobURL);
        blob.FetchAttributes();
        MemoryStream stream = new MemoryStream();
        BlobRequestOptions options = new BlobRequestOptions();
        options.BlobListingDetails = BlobListingDetails.Metadata;
        blob.DownloadToStream(stream,options);
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }

对此进行搜索,我发现大多数人都以另一种方式遇到问题 - 即文件在他们不希望被缓存时被缓存。但是,我找到的所有示例都没有从另一个 URL 引用文件。它们似乎都使用数据库或只是不同的物理路径。

I have implemented a VirtualPathProvider to return Theme files (images,css) for an Azure web site from the Azure CDN. It is working fine apart from one thing: the files that are coming from the CDN all have their cache control property set to "private" and so are never cached.

The actual blobs have their properties set correctly and if I access one by it's direct URL (i.e. not through the VPP) then the cache control is correct.

The problem seems to be in the Open() method of the VirtualFile class I have to implement to return the file as a Stream?

public override Stream Open()
    {
        CloudBlobClient client = new CloudBlobClient(cdnURL);
        CloudBlob blob = client.GetBlobReference(blobURL);
        blob.FetchAttributes();
        MemoryStream stream = new MemoryStream();
        BlobRequestOptions options = new BlobRequestOptions();
        options.BlobListingDetails = BlobListingDetails.Metadata;
        blob.DownloadToStream(stream,options);
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }

Searching on this and I find most people have the problem the other way - i.e. files are cached when they don't want them to be. However none of the examples I can find are referencing a file from another URL. They all seem to use databases or just different physical paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-12-24 12:41:24

感谢asp.net论坛上的这个答案 http://forums.asp.net/post/4716700 .aspx

我通过添加 Open() 方法解决了该问题:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.AppendCacheExtension("max-age=86400");

Thanks to this answer on the asp.net forum http://forums.asp.net/post/4716700.aspx

I have resolved the issue by adding in my Open() method:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.AppendCacheExtension("max-age=86400");
昔日梦未散 2024-12-24 12:41:24

我认为您可能错过了 CDN 如何获得优势的关键点。 CDN 通过将资源放置在更靠近请求文件的客户端的位置来提供帮助。即当客户端请求文件时,它会直接转到 CDN URL。这里发生的情况似乎是您将文件从 CDN 下载到运行 Web 服务器的代码,然后从那里将流返回到客户端。

如果我错了,请纠正我。

还值得注意的是,缓存属性不是您要返回的文件流的一部分,它们是可以在 CloudBlob.Properties.CacheControl 中找到的附加属性

I think you may have missed a crucial point in how the CDN gains it's advantage. The CDN helps by putting the resources closer to the client requesting the file. i.e when the client requests the file it goes straight to the CDN URL. What seems to be happening here is you're downloading the file from the CDN to you code that's running web server and then returning the stream to the client from there.

Please correct me if I'm wrong.

It's also worth noting that the cache properties are not part of the file stream that you're returning, they're additional properties that can be found in CloudBlob.Properties.CacheControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文