Created
January 16, 2013 08:58
-
-
Save PeteGoo/4545689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CacheCow.Client.FileCacheStore; | |
namespace Caching.WpfApp { | |
public class PeopleProxy { | |
public async Task<IEnumerable<Person>> GetAll() { | |
var handler = new CachingHandler( | |
new FileStore("c:\\temp\\cache.cow")) { | |
InnerHandler = new HttpClientHandler() | |
}; | |
HttpClient client = new HttpClient(handler) { | |
BaseAddress = new Uri("http://ipv4.fiddler/Caching.WebApplication/"), | |
}; | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
HttpResponseMessage response = await client.GetAsync("api/people"); | |
if (!response.IsSuccessStatusCode) { | |
// maybe you'll want to throw a real exception type | |
throw new Exception("Could not fetch people"); | |
} | |
return await response.Content.ReadAsAsync<IEnumerable<Person>>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET http://127.0.0.1/Caching.WebApplication/api/people HTTP/1.1 | |
Accept: application/json | |
Host: 127.0.0.1 | |
Connection: Keep-Alive | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTTP/1.1 200 OK | |
Cache-Control: max-age=20 | |
Content-Type: application/json; charset=utf-8 | |
Server: Microsoft-IIS/8.0 | |
X-AspNet-Version: 4.0.30319 | |
X-Powered-By: ASP.NET | |
Date: Wed, 16 Jan 2013 08:56:53 GMT | |
Content-Length: 161 | |
[{"id":1,"firstName":"Peter","lastName":"Goodman","favouriteMovie":"The Big Lebowski"},{"id":2,"firstName":"Foo","lastName":"Bar","favouriteMovie":"The Grinch"}] |
OK I rushed the merge, there was no need to change the code. The code is doing as it should do so I have reverted the changes.
Have a look here:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
The point here is that a cache implementation MAY choose to use the stale value - as CacheCow is doing. Server usually prevents this behaviour by sending must-revalidate.
In your case, response from the server did not have must-revalidate so CacheCow is free to serve stale response from cache. You can change this behaviour however by setting ResponseStoragePreparationRules and in there in addition to default work, set the response.Headers.CacheControl.MustRevalidate (make sure CacheControl header has a value and is not null):
ResponseStoragePreparationRules = (response) =>
{
// 14.9.3
// If a response includes both an Expires header and a max-age directive,
// the max-age directive overrides the Expires header, even if the Expires header is more restrictive.
if(response.Content.Headers.Expires!=null &&
(response.Headers.CacheControl.MaxAge != null || response.Headers.CacheControl.SharedMaxAge!=null))
{
response.Content.Headers.Expires = null;
}
response.Headers.CacheControl.MustRevalidate = true;
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I merged the code but I think it is still not right. Will need to work on it.
This needs to fall into the Must-Revalidate which is missing in your server's response.