Craig
Craig

Reputation: 36816

Redis StackExchange cache performance

I am trying the Redis StackExchange change provider with Azure and just wondering on the best setup.

Considering the following code

private static ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(...);

public string Get(string key)
{
    IDatabase cache = connection.GetDatabase();
    return cache.StringGet(key);
}

Is the getting of the database going to be a performance hit calling it each time a call to the cache is made?

Should it be managed some way else?

Should it just be created for the lifetime of the object but not static?

What is best practice around managing the IDatabase?

Upvotes: 3

Views: 3178

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062492

Is the getting of the database going to be a performance hit calling it each time a call to the cache is made?

Not noticeably so; as described here:

The object returned from GetDatabase is a cheap pass-thru object, and does not need to be stored.

Of course, you can choose to store it, if you so wish. As it happens, we do that - but that is simply because we use different database numbers (for different sites), and it is just as convenient to store an IDatabase as it is to store the int that represents the database number.

Other than that, though, you don't need to worry too much - if you allocate on-demand, it will still be very cheap to collect etc.

The only other thing worth saying is that it may be worth caching in-memory before you touch redis: a redis call is fast, but a redis call that you don't make is faster.

Upvotes: 6

Related Questions