加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 多久我应该打开/关闭我的Booksleeve连接?

发布时间:2020-12-16 00:23:32 所属栏目:asp.Net 来源:网络整理
导读:我在C#/ ASP.NET 4应用程序中使用Booksleeve库。目前,RedisConnection对象是MonoLink类中的静态对象。我应该保持这个连接是开放的,还是应该在每个查询/交易之后打开/关闭(正如我现在所做的)?只是稍微困惑以下是我现在使用的方式: public static MonoLink
我在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;
}

谢谢,提前,任何答复/见解。此外,这个图书馆有哪些官方文件?谢谢你这么。 ^ _ ^。

解决方法

Should I be keeping this connection open,or should I be open/closing
it after each query/transaction (as I’m doing now)?

如果您每次要进行查询/事务时都会打开一个新的连接,那么可能有一点开销,尽管redis是为高级别并发连接的客户端设计的,但是如果数量大概是数万,则可能会出现性能问题。据我所知,连接池应该由客户端库完成(因为redis本身没有这个功能),所以你应该检查booksleeve supports这个东西。否则,您应该在应用程序启动时打开连接,并保持打开连接,以保持终身(如果您因为某些原因而不需要并行客户端连接到redis)。

Also,is there any sort of official documentation for this library?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读