即使在使用文件依赖性插入后缓存项也不会被存储

Cache item is not getting stored even after inserting using file dependency

我正在尝试使用 Cache.Insert() 将字符串存储在缓存中。当我尝试使用文件依赖项存储它时,在 Cache.Insert() 之后我的缓存值仍然为空。请帮我解决这个问题。此代码也在 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.

您使用的绝对过期时间不正确。

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);