8

I am using the StackExchange.Redis 1.0.450 nuget in C#. I have code like below which checks if a keyexists in redis before adding it -

if (!Cache.KeyExists(fKey))
{
    Cache.StringSet(fKey, Serialize(data));
}

where Cache is Database object

I was reading about the redis SET command here http://redis.io/commands/set and found that SET will overwrite existing key value if it already exists. Using StackExchange.Redis can I safely remove the exist check condition and call just -

Cache.StringSet(fKey, Serialize(data));

Appreciate your response.

Vishal Thakur
  • 95
  • 1
  • 9
  • What makes you think you _can't_ remove it safely? At the end of the day, another thread may have put something in the cache between the check for `KeyExists` and `StringSet` anyway. – James Thorpe Jun 10 '15 at 13:13
  • Well do you *want* to overwrite if the key already exists, or do you want to leave the existing value? – Jon Skeet Jun 10 '15 at 13:14
  • Thanks James & Jon, I am fine even if it overwrites. Thank you. – Vishal Thakur Jun 10 '15 at 13:17
  • @VST I'm curious about why you first accepted my answer which exactly answers your question, and later you opted to mark the other one. It's not for the points. It's just because I feel that the other answer has nothing to do with your issue :P – Matías Fidemraizer Jun 10 '15 at 13:29
  • Matias I actually first accepted the answer which is currently accepted & not yours. I was not aware that I cannot accept multiple answers I thought yours is also one of useful answers and marked it correct and realized other is unchecked. About the relevance I felt that the current one is more relevant because My question was more about is it required to do exists check. What you suggested is how can I avoid exists check. So switched back to the other one. Nevertheless thanks for responding - all of you. – Vishal Thakur Jun 10 '15 at 13:37
  • @VST My answer gives you the point of not having to check at all, which is better than the other answer.... Is **not checking** almost the same as **you're not required to do the check**? ;) – Matías Fidemraizer Jun 10 '15 at 13:40
  • Finally I've dropped my answer. I thought you were looking for not overwriting the key if it exists. Since you want the opposite, my answer is useless. – Matías Fidemraizer Jun 10 '15 at 13:49
  • If you want fine-grained control, see the `when` optional parameter; default behavior is flat overwrite, but there is a `When.NotExists` – Marc Gravell Jun 10 '15 at 14:14

2 Answers2

4

yes, you can savely remove that. We also don't check for existence as that would lead to have two access points to the cache where only one is necessary. This would really slow down cache access.

You may want to consider three other things:

  • you may have to make the set action repeatable in case the redis cache is not available in the moment of access.
  • you may have to make the initialization of the connection repeatable
  • please refer to buffer redis stream how to make (de)serialization of redis cache entries reliable and fast.
2

The default behavior is to simply overwrite, so if that is ok for you: you don't need a check. There is also an optional when parameter that allows you to control this more finely - see the NX etc parameter in redis SET documentation to see what this means in reality. For "equality" checks, you can use a transaction with a constraint.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900