2017-02-07 34 views
0

我使用这行代码从缓存中读取先前保存的对象任务<String>在对象缓存

Task<string> responselist = cache["responselist"] as Task<string>; 

线用来保存对象

cache.Set("responselist", response.Content.ReadAsStringAsync(), policy); 

的原因,我使用responselist变量键入Task,因为我在我的方法中返回“Task”对象。

我对Web API相当陌生。我只想知道这是否有意义,或者有更好的选择? PS:它可以100%正常工作。

回答

0

您应该使用async/await关键字并将其存储在缓存中。

public async Task Action(...) 
    { 
     string content = await response.Content.ReadAsStringAsync(); 
     cache.Set("responselist", content, policy); 

     /* ... */ 

     string content = cache["responselist"] as string; 
    } 

存储基本类型/ POCO类比类Tas​​k更自然。通过使用基本类型,您可以将容量保存在缓存中。