小编典典

我应该多久打开/关闭我的Bookleeve连接?

redis

我在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;
}

预先感谢您的任何回应/见解。另外,该库是否有任何官方文档?谢谢你^ _ ^。


阅读 314

收藏
2020-06-20

共1个答案

小编典典

根据Booksleeve的[作者所说,

该连接是线程安全的,并且打算被大量共享。请勿在每次操作时进行连接。

2020-06-20