C# 调用回调函数后ObjectCache未更新
上下文:我需要一个类将文本文件中的一些关键字放入缓存。我在那个文件上放了一个侦听器,所以当它被修改时,缓存会自动更新 ASP.NETMVC3——框架4.5 Global.asax:C# 调用回调函数后ObjectCache未更新,c#,asp.net,global-asax,objectcache,C#,Asp.net,Global Asax,Objectcache,上下文:我需要一个类将文本文件中的一些关键字放入缓存。我在那个文件上放了一个侦听器,所以当它被修改时,缓存会自动更新 ASP.NETMVC3——框架4.5 Global.asax: protected void Application_Start() { // Caching keywords BigBrother.InitiateCaching(BigBrother.ReInitiateCaching); } private static readonly Cache _ca
protected void Application_Start()
{
// Caching keywords
BigBrother.InitiateCaching(BigBrother.ReInitiateCaching);
}
private static readonly Cache _cacheHandler = new Cache();
public static void InitiateCaching(CacheEntryRemovedCallback pCallBackFunction)
{
string filePath = HostingEnvironment.MapPath("~/Configurations/Surveillance/keywords.txt");
var fileContent = File.ReadAllText(filePath, Encoding.UTF8).Split(';');
_cacheHandler.AddToMyCache("surveillanceKeywords", fileContent, CachePriority.Default, new List<string> { filePath }, pCallBackFunction);
}
public static void ReInitiateCaching(CacheEntryRemovedArguments arguments)
{
InitiateCaching(ReInitiateCaching);
}
public void AddToMyCache(string cacheKeyName, object cacheItem, CachePriority cacheItemPriority, List<string> filePath,
CacheEntryRemovedCallback pCallBackFunction)
{
callback = pCallBackFunction;
policy = new CacheItemPolicy
{
Priority = (cacheItemPriority == CachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable,
AbsoluteExpiration = DateTimeOffset.Now.AddYears(1),
RemovedCallback = callback
};
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath));
cache.Set(cacheKeyName, cacheItem, policy);
}
BigBrother.cs类:
protected void Application_Start()
{
// Caching keywords
BigBrother.InitiateCaching(BigBrother.ReInitiateCaching);
}
private static readonly Cache _cacheHandler = new Cache();
public static void InitiateCaching(CacheEntryRemovedCallback pCallBackFunction)
{
string filePath = HostingEnvironment.MapPath("~/Configurations/Surveillance/keywords.txt");
var fileContent = File.ReadAllText(filePath, Encoding.UTF8).Split(';');
_cacheHandler.AddToMyCache("surveillanceKeywords", fileContent, CachePriority.Default, new List<string> { filePath }, pCallBackFunction);
}
public static void ReInitiateCaching(CacheEntryRemovedArguments arguments)
{
InitiateCaching(ReInitiateCaching);
}
public void AddToMyCache(string cacheKeyName, object cacheItem, CachePriority cacheItemPriority, List<string> filePath,
CacheEntryRemovedCallback pCallBackFunction)
{
callback = pCallBackFunction;
policy = new CacheItemPolicy
{
Priority = (cacheItemPriority == CachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable,
AbsoluteExpiration = DateTimeOffset.Now.AddYears(1),
RemovedCallback = callback
};
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath));
cache.Set(cacheKeyName, cacheItem, policy);
}
私有静态只读缓存_cacheHandler=new Cache();
公共静态无效初始化缓存(CacheEntryRemovedCallback pCallBackFunction)
{
字符串filePath=HostingEnvironment.MapPath(“~/Configurations/supervision/keywords.txt”);
var fileContent=File.ReadAllText(filePath,Encoding.UTF8).Split(“;”);
_AddToMyCache(“监视关键字”,文件内容,CachePriority.Default,新列表{filePath},pCallBackFunction);
}
公共静态void重新初始化缓存(CacheEntryRemovedArguments)
{
初始化缓存(重新初始化缓存);
}
Cache.cs:
protected void Application_Start()
{
// Caching keywords
BigBrother.InitiateCaching(BigBrother.ReInitiateCaching);
}
private static readonly Cache _cacheHandler = new Cache();
public static void InitiateCaching(CacheEntryRemovedCallback pCallBackFunction)
{
string filePath = HostingEnvironment.MapPath("~/Configurations/Surveillance/keywords.txt");
var fileContent = File.ReadAllText(filePath, Encoding.UTF8).Split(';');
_cacheHandler.AddToMyCache("surveillanceKeywords", fileContent, CachePriority.Default, new List<string> { filePath }, pCallBackFunction);
}
public static void ReInitiateCaching(CacheEntryRemovedArguments arguments)
{
InitiateCaching(ReInitiateCaching);
}
public void AddToMyCache(string cacheKeyName, object cacheItem, CachePriority cacheItemPriority, List<string> filePath,
CacheEntryRemovedCallback pCallBackFunction)
{
callback = pCallBackFunction;
policy = new CacheItemPolicy
{
Priority = (cacheItemPriority == CachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable,
AbsoluteExpiration = DateTimeOffset.Now.AddYears(1),
RemovedCallback = callback
};
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath));
cache.Set(cacheKeyName, cacheItem, policy);
}
public void AddToMyCache(字符串cacheKeyName、对象cacheItem、CachePriority、列表文件路径、,
CacheEntryRemovedCallback(pCallBackFunction)
{
callback=pCallBackFunction;
policy=newcacheitempolicy
{
优先级=(cacheItemPriority==CachePriority.Default)?cacheItemPriority.Default:cacheItemPriority.NotRemovable,
AbsoluteExpiration=DateTimeOffset.Now.AddYears(1),
RemovedCallback=回调
};
policy.ChangeMonitors.Add(新主机文件更改监视器(文件路径));
Set(cacheKeyName、cacheItem、policy);
}
因此常见的工作流是:
1.启动应用程序启动,BigBrother使用回调函数将关键字放入缓存中(效果非常好)
2.听文件“keywords.txt”
3.编辑文件“keywords.txt”时,刷新缓存(实际上,会调用ReinititeCaching,但我的缓存项“surveillanceKeywords”返回null)
整个程序在我的本地环境中运行,但在服务器上不运行。我错过什么了吗
谢谢。您是否为
AppDomain.CurrentDomain.UnhandledException设置了未经处理的异常处理程序,因为服务器上的权限设置可能不同,并且更改监视器在不同的线程上执行?是的,我有一个处理程序,不幸的是没有异常。