缓存过期时间设置之后,不管用
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
var sb = new StringBuilder();
for(var i = 0; i < 3; i++)
{
sb.AppendLine($@"{getValue()} {DateTime.Now}");
Thread.Sleep(2000);
}
return Content(sb.ToString());
//return View();
}
private string getValue()
{
var key = "name";
var d = DateTime.Now;
//var expireTime = DateTime.Now.AddSeconds(3);
if (HttpRuntime.Cache.Get(key) == null)
{
// HttpRuntime.Cache.Insert(key, d, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(3));
HttpRuntime.Cache.Insert(key, d, null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration);
return d.ToString();
}
else
{
return HttpRuntime.Cache.Get(key).ToString();
}
}
}
输出结果是这样的:
2017/7/20 16:22:28 2017/7/20 16:22:28
2017/7/20 16:22:28 2017/7/20 16:22:30
2017/7/20 16:22:28 2017/7/20 16:22:32
再次刷新页面,仍然不管用
2017/7/20 16:22:28 2017/7/20 16:23:17
2017/7/20 16:22:28 2017/7/20 16:23:19
2017/7/20 16:22:28 2017/7/20 16:23:21
IISExpress和IIS下都试了,就是不行,但放到同事的机器上就没问题