Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use StackExchange.Redis to store a Null value in Redis?

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.

like image 273
oatsoda Avatar asked Jun 13 '16 14:06

oatsoda


People also ask

What is StackExchange Redis?

StackExchange. Redis is a high performance general purpose redis client for . NET languages (C#, etc.). It is the logical successor to BookSleeve, and is the client developed-by (and used-by) Stack Exchange for busy sites like Stack Overflow.

What is Redis ConnectionMultiplexer?

The ConnectionMultiplexer is the main arbiter of the connection to Redis inside the CLR, your application should maintain a single instance of the ConnectionMultiplexer throughout its runtime. You can initialize the Multiplexer with either a connection string, or with a ConfigurationOptions object.

What is Rediskey?

Redis Keys are Ideal for Keeping Track of Lots of Strings. For the (simple string) key-value data type Redis allows you to set, get, delete, and increment arbitrary string <key, value> pairs. You can only have 1 value at a time for each key (i.e. set is destructive assignment). You can also delete keys.


1 Answers

This is usual dilemma with nulls. We had similar situations and here is how we dealt with them:

  1. We retrieved list of dates from backend system and stored them in cache. DAL class returned 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.
  2. We used to store users preference as a string in backend. A 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.

like image 196
zendu Avatar answered Oct 02 '22 18:10

zendu