3

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

"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." I assume by this you mean that once you make a call to the cache then store the value in memory, possibly just for the life of the request, but it could make a difference. Otherwise how do you handle invalidation of the in-memory across servers?
@Craig ideally, longer than the request; when invalidation matters: pub/sub; actually, recent versions of redis optionally include keyspace change notifications, but that needs to be enabled at the server; we just use regular explicit pub/sub when we need invalidation (our "set value" method has a bool broadcastInvalidation = false, if you see what I mean)
Nice. Do you have any examples of your broadcastInvalidation implementation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.