Sunday, 15 April 2012

Client side caching using Last-Modified header and OutputCacheAttribute in asp.net mvc 3 -



Client side caching using Last-Modified header and OutputCacheAttribute in asp.net mvc 3 -

edited

i want cache images on client , know there different ways in mvc 3: (correct me if i'm wrong)

1) can utilize outputcacheattribute works help of expires http header. homecoming 304 not modified unless time expire (even if image changed).

2) avoid displaing stale images can utilize last-modified http header (with outputcacheattribute). in case browser sends request server if-modified-since http header. on server verify whether object still valid or not , if homecoming last-modified http header (and browser takes image local cache); if object modified homecoming 200 ok status. so, browser needs send request server each time before taking image it's own cache. here example -

3) there way (as told right way in case, cause images alter rarely... anyway, need implement this): add together modified date image url , set caching expires eternity (1 year or more). if image have changed should send new url new version.

here code:

public class lastmodifiedcacheattribute : actionfilterattribute { public override void onactionexecuted(actionexecutedcontext filtercontext) { if (filtercontext.result filepathresult) { var result = (filepathresult)filtercontext.result; var lastmodify = file.getlastwritetime(result.filename); if (!hasmodification(filtercontext.requestcontext, lastmodify)) filtercontext.result = notmodified(filtercontext.requestcontext, lastmodify); setlastmodifieddate(filtercontext.requestcontext, lastmodify); } base.onactionexecuted(filtercontext); } private static void setlastmodifieddate(requestcontext requestcontext, datetime modificationdate) { requestcontext.httpcontext.response.cache.setlastmodified(modificationdate); } private static bool hasmodification(requestcontext context, datetime modificationdate) { var headervalue = context.httpcontext.request.headers["if-modified-since"]; if (headervalue == null) homecoming true; var modifiedsince = datetime.parse(headervalue).tolocaltime(); homecoming modifiedsince < modificationdate; } private static actionresult notmodified(requestcontext response, datetime lastmodificationdate) { response.httpcontext.response.cache.setlastmodified(lastmodificationdate); homecoming new httpstatuscoderesult(304, "page has not been modified"); } }

and registered lastmodifiedcacheattribute in global.asax , applied next outputcacheattribute action method.

[httpget, outputcache(duration = 3600, location = outputcachelocation.client, varybyparam = "productid")] public filepathresult getimage(int productid) { // code }

if utilize code above seems browser doesn't send requests server, instead take images cache unless duration not ended. (when alter image browser doesn't display new version)

questions:

1) how implement 3rd approach, browser take images client cache (and not send response server each time wants image) unless image modified? edited: actual code appreciated.

2) in code above time of first image request written last-modified (don't know why). how write modification date of file last-modified? edited: question relates sec approach. also, if cache on client , utilize last-modified implementation 304 not modified status if press f5. if reenter same url 200 ok. if cache on client without using last-modified homecoming 200 ok no matter what. how explained?

you using etags (http://en.wikipedia.org/wiki/http_etag), that's first thing thought of reading question.

you have here: set etag fileresult - mvc 3

asp.net asp.net-mvc asp.net-mvc-3 caching browser-cache

No comments:

Post a Comment