Search code examples
cachingredisnode-redis

How to store an empty array in redis?


I am using Redis as cache only. I am performing IO expensive data and storing in redis. Eg of data:

[{"fname": "temp.txt", "size": 50, "type":"pdf", "mt": "Timestamp"},{..},...]

Now, due to data being an array of object, I have converted the data into an array of strings (Stringified JSON). The only case when the data is not in cache is if it's evicted or there are changes in data in which case I remove the key-value pair.

MY Concern:
In some cases, I have to store an empty array (no data for key) in Redis. How can I achieve this? The issue is I cannot reevaluate if data for the key is indeed empty or does not exist (in which Redis returns an empty array as well) and since it's an IO-intensive work I don't want to waste resource on reevaluation.

What is the best way to resolve this issue? Any help/suggestion on caching, approach or designing is equally appreciated.


Solution

  • It seems you are not doing any list specific operation (e.g. add element, delete element, read subset, etc.). In that case, you can consider using string instead of list. So you can convert the data into a single string (Stringified JSONArray) instead of an array of strings. And save that string in Redis. An empty array would be stringified as [] and you would be able to differentiate between no data and empty array.