Reputation: 2178
Is there any way to use the StackExchange.Redis client to store a Null value in Redis?
If you use IDatabase.StringGet(key)
then the returning of Null is used to signify that there "was no result". Therefore if you use IDatabase.StringSet(key, null)
then the result is you don't know whether there is a Null value or No value!
Is there any mechanism for catering for this? - i.e. you want to cache a negative.
I was rather hoping to avoid any nasty sentinel values (like value ?? new byte[] {1}
) that could later cause issues if a value happened to match!
I had a look at RedisValue.Null
but that just yields a Null which has the same issues as above.
Upvotes: 16
Views: 16471
Reputation: 1216
This is usual dilemma with nulls. We had similar situations and here is how we dealt with them:
null
if no dates were retrieved and threw an exception in case of error/timeout communicating with backend system. In order to cache null value, we replaced null with empty list - which still signified that there was no date available.null
value signified that user had not set preference. To cache this, we cached special string value that would never occur in the use case - something like NO PREFERENCE.HTH.
Upvotes: 4
Reputation: 944
I'm using a proxy pattern to reduce calls to the database. The only reasonable way I found was to store an empty byte array in Redis.
byte[] array = new byte[0];
IDatabase.StringSet(key, array)
When querying Redis you can use value.IsNull
to check if byte array is empty or not.
This way seems preferable to storing 0
, ""
or any other arbitrary value.
Upvotes: 1