原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/14661797.html
zset常规操作
127.0.0.1:6379> zadd my_key 1 one (integer) 1 127.0.0.1:6379> zadd my_key 2 two (integer) 1 127.0.0.1:6379> zadd my_key 3 three (integer) 1 127.0.0.1:6379> zadd my_key 4 four (integer) 1 127.0.0.1:6379> zrange my_key 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 127.0.0.1:6379>
以每个小时作为zset的key
先把当前时间转换为 毫秒 的时间戳,然后除以1小时,即 小时序号 = System.currentTimeMillis() / 1000 / 60 / 60
然后用这个 小时序号 作为zset的key,zadd 小时序号 score member
package org.fool.test; public class Test { public static void main(String[] args) { System.out.println(System.currentTimeMillis() / 1000 / 60 / 60); // 2021-04-15 10:00:00 long l1 = 1618452000000L; // 449570 System.out.println(l1 / 1000 / 60 / 60); // 2021-04-15 10:30:00 long l2 = 1618453800000L; // 449570 System.out.println(l2 / 1000 / 60 / 60); // 2021-04-15 10:59:59 long l3 = 1618455599000L; // 449570 System.out.println(l3 / 1000 / 60 / 60); // 2021-04-15 11:00:00 long l4 = 1618455600000L; // 449571 System.out.println(l4 / 1000 / 60 / 60); } }