在多个应用程序之间共享企业库数据库缓存 [英] Sharing Enterprise Library Database Cache between Multiple Applications
问题描述
我在Enterprise Library 5缓存块中遇到了一个奇怪的问题,其中两个应用程序共享同一个数据库缓存。我编写了一个非常简单的静态缓存管理器类,该类结束了EntLib5 ICacheManager的创建,并对其进行读取/写入。
I'm having a strange problem with the Enterprise Library 5 Caching Block where two applications are sharing the same database cache. I've written a very simple static cache manager class which wraps up creating the EntLib5 ICacheManager, and reading/writing to it.
private static ICacheManager _manager = null;
private static ICacheManager Manager
{
get
{
lock (ClientLock)
{
if (_manager == null)
_manager = CacheFactory.GetCacheManager();
return _manager;
}
}
}
public static object Get(string cacheKey)
{
return Manager.GetData(cacheKey);
}
public static void Add(string cacheKey, object cacheItem)
{
Manager.Add(cacheKey, cacheItem);
}
我的数据库已正确设置,配置文件也已正确设置,我可以阅读&从单个控制台应用程序写入数据库缓存没有问题。
My database is setup correctly as are the configuration files and I can read & write to the database cache from a single console application with no problems.
但是我试图在共享相同缓存的两个应用程序之间进行测试。问题似乎是,如果Console_A中的Static CachingManager类将项目添加到缓存中,则在Console_B实例化其为静态CacheManager之后,Console_B不会获取更改。
However I'm trying to do a test between two applications sharing the same cache. The problem seems to be that if the Static CachingManager class in Console_A adds items to the Cache, after Console_B has instantiated it's static CacheManager, the changes are not picked up by Console_B.
Here's a quick timeline to explain.
Start Console_A
Write Item1 from Console_A to Cache - (Static ICacheManager created with noticeable 1sec pause) Success - 1 Item in Cache
Write Item2 from Console_A to Cache - Success - 2 Items in Cache
Write Item3 from Console_A to Cache - Success - 3 Items in Cache
Start Console_B
Read Item1 from Cache to Console_B - (Static ICacheManager created with noticeable 1sec pause) - Success - 3 Items in Cache
Read Item2 from Cache to Console_B - Success - 3 Items in Cache
Read Item3 from Cache to Console_B - Success - 3 Items in Cache
Write Item4 from Console_A to Cache - Success - 4 Items in Cache (Confirmed in DB)
Read Item4 from Cache to Console_B - Failure - 3 Items in Cache
因此,似乎Console_B最初设置它是缓存管理器后,它不会返回数据库刷新数据。包含
So it seems that once Console_B initially sets up it's cache manager, it doesn't go back to the database to refresh the data. the Contains
我也尝试从我的媒体资源中删除<code> if(_manager == null)支票,因此
I've tried removing the if(_manager == null)
check from my Property as well so that it pulls a new
CacheManager from the factory everytime but it made no difference.
任何建议都值得赞赏。
任何建议都应得到赞赏。
EDIT
编辑
似乎 LoadItems
仅在实例化ICacheManager时调用Sproc,而无论您是否再次调用CacheFActory / Unity EntLibContainer,都不会在应用程序生命周期中再次调用Sproc。
推荐答案
Console_A和Console_B的应用程序域不同。即使您已将缓存管理器定义为静态的,它也不存在于应用程序域之外,因此实际上您有两个彼此不认识的单独的缓存管理器。
I'd recommend using a service (WCF) and calling your business methods through that service, that way all console applications will be calling the same service and it will have one instance of a cache manager residing in it's own application domain.
这篇关于在多个应用程序之间共享企业库数据库缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!