开发者

How often should I open/close my Booksleeve connection?

开发者 https://www.devze.com 2023-04-08 00:12 出处:网络
I\'m using the Booksleeve library in a C#/ASP.NET 4 application.Currently the RedisConnection object is a static object across my MonoLink class.Should I be keeping this connection open, or should I b

I'm using the Booksleeve library in a C#/ASP.NET 4 application. Currently the RedisConnection object is a static object across my MonoLink class. Should I be keeping this connection open, or should I be open/closing it after each query/transaction (as I'm doing now)? Just slightly confused. Here's how I'm using it, as of now:

public static MonoLink CreateMonol开发者_JAVA技巧ink(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;
}

Thanks, in advance, for any responses/insight. Also, is there any sort of official documentation for this library? Thank you S.O. ^_^.


According to the author of Booksleeve,

The connection is thread safe and intended to be massively shared; don't do a connection per operation.


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

There is probably a little overhead if you will open a new connection each time you want to make a query/transaction and although redis is designed for high level of concurrently connected clients, there might be performance problems if their number is around tens of thousands. As far as I know connection pooling should be done by the client libraries (because redis itself doesn't have this functionality), so you should check if booksleeve supports this stuff. Otherwise you should open the connection when your application starts and keep it open for it's lifetime (in case you don't need parallel clients connected to redis for some reason).

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

The only documentation I was able to find regarding how to use it was tests folder in it's source codes.


For reference (continuing @bzlm's answer), I created a Singleton that always provides the same Redis connection using BookSleeve (if it's closed, it's being created. Else, the existing connection is being served).

Look at this: https://stackoverflow.com/a/8777999/290343

You consume it like that:

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

精彩评论

暂无评论...
验证码 换一张
取 消