在 ASP.NET 2.0 中的特定时间清除缓存

发布于 2024-08-13 21:07:22 字数 550 浏览 6 评论 0原文

所以我有一个在午夜运行的进程,它为 Flash 对象设置起点和终点。这只需每天运行一次,因此我显然正在缓存结果集。

然而,我遇到的问题是,如果数据在午夜之后仍然被缓存,那么它就不会提取最正确的数据,直到缓存过期。

我基本上需要缓存在晚上 11:59:59 过期,以便在中午 12:00 获得正确的数据。

我猜测对我从中提取数据的表的 SQL 缓存依赖关系是理想的,但我以前从未设置过。

有没有办法告诉缓存在中午删除特定项目?

谢谢你们!

--绝对过期---

我想我明白了:

DateTime expireWeights = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59, 999);
Cache.Insert("CacheItemName", itemToCache, null, expireWeights, System.Web.Caching.Cache.NoSlidingExpiration);

So I have a process which runs at midnight which sets a start and end point for a flash object. This only needs to run once a day, so I'm obviously caching the result set.

However, the problem I'm running into is, if the data is still cached after midnite, it's not pulling in the most correct data, until the cache expires.

I basically need the cache to expire at 11:59:59PM, so that at 12:00am it gets the correct data.

I'm guessing a SQL Cache Dependency on the table I'm pulling the data from would be ideal, however I have never set that up before.

Is there a way to tell the cache to remove a specific item at exactly midnite?

Thanks guys!

--Absolute Expiration---

I think I got it:

DateTime expireWeights = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59, 999);
Cache.Insert("CacheItemName", itemToCache, null, expireWeights, System.Web.Caching.Cache.NoSlidingExpiration);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ花落谁相伴 2024-08-20 21:07:22

您可以在Cache对象上设置一个absoluteExpiration时间,它是一个DateTime。

您还可以将absoluteExpiration 与SqlCacheDependency 结合起来。

关于缓存过期时不拉取新数据的问题:您可以连接 CacheItemRemovedCallback 以接收缓存过期时的通知,并在那时刷新缓存。

You can set an absoluteExpiration time on the Cache object, which is a DateTime.

You can also combine an absoluteExpiration with a SqlCacheDependency.

Regarding the issue of it not pulling the new data when the cache expires: you can wire up a CacheItemRemovedCallback to receive notification of when it expires, and refresh the cache at that time.

诠释孤独 2024-08-20 21:07:22

我不明白为什么你有绝对过期的问题?您可以指定该项目将从缓存中过期的确切日期时间。因此,下面这行代码会将“myObject”插入到“MyKey”键下的缓存中,并将在第二天午夜过期(无论它何时进入缓存)。

Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);

I don't understand why you have a problem with absolute expiration? You can indicate the exact datetime in which the item will expire from the cache. Therefore, the following line of code will insert "myObject" into the cache under the key "MyKey" and will expire at midnight on the next day (Regardless of when it enteres the cache).

Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
凉世弥音 2024-08-20 21:07:22

如果您查看此处,在“Time- based Dependency”,它展示了如何将项目设置为在特定时间过期。这就是您要找的吗?

If you look here, under "Time-based Dependency", it shows how to set the item to expire at a specific time. Is that what you're looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文