日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

RedisTemplate常用集合使用说明-opsForList(三)

發布時間:2025/3/12 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RedisTemplate常用集合使用说明-opsForList(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 基礎配置介紹已經在前面的《RedisTemplate常用集合使用說明(一)》中已經介紹了,現在我們直接介紹opsForList()方法的使用:

1、leftPush(K key, V value)

在變量左邊添加元素值。

redisTemplate.opsForList().leftPush("list","a"); redisTemplate.opsForList().leftPush("list","b"); redisTemplate.opsForList().leftPush("list","c");

2、index(K key, long index)

獲取集合指定位置的值。

String listValue = redisTemplate.opsForList().index("list",1) + ""; System.out.println("通過index(K key, long index)方法獲取指定位置的值:" + listValue);

3、range(K key, long start, long end)

獲取指定區間的值。

List<Object> list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過range(K key, long start, long end)方法獲取指定范圍的集合值:"+list);

4、leftPush(K key, V pivot, V value)

把最后一個參數值放到指定集合的第一個出現中間參數的前面,如果中間參數值存在的話。

redisTemplate.opsForList().leftPush("list","a","n"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過leftPush(K key, V pivot, V value)方法把值放到指定參數值前面:" + list);

5、leftPushAll(K key, V… values)

向左邊批量添加參數元素。

redisTemplate.opsForList().leftPushAll("list","w","x","y"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過leftPushAll(K key, V... values)方法批量添加元素:" + list);

6、leftPushAll(K key, Collection values)

以集合的方式向左邊批量添加元素。

List newList = new ArrayList(); newList.add("o"); newList.add("p"); newList.add("q"); redisTemplate.opsForList().leftPushAll("list",newList); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過leftPushAll(K key, Collection<V> values)方法以集合的方式批量添加元素:" + list);

7、leftPushIfPresent(K key, V value)

如果存在集合則添加元素。

redisTemplate.opsForList().leftPushIfPresent("presentList","o"); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println("通過leftPushIfPresent(K key, V value)方法向已存在的集合添加元素:" + list);

8、rightPush(K key, V value)

向集合最右邊添加元素。

redisTemplate.opsForList().rightPush("list","w"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過rightPush(K key, V value)方法向最右邊添加元素:" + list);

9、rightPush(K key, V pivot, V value)

向集合中第一次出現第二個參數變量元素的右邊添加第三個參數變量的元素值。

redisTemplate.opsForList().rightPush("list","w","r"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過rightPush(K key, V pivot, V value)方法向最右邊添加元素:" + list);

10、rightPushAll(K key, V… values)

向右邊批量添加元素。

redisTemplate.opsForList().rightPushAll("list","j","k"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過rightPushAll(K key, V... values)方法向最右邊批量添加元素:" + list);

11、rightPushAll(K key, Collection values)

以集合方式向右邊添加元素。

newList.clear(); newList.add("g"); newList.add("h"); redisTemplate.opsForList().rightPushAll("list",newList); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過rightPushAll(K key, Collection<V> values)方法向最右邊以集合方式批量添加元素:" + list);

12、rightPushIfPresent(K key, V value)

向已存在的集合中添加元素。

redisTemplate.opsForList().rightPushIfPresent("presentList","d"); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println("通過rightPushIfPresent(K key, V value)方法已存在的集合向最右邊添加元素:" + list);

13、size(K key)

獲取集合長度。

long listLength = redisTemplate.opsForList().size("list"); System.out.println("通過size(K key)方法獲取集合list的長度為:" + listLength);

14、leftPop(K key)

? 移除集合中的左邊第一個元素。

Object popValue = redisTemplate.opsForList().leftPop("list"); System.out.print("通過leftPop(K key)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("list",0,-1); System.out.println(",剩余的元素是:" + list);

15、leftPop(K key, long timeout, TimeUnit unit)

移除集合中左邊的元素在等待的時間里,如果超過等待的時間仍沒有元素則退出。

popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS); System.out.print("通過leftPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println(",剩余的元素是:" + list);

16、rightPop(K key)

? 移除集合中右邊的元素。

?

popValue = redisTemplate.opsForList().rightPop("list"); System.out.print("通過rightPop(K key)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("list",0,-1); System.out.println(",剩余的元素是:" + list);

17、rightPop(K key, long timeout, TimeUnit unit)

移除集合中右邊的元素在等待的時間里,如果超過等待的時間仍沒有元素則退出。

popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS); System.out.print("通過rightPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println(",剩余的元素是:" + list);

18、rightPopAndLeftPush(K sourceKey, K destinationKey)

移除集合中右邊的元素,同時在左邊加入一個元素。

popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12"); System.out.print("通過rightPopAndLeftPush(K sourceKey, K destinationKey)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("list",0,-1); System.out.println(",剩余的元素是:" + list);

19、rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)

移除集合中右邊的元素在等待的時間里,同時在左邊添加元素,如果超過等待的時間仍沒有元素則退出。

popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS); System.out.println("通過rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)方法移除的元素是:" + popValue); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.print(",剩余的元素是:" + list);

20、set(K key, long index, V value)

*在集合的指定位置插入元素*,如果指定位置已有元素,則覆蓋,沒有則新增,超過集合下標*+n*則會報錯。

redisTemplate.opsForList().set("presentList",3,"15"); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.print("通過set(K key, long index, V value)方法在指定位置添加元素后:" + list);

21*、remove(K key, long count, Object value)

從存儲在鍵中的列表中刪除等于值的元素的第一個計數事件。count> 0:刪除等于從左到右移動的值的第一個元素;count< 0:刪除等于從右到左移動的值的第一個元素;count = 0:刪除等于value的所有元素。

long removeCount = redisTemplate.opsForList().remove("list",0,"w"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過remove(K key, long count, Object value)方法移除元素數量:" + removeCount); System.out.println(",剩余的元素:" + list);

22、trim(K key, long start, long end)

截取集合元素長度,保留長度內的數據。

redisTemplate.opsForList().trim("list",0,5); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通過trim(K key, long start, long end)方法截取后剩余元素:" + list);

總結

以上是生活随笔為你收集整理的RedisTemplate常用集合使用说明-opsForList(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。