0

在我的生产服务器上,我有一个大量使用的 SQL Server 实例和我的应用程序,它是 ac# Windows 服务。我的服务在不同时间运行各种工作。它每 24 小时发送一封电子邮件,提供有关各种工作进度的最新信息。

代码通过检查内存缓存中的标志来确定是否发送电子邮件。代码如下所示:-

if (Common.MemCache.Get(Common.MemoryCacheActions.DailyEmail) == null)
{
    Common.Logger.Info("Collating email data.");
    Common.MemCache.Add(Common.MemoryCacheActions.DailyEmail, String.Empty, DateTime.Now.AddHours(24));

    SendEmail();
}

当我的主循环每分钟左右检查一次空条目时,我收到了很多电子邮件。就好像 Add 失败了,或者 item 被驱逐了?

我在一些地方读到,如果没有足够的可用内存,项目可能会被驱逐。发生这种情况时,维护脚本正在 SQL Server 中运行,并且盒子上的可用内存几乎为零。我关闭了一些我在服务器上运行的程序、chrome、SSMS 等,瞧,没有更多的日志条目或电子邮件。

由于我已将项目的过期时间明确设置为现在 + 24 小时,我希望它会在 24 小时内添加项目!

这里的解决方案是什么,我是否必须将 CacheItemPriority 设置为 NotRemoveable,然后在 24 小时后手动删除该项目?

有点违背了我如何使用缓存的意义,或者 NotRemovable 会与 AbsoluteExpirationDate 一起工作吗?

4

2 回答 2

1

You can use

MemoryCache.CreateCacheEntryChangeMonitor()

Creates a CacheEntryChangeMonitor object that can trigger events in response to changes to specified cache entries.

to see if the relevant item is removed from the cache.

That method returns a CacheEntryChangeMonitor instance, whose NotifyOnChanged method is used to provide a callback method that is invoked when the cache changes.

Also, verify that your service is not restarting. Perhaps it is crashing and set to restart on crash?

于 2014-10-21T22:37:03.990 回答
0

我认为,所描述的行为是正确的 - 没有合同或定义说该项目不能随时从缓存中驱逐。

恕我直言,每 24 小时安排一次电子邮件要好得多,例如 https://www.quartz-scheduler.net/或您选择的任何其他调度库。

那会很清楚地表明你的意图。

于 2017-09-05T09:02:44.023 回答