我在C#/ ASP.NET 4應用程序中使用Booksleeve庫。目前,RedisConnection對象是我的MonoLink類中的一個靜態對象。我應該保持這個連接是否打開,還是應該在每個查詢/事務之後打開/關閉它(正如我現在所做的那樣)?只是有點困惑。以下是我如何使用它:截至目前:我應該多久打開一次/關閉我的書籍連接?
public static MonoLink CreateMonolink(string URL)
{
redis.Open();
var transaction = redis.CreateTransaction();
string Key = null;
try
{
var IncrementTask = transaction.Strings.Increment(0, "nextmonolink");
if (!IncrementTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Monolink index increment timed out.");
}
// Increment complete
Key = string.Format("monolink:{0}", IncrementTask.Result);
var AddLinkTask = transaction.Strings.Set(0, Key, URL);
if (!AddLinkTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Add monolink creation timed out.");
}
// Run the transaction
var ExecTransaction = transaction.Execute();
if (!ExecTransaction.Wait(5000))
{
throw new System.TimeoutException("Add monolink transaction timed out.");
}
}
catch (Exception ex)
{
transaction.Discard();
throw ex;
}
finally
{
redis.Close(false);
}
// Link has been added to redis
MonoLink ml = new MonoLink();
ml.Key = Key;
ml.URL = URL;
return ml;
}
非常感謝您的任何迴應/見解。另外,這個圖書館有沒有官方文檔?謝謝你這麼。^_ ^。
這應該是公認的答案 – welegan