简体   繁体   English

System.Net.Http.HttpClient缓存

[英]System.Net.Http.HttpClient cache

Is there a way to set up cache for HttpClient ? 有没有办法为HttpClient设置缓存? I would like to get the HttpClient response (which is reaching my endpoint with get method), but if there is no internet connection on the client device, I would like to return the last response for this specific call. 我想获取HttpClient响应(通过get方法到达我的端点),但是如果客户端设备上没有Internet连接,我想返回此特定调用的最后一个响应。 If there is a internet connection, the HttpClient get the response (normal behavior) and update the cache by this new response for this call. 如果存在Internet连接,则HttpClient将获得响应(正常行为)并通过此调用的新响应来更新缓存。

Any idea to set up this behavior in a cross plateform Xamarin Forms project ? 有任何想法在跨平台Xamarin Forms项目中设置此行为吗?

You may find useful any of these Akavache methods: 这些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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM