MemoryCache 过期项目 - 空引用异常

问题描述

我们的批处理应用程序出现问题。它是一个用 C# 编写的控制台应用程序,它在循环中运行,从数据库获取任务。它(几乎)永远不会退出 - 仅用于部署。我们使用 MemoryCache(隐藏在我们的抽象层下)来缓存一些数据。有时我们会通过以下堆栈跟踪获得空引用异常:

at System.Runtime.Caching.MemoryCacheEntry.Release(MemoryCache cache,CacheEntryRemovedReason reason)
at System.Runtime.Caching.MemoryCacheStore.RemoveFromCache(MemoryCacheEntry entry,CacheEntryRemovedReason reason,Boolean delayRelease)
at System.Runtime.Caching.MemoryCacheStore.Remove(MemoryCacheKey key,MemoryCacheEntry entryToRemove,CacheEntryRemovedReason reason)
at System.Runtime.Caching.ExpiresBucket.FlushExpiredItems(DateTime utcNow,Boolean useInsertBlock)
at System.Runtime.Caching.CacheExpires.FlushExpiredItems(Boolean checkDelta,Boolean useInsertBlock)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Boolean preserveSyncCtx)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.dispatch()

我浏览了反编译的 MemoryCacheMemoryCacheEntryMemoryCacheStoreExpiresBucket 并且(据我所知)项目过期和删除的过程是一个相当“防御编程”软件。我没有看到任何迹象表明缓存中实际存储的内容会影响此过程。

我注意到在幕后我们的缓存抽象层使用了多个 MemoryCache 实例。我看了一点,但只发现这会影响应用程序性能(现在对我们来说不是问题)。但是我开始考虑是否存在一些竞争条件会导致这些多个 MemoryCache 导致此类错误。例如框架触发清除所有(或部分)MemoryCache 实例的过期项目,它们一起处理,MemoryCacheStore 以某种方式在两个不同的线程中处理。只是我的一个想法,但也许有人有类似的问题,可以分享根本原因。

我们使用的是 .NET Framework 4.8。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...