【问题标题】:How often should I open/close my Booksleeve connection?我应该多久打开/关闭 Booksleeve 连接?
【发布时间】:2011-11-24 12:25:38
【问题描述】:

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

在此先感谢您的任何回复/见解。另外,这个库有什么官方文档吗?谢谢你这么。 ^_^。

【问题讨论】:

    标签: asp.net c#-4.0 redis booksleeve


    【解决方案1】:

    According toauthor of Booksleeve,

    连接是线程安全的,旨在被大规模共享; 不要每次操作都建立连接。

    【讨论】:

    • 标记为正确答案,因为文档现在可用。
    【解决方案2】:

    我应该保持这个连接打开,还是应该打开/关闭 在每次查询/交易之后(就像我现在所做的那样)?

    如果您每次要进行查询/事务时都打开一个新连接,可能会有一点开销,虽然 redis 是为高级并发连接的客户端设计的,但如果它们的数量在几十个左右,可能会出现性能问题数以千计。据我所知,连接池应该由客户端库完成(因为redis本身没有这个功能),所以你应该检查一下booksleeve supports这个东西。否则,您应该在应用程序启动时打开连接并在其生命周期内保持打开状态(以防由于某种原因您不需要将并行客户端连接到 redis)。

    另外,这个库有什么官方文档吗?

    我能找到的关于如何使用它的唯一文档是它的源代码中的tests folder。

    【讨论】:

      【解决方案3】:

      作为参考(继续@bzlm's answer),我创建了一个单例,它始终使用 BookSleeve 提供相同的 Redis 连接(如果它已关闭,则正在创建它。否则,正在提供现有连接)。

      看这个:https://stackoverflow.com/a/8777999/290343

      你这样消费它:

      RedisConnection connection = Redis.RedisConnectionGateway.Current.GetConnection();
      

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 2010-10-26
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多