开发者

Is it possible to use WebClient to retrieve a file from the browser cache without checking for a newer version of the file?

I am using an instance of WebClient in my Silverlight application to retrieve an image file for display. In order to service this application, I have set up a WCF REST service. The following is a snippet of code I wrote for that service:

DateTime modSince = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.IfModifiedSince] 
    == null 
    ? DateTime.MinValue 
    : DateTime.Parse(WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.IfModifiedSince].ToString());

FileInfo fInfo = new FileInfo(filePath);

if (fInfo.LastWriteTime > modSince)
{
    WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
    WebOperationContext.Current.OutgoingResponse.LastModified = fInfo.LastWriteTime;
}
else
{
    WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotModified;
}

The service开发者_运维技巧 is set up such that on a request for an image file, the if-modified-since attribute of the request header is examined. If this value is less than the last time the file is modified, the file is returned. Otherwise, a 304 message is returned.

We are interested in making the transaction faster. Since the images rarely, if ever, change it is desirable to look up the cache for a copy of the image before making a request to the server, which would save on network latency. In trying to implement this for IE, I have changed the Temporary Internet Files settings to "never check for newer versions of stored pages". When I enter in the browser's address bar the REST service URL for an image that has a copy stored in the cache, the copy is retrieved from the cache without the server being called. However, when the URL is entered as a parameter to the WebClient's OpenReadAsync method a GET request is sent to the server, leading to a 304 message.

Is there any way I could use the WebClient to look up the cache without a GET request being sent to the server?


You need to set the CahcePolicy on the WebClient:

WebClient wc = new WebClient();
wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.CacheIfAvailable);

This will use Microsoft.Win32.WinInetCache by default which is IE's cache manager.


UPDATE

OK, I did not realise it is SL. In that case, if the image is small I think it is easier to simplify things by retrieving from the server anyway. If the file is large, expose a method to return the date it was last changed and implement a local caching using Isolated Storage.


You can't prevent the request from code in Silverlight. The "never check" setting of IE is only used in the absence of cache control directives from the server, I would not recommend you use it.

I'm not sure why you are using WCF Rest to deliver images to the client instead of simply placing the images in a folder on the server and fetching them as standard static content?

Either way the correct approach to the your problem is for the server to specify how the images should be cached by including a Cache-Control header in the response. Something like "Cache-Control: max-age=86400".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜