C# System.Net.Http.HttpClient缓存

C# System.Net.Http.HttpClient缓存,c#,caching,xamarin,httpclient,C#,Caching,Xamarin,Httpclient,有没有办法为HttpClient设置缓存? 我想获取HttpClient响应(它通过get方法到达我的端点),但是如果客户端设备上没有internet连接,我想返回此特定调用的最后一个响应。如果存在internet连接,HttpClient将获得响应(正常行为),并通过此调用的新响应更新缓存 有没有在跨平台Xamarin Forms项目中设置此行为的想法?您可能会发现以下任何方法都很有用: //尝试从缓存返回对象。如果该项目没有 //如果存在或返回错误,请调用Func以返回最新的 //并将结果插

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


有没有在跨平台Xamarin Forms项目中设置此行为的想法?

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

//尝试从缓存返回对象。如果该项目没有
//如果存在或返回错误,请调用Func以返回最新的
//并将结果插入缓存中。
IObservable GetOrFetchObject(字符串键,Func fetchFunc,DateTimeOffset?absoluteExpiration=null);
//与GetOrFetchObject类似,但不是异步的
IObservable GetOrCreateObject(字符串键,Func fetchFunc,DateTimeOffset?absoluteExpiration=null);
//立即返回对象的缓存版本(如果可用),但*始终*
//同时执行fetchFunc以检索对象的最新版本。
IObservable GetAndFetchLatest(字符串键,
Func fetchFunc,
Func fetchPredicate=null,
DateTimeOffset?绝对过期=空);
// 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);