Search code examples
asp.netc#-4.0redisbooksleeve

How often should I open/close my Booksleeve connection?


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

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


Solution

  • According to the author of Booksleeve,

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