我正在尝试使用Cache.Insert()将字符串存储在缓存中。当我尝试使用文件依赖项存储它时,在Cache.Insert()之后,我的缓存值仍为null。请帮我解决这个问题。此代码也在Page_load函数中。 下面是我的缓存代码:
// add 30secs expiration timer
DateTime time = DateTime.Today.AddSeconds(30);
// add file dependency for a file
CacheDependency quesDependency = new CacheDependency(filePath, time);
Cache.Insert("question_of_the_day", html, quesDependency,time, Cache.NoSlidingExpiration);
// after this step i still get Cache["question_of_the_day"] = null instead of getting that html string.
答案 0 :(得分:0)
您使用的绝对过期时间不正确。
DateTime.Today - An object that is set to today's date, with the time component set to 00:00:00.
https://msdn.microsoft.com/en-us/library/system.datetime.today%28v=vs.110%29.aspx
更改
DateTime time = DateTime.Today.AddSeconds(30);
到
DateTime time = DateTime.Now.AddSeconds(30);