System.Net.Http.HttpClient缓存

时间:2017-01-23 11:33:49

标签: c# caching xamarin httpclient

有没有办法为HttpClient设置缓存? 我想获得HttpClient响应(使用get方法到达我的端点),但如果客户端设备上没有互联网连接,我想返回此特定呼叫的最后一个响应。如果有互联网连接,HttpClient将获得响应(正常行为)并通过此呼叫的新响应更新缓存。

有任何想法在跨平台Xamarin Forms项目中设置此行为吗?

1 个答案:

答案 0 :(得分:0)

您可能会发现以下任何Akavache方法都很有用:

// Attempt to return an object from the cache. If the item doesn't
// exist or returns an error, call a Func to return the latest
// version of an object and insert the result in the cache.
IObservable<T> GetOrFetchObject<T>(string key, Func<Task<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null);

// Like GetOrFetchObject, but isn't async
IObservable<T> GetOrCreateObject<T>(string key, Func<T> fetchFunc, DateTimeOffset? absoluteExpiration = null);

// Immediately return a cached version of an object if available, but *always*
// also execute fetchFunc to retrieve the latest version of an object.
IObservable<T> GetAndFetchLatest<T>(string key,
    Func<Task<T>> fetchFunc,
    Func<DateTimeOffset, bool> fetchPredicate = null,
    DateTimeOffset? absoluteExpiration = null);