一、RedisTemplate隊列插入
1、從集合左邊插入值
https://blog.csdn.net/weixin_43658899/article/details/121040307
leftPush(K key, V value)
redisTemplate.opsForList().leftPush("leftdatakey","bbbb");
2、從集合左邊開始在v1值后邊插入新值v2
leftPush(key, v1, v2)
// 在隊列中從左向右查找v1 沒有不插入 有在v1值左側插入v2 多個v1取左數第一個v1
redisTemplate.opsForList().leftPush("leftdatakey", "bb", "cc");
3、從右側插入新值
rightPush(K key, V value)
redisTemplate.opsForList().rightPush("rightdatakey",'bbb');
4、從集合右邊邊開始在v1值后邊插入新值v2
rightPush(key, v1, v2)
// 在隊列中從右向左查找v1 沒有不插入 有在v1值右側插入v2 多個v1取右數第一個v1
redisTemplate.opsForList().rightPush("leftdatakey", "bb", "cc");
5、從左邊批量插入新值
leftPushAll(K key, Collection values)redisTemplate.opsForList().leftPushAll("key", JSONArray.parseArray(JSON.toJSONString(plist)));
6、從右側批量插入
rightPushAll(K , V)
redisTemplate.opsForList().leftPushAll("key", JSONArray.parseArray(JSON.toJSONString(plist)));
7、如果key存在,從左邊插入新值 否則不插入
redisTemplate.opsForList().leftPushIfPresent("key", "a");
8、如果key存在,在右側新插入新值 ,否則不插入
redisTemplate.opsForList().rightPushIfPresent("key", "a");
9、將key作為對象插入隊列
IMPlatformResult<Object> result = new IMPlatformResult<>();result.setUserId(71L);result.setGroupId(73L);String key = StrUtil.join(":", RedisKey.IM_USER_GROUP_MEMBER_TIME,appName);// 插入隊列redisTemplate.opsForList().rightPush(key, JSONObject.toJSONString(result));
二、RedisTemplate隊列刪除
1、默認移除key中最左的一個值
redisTemplate.opsForList().leftPop("key");
2、默認從最右側移除一個值
redisTemplate.opsForList().rightPop("key");
3、指定過期時間后刪除key中最左的一個值
redisTemplate.opsForList().leftPop("key",1,TimeUnit.MINUTES);
4、指定過期時間后刪除key中最右的一個值
redisTemplate.opsForList().rightPop("key",1,TimeUnit.MINUTES);
5、移除k1中最右的值,并將移除的值插入k2中最左側
// k1和k2不是同一個key時,k1右側移除,k2左側插入,k2不存在時則新增一個然后在插入
// k1和k2是同一個key時,相當于把最右側的值移到了最左側
redisTemplate.opsForList().rightPopAndLeftPush("key1", "key2");
6、指定過期時間后,移除k1中最右的值,并將移除的值插入k2中最左側
// k1和k2不是同一個key時,k1右側移除,k2左側插入,k2不存在時則新增一個然后在插入
// k1和k2是同一個key時,相當于把最右側的值移到了最左側
redisTemplate.opsForList().rightPopAndLeftPush("key1", "key2",1,TimeUnit.MINUTES);
7、刪除列隊中指定位置的key值
// 隊列左從0 開始 0/1/2/3
redisTemplate.opsForList().remove(key, 3, "a");
三、RedisTemplate隊列替換
1、在指定坐標位置插入(替換)新值
set(K key, final long index, V value)
index不存在,報錯(ERR index out of range)
key不存在,報錯(ERR no such key)
從左側插入
redisTemplate.opsForList().set("key",2,"a");
2、截取下標long1和long2之間的值,包括long1和long2對應的值,并將其保留為key對應的新值
左側坐標從0開始,右側從-1開始
當long1超過坐標時(此時與long2無關),都會截取為空,key會被刪除
當long1為負時(此時與long2無關),都會截取為空,key會被刪除
當long1為正且在下標存在其中,long2為負數時,只要兩個沒有重疊,相當于去左去右,保留了中間的部分
當long1為正且在下標存在其中,long2為負數時,只要兩個交叉重疊,截取為空,如下圖redisTemplate.opsForList().trim("key",1,3);
四、RedisTemplate隊列查詢
1、獲取隊列中指定下標間的值
redisTemplate.opsForList().range("key", 0, -1);
2、獲取隊列所有值
0代表左側開始 -1代表右側末端
redisTemplate.opsForList().range(key, 0,-1)
3、獲取對應key的集合長度
Long size = redisTemplate.opsForList().size("key");
4、獲取指定位置的值(index從左往右,從0開始)
String string1 = (String) redisTemplate.opsForList().index("key", 2);
5、取隊列集合轉為list集合
List<IMPlatformResult> results = new LinkedList<>();log.info("緩存隊列 " + redisTemplate.opsForList().range(key, 0,-1));List<Object> jsonArray = redisTemplate.opsForList().range(key, 0, -1);if(CollectionUtils.isNotEmpty(jsonArray)){for(Object o: jsonArray){IMPlatformResult b = JSONObject.parseObject(o.toString(),IMPlatformResult.class);results.add(b);}}