Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is "ConnectionMultiplexer.IsConnected" expensive?

In my caching AddItem and GetItem methods, I check if the connection to redis is alive or not before proceeding, is ConnectionMultiplexer.IsConnected an expensive method call? or should I just catch an exception and reconnect in that case?

like image 484
Nean Der Thal Avatar asked Feb 19 '16 23:02

Nean Der Thal


1 Answers

Looking at ConnectionMultiplexer.IsConnected code:

public bool IsConnected
{
    get
    {
        var tmp = serverSnapshot;
        for (int i = 0; i < tmp.Length; i++)
            if (tmp[i].IsConnected) return true;
        return false;
    }
}

It seems like all work done here is going through server endpoints to see whether there is at-least one server endpoint connected.

Looking at ServerEndPoint.IsConnected code:

public bool IsConnected
{
    get
    {
        var tmp = interactive;
        return tmp != null && tmp.IsConnected;
    }
}

All work done here is returning interactive(of type PhysicalBridge) IsConnected value.

Looking at PhysicalBridge.IsConnected code:

public bool IsConnected => state == (int)State.ConnectedEstablished;

You can see all work done here is return whether two int's are equal.

So it seems like very little work is done calling ConnectionMultiplexer.IsConnected property.

like image 175
Kobynet Avatar answered Nov 14 '22 02:11

Kobynet