Skip to content

Instantly share code, notes, and snippets.

@ryansroberts
Created December 12, 2014 23:41
Show Gist options
  • Save ryansroberts/ade02844f44e4c9a0e7f to your computer and use it in GitHub Desktop.
Save ryansroberts/ade02844f44e4c9a0e7f to your computer and use it in GitHub Desktop.
namespace Syndication.Client.Http
{
public class CachingAdapter : HttpClientAdapter
{
readonly HttpClient client;
public CachingAdapter(ICacheStore cacheStore, string[] defaultVaryHeaders)
{
//http://msdn.microsoft.com/en-us/library/system.net.cache.requestcachelevel(v=vs.110).aspx
//http://blog.technovert.com/2013/01/httpclient-caching/
//http://blogs.msdn.com/b/henrikn/archive/2012/08/07/httpclient-httpclienthandler-and-httpwebrequesthandler.aspx
if(cacheStore == null)
client = new HttpClient();
else
{
client = new HttpClient(new CustomCachingHandler(cacheStore)
{
DefaultVaryHeaders = defaultVaryHeaders,
InnerHandler = new HttpClientHandler()
});
}
}
public void Dispose()
{
client.Dispose();
}
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
return client.SendAsync(request).ContinueWith(task => new ResponseDecompressor().DecompressResponse(task));
}
}
}
namespace Syndication.Client.Http
{
public class Factory : HttpClientAdapterFactory
{
readonly ICacheStore _cacheStore;
private readonly string[] _defaultVaryHeaders;
public Factory(ICacheStore cacheStore, string[] defaultVaryHeaders)
{
_cacheStore = cacheStore;
_defaultVaryHeaders = defaultVaryHeaders;
}
public HttpClientAdapter CreateClient(PipelineOptions options)
{
return new CachingAdapter(_cacheStore, _defaultVaryHeaders);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment