我有以下要求:
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var data = client.DownloadString("http://finance.yahoo.com/webservice/v1/symbols/IBM,%5EIXIC/quote?format=json&view=detail");
StringBuilder builder = new StringBuilder();
builder.Append("<script type=\"text/javascript\">");
builder.Append("stockQuotes = new Object();");
builder.Append(data);
builder.Append("</script>");
ltrData.Text = builder.ToString();有没有一种方法可以在内存中缓存30分钟的响应?如果是这样的话,请怎么做?
发布于 2016-07-19 16:14:17
我能够使用下面的代码让它工作。希望它能在某个时间点上帮助其他人。
这样命名:
var jsonObject1 = JsonConvert.DeserializeObject<Rootobject>(GetCachedJson(true, 120));使用以下方法:
public static string GetCachedJson(bool requireCache, int minuteInCache)
{
if (requireCache)
{
if (HttpRuntime.Cache[CacheKey()] == null)
{
string dataJSON = GetJSONPeopleDetails();
HttpRuntime.Cache.Insert(CacheKey(), dataJSON, null, DateTime.Now.AddMinutes(minuteInCache), Cache.NoSlidingExpiration);
}
return HttpRuntime.Cache[CacheKey()].ToString();
}
else
{
return GetJSONPeopleDetails();
}
}
private static string CacheKey()
{
string CACHE_NAME = "JSON_Blah.RESPONSE" + HttpContext.Current.Request.QueryString["uid"];
return CACHE_NAME;
}https://stackoverflow.com/questions/38447075
复制相似问题