我在兩臺服務器上有我的網站代碼,我想在它們之間共享一個緩存,所以我使用的是Microsoft AppFabric。我只想在那裏有一個項目在緩存中 - 每個站點訪問者不是一個項目,只有一個項目總數。爲什麼我的AppFabric緩存中有多個緩存項目使用相同的密鑰?
我正在使用的代碼嘗試完成此操作,但是,它並不按照我希望的方式工作。我認爲問題是緩存中有很多項目,儘管它們都具有相同的密鑰。但它可能是其他事情正在發生。查看代碼評論以獲取更多詳細信息。
在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>
在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){}
}
如果你想對我在做什麼,爲什麼,我想使用高速緩存超時模擬Windows服務的更多信息如描述here。但我試圖讓它在兩臺服務器上運行。
我刪除了整個''sessionState>'部分,但似乎沒有改變任何東西。我仍然有多個項目。 – Kalina 2013-04-29 16:55:55
你怎麼知道有多個項目?你使用PS cmdlet Get-Statistics? – Cybermaxs 2013-04-29 18:39:32
我只是假設它是多個緩存項目。我無法訪問PowerShell,也無法檢查;但還有什麼可以解決的呢?一個緩存項目多次失效? – Kalina 2013-05-01 14:57:19