我不知道爲什麼SqlCacheDependency
對象的HasChanged
值最初從命令執行返回爲false,但在數據從數據庫返回後幾乎立即返回,值更改爲true。爲什麼我的SqlCacheDependency HasChanged會變回false,但在更改爲true後幾乎立即變爲false?
有時甚至在項目被插入到緩存之前發生,導致緩存立即丟棄它,有時候是在插入之後,我可以獲取一個枚舉器,它在緩存中但是在我循環到該項目在緩存中已被刪除。
存儲過程:
ALTER PROCEDURE [dbo].[ntz_dal_ER_X_Note_SelectAllWER_ID]
@ER_ID int
AS
BEGIN
SELECT
ER_X_Note_ID,
ER_ID,
Note_ID
FROM dbo.ER_X_Note e
WHERE
ER_ID = @ER_ID
END
的數據庫是MS SQL Server 2008中,中介服務已啓用,以及一些輸出做緩存和保持緩存。舉例來說,這個工作得很好:
ALTER PROC [dbo].[ntz_dal_GetCacheControllerByEntityName] (
@Name varchar(50)
) AS
BEGIN
SELECT
CacheController_ID,
EntityName,
CacheEnabled,
Expiration
From dbo.CacheController cc
WHERE EntityName = @Name
END
它調用存儲過程中的問題,失敗的代碼:
,工程 const string sprocName = "ntz_dal_GetCacheControllerByEntityName";
string cacheControlPrefix = "CacheController_" + CachePrefix;
CacheControl controller = (CacheControl)_cache[cacheControlPrefix];
if (controller == null)
{
try
{
SqlParameter[] arParms = {
new SqlParameter("@Name", CachePrefix),
};
SqlCacheDependency sqlCacheDependency;
// Execute query.
DataSet result = _dbTransaction != null
? _dbConnection.ExecuteDataset(_dbTransaction, sprocName, out sqlCacheDependency, arParms)
: _dbConnection.ExecuteDataset(sprocName, out sqlCacheDependency, arParms);
controller = result.Tables[0].Rows.Count == 0
? new CacheControl(false)
: new CacheControl(result.Tables[0].Rows[0]);
_cache.Insert(cacheControlPrefix, controller, sqlCacheDependency);
}
catch (Exception ex)
{
// if sproc retreival fails cache the result of false so we don't keep trying
// this is the only case where it can be added with no expiration date
controller = new CacheControl(false);
// direct cache insert, no dependency, no expiration, never try again for this entity
if (HttpContext.Current != null && UseCaching && _cache != null) _cache.Insert(cacheControlPrefix, controller);
}
}
return controller;
的AddToCache
方法重載,
DataSet toReturn;
Hashtable paramHash = new Hashtable();
paramHash.Add("ER_ID", _eR_ID.IsNull ? null : _eR_ID.Value.ToString());
string cacheName = BuildCacheString("ntz_dal_ER_X_Note_SelectAllWER_ID", paramHash);
toReturn = (DataSet)GetFromCache(cacheName);
if (toReturn == null)
{
// Set up parameters (1 input and 0 output)
SqlParameter[] arParms = {
new SqlParameter("@ER_ID", _eR_ID),
};
SqlCacheDependency scd;
// Execute query.
toReturn = _dbTransaction != null
? _dbConnection.ExecuteDataset(_dbTransaction, "dbo.[ntz_dal_ER_X_Note_SelectAllWER_ID]", out scd, arParms)
: _dbConnection.ExecuteDataset("dbo.[ntz_dal_ER_X_Note_SelectAllWER_ID]", out scd, arParms);
AddToCache(cacheName, toReturn, scd);
}
return toReturn;
代碼有更多的測試;工作方法中的直接_cache.Insert
是繞過其他測試。工作代碼有助於確定數據庫緩存是否應該發生。
你可以看到,當「非工作」的數據最初檢索,一切都OK:
而是介於隨機超出該點,在這種情況下,剛剛步入下一個方法
然而數據並沒有改變;我是唯一一個接觸這個數據庫實例的人。
我會試試它,但我認爲這不太可能,因爲param哈希僅用於構建創建緩存項的密鑰名稱。 – 2011-12-27 00:38:39
@TheEvilGreebo:對不起,我覺得我沒有足夠的溝通。我認爲問題是這行代碼'新的SqlParameter(「@ ER_ID」,_eR_ID)'。這將整個_eR_ID對象傳遞給參數,我不確定它是如何將它轉換爲Sql的值(我懷疑它在對象上調用了ToString())。之前在代碼中使用_eR_ID.Value,所以我認爲您需要的最小更改是'new SqlParameter(「@ ER_ID」,_eR_ID.Value)'。 – 2011-12-27 00:51:58
哦,我明白你的意思了。那麼_eR_ID是一個SqlInt32 - 所以人們會認爲使用它初始化一個新的SqlParm不會引起問題,但它值得一試! – 2011-12-27 01:47:51