I have my website code on two servers and I want a shared cache between them, so I am using Microsoft AppFabric. I only ever want there to be one item in the cache -- not one item per site visitor, just one item total.
The code I'm using to try to accomplish this is below, however, it is not working the way I want it to. I think the problem is that there are many items in the cache, despite them all having the same key. But it could be something else that's happening. See the code comments for more details.
In Web.config
:
<dataCacheClient>
<hosts>
<host name="MyHost1" cachePort="22233"/>
<host name="MyHost2" cachePort="22233"/>
</hosts>
<securityProperties mode="None" protectionLevel="None" />
</dataCacheClient>
...
<system.web>
<sessionState mode="InProc" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<add
name="AppFabricCacheSessionStoreProvider"
type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"
cacheName="MyCache"
sharedId="MySharedApp"/>
</providers>
</sessionState>
...
</system.web>
In Global.asax.cs
:
private static DataCacheFactory _cacheFactory = new DataCacheFactory();
private static DataCache _cache;
private void RegisterCacheEntry(){
try
{
// Set up cache
_cache = _cacheFactory.GetCache("MyCache");
// Prevent duplicate key addition
if (_cache.GetCacheItem(CacheItemKey) != null || _cache.Get(CacheItemKey) != null)
return;
// Add item to cache
_cache.Add(CacheItemKey, CacheItem, TimeSpan.FromMinutes(10));
// When the cache item is removed, the callback:
// 1) sends me an email
// 2) adds the same item with the same key to the cache again.
// So I SHOULD be getting one email every 10 minutes.
// However, it starts with one, but the number of emails that I get increases every 10 minutes.
// i.e., I get one email at time t=0, two emails at t=1, three at t=2, then four, etc.
_cache.AddItemLevelCallback(CacheItemKey, DataCacheOperations.RemoveItem, CacheItemRemovedCallback);
}
catch (Exception e){}
}
If you want more information on what I'm doing and why, I'm trying to simulate a Windows Service using cache timeout as described here. But I'm trying to get this to work on two servers.