有没有办法为 HttpClient 设置缓存? 我想获得 HttpClient 响应(通过 get 方法到达我的端点),但如果客户端设备上没有互联网连接,我想返回此特定调用的最后响应。如果有 Internet 连接,HttpClient 会获取响应(正常行为)并通过此调用的新响应更新缓存。
想在跨平台 Xamarin Forms 项目中设置此行为吗?
最佳答案
您可能会发现其中任何一个有用 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);
关于c# - System.Net.Http.HttpClient缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805416/