You know, the question already has an answer. And you are already talking about how to solve your problem:
- Use ZSET - enter the time and value as a counter.
- Use HSET - enter the time and value as a counter.
- Use string keys - key name as time and value as counter.
Why only these cases - only of these structures ( ZSET , HSET and string keys ) has atomic methods for increasing values.
So relevant:
- You must choose the right data structure.
- Fix the problem with the data selection.
The first question is the trade-off between memory and performance. From your question, you do not need to have any types if sorting such sorted sets is not the best solution - it consumes a lot of memory and ZINCRBY is the time complexity of O (log (N)) rather HINCRBY and INCRBY O (1) . Therefore, we must choose betweeh hashes and string keys. Look at the question and answer about the correct memory optimization in redis - according to this, I think you should use hashes as the data type for your solution.
The second question is common for all types of data structures, since all their types do not contain select by name functions or their analogues. And we can use HMGET or LUA Scripts to solve this problem. In any case, this solution will have a time complexity of O (n) .
Here is an example with Jedis (I'm not a Java programmer, sorry for possible errors):
int fromMinute = 1; int toMinute = 10; List<String> list = new ArrayList<String>(); for(int i = fromMinute ; i < toMinute ; i++) { list.add(i.toString()); } Jedis jedis = new Jedis("localhost"); List<String> values = jedis.hmget("your_set_name", list);
This solution is atomic, fast, has O (n) time complexity and consumes as little memory as possible in redis.
source share