Redis(RedisTemplate)使用list链表
生活随笔
收集整理的這篇文章主要介紹了
Redis(RedisTemplate)使用list链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html
package com.wbg.springRedis.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.connection.RedisListCommands; import org.springframework.data.redis.core.RedisTemplate;import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit;public class TestList {static RedisTemplate redisTemplate = null;public static void main(String[] args) throws UnsupportedEncodingException {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-redis.xml");redisTemplate = applicationContext.getBean(RedisTemplate.class);//刪除鏈表if (redisTemplate.hasKey("list"))redisTemplate.delete("list");//那node3插入到list鏈表redisTemplate.opsForList().leftPush("list", "node1");print();List<String> list = new ArrayList<>();for (int i = 2; i < 5; i++) {list.add("node" + i);}//相當lpush把多個值從左插入到鏈表redisTemplate.opsForList().leftPushAll("list", list);print();//右邊插入一個節點redisTemplate.opsForList().rightPushAll("list", "node6");//獲取下標為0的節點System.out.println(redisTemplate.opsForList().index("list", 0));//獲取鏈表的長度System.out.println(redisTemplate.opsForList().size("list"));//彈出(刪除)左邊一個節點System.out.println(redisTemplate.opsForList().leftPop("list"));//彈出(刪除)右邊一個節點System.out.println(redisTemplate.opsForList().rightPop("list"));print();//需要使用更為底層的命令才能操作linsert命令//在node2前插入before_node節點 RedisListCommands.Position.BEFORE, redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"),RedisListCommands.Position.BEFORE,"node2".getBytes("utf-8"),"before_node".getBytes("utf-8"));print();//在node2后插入after_node節點 RedisListCommands.Position.AFTER, redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"),RedisListCommands.Position.AFTER,"node2".getBytes("utf-8"),"after_node".getBytes("utf-8"));print();//如果list存在 左邊插入redisTemplate.opsForList().leftPushIfPresent("list", "leftEx");//如果list存在 右邊插入redisTemplate.opsForList().rightPushIfPresent("list", "rightEx");//左到右 下獲取標從0-10節點元素list = redisTemplate.opsForList().range("list", 0, 5);System.out.println(list);//左到右刪除多個node節點redisTemplate.opsForList().remove("list", 3, "node");//給鏈表下標設置新值redisTemplate.opsForList().set("list", 0, "new_value");print();/*-------------------Spring對Redis阻塞命令的操作--------*///清空數據redisTemplate.delete("list");redisTemplate.delete("list1");List<String> nodeList = new ArrayList<>();for (int i = 0; i < 5; i++) {nodeList.add("node"+i);}redisTemplate.opsForList().leftPushAll("list1",nodeList);//相當與blpop命令 可以設置時間參數redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);nodeList.clear();for (int i = 0; i < 2; i++) {nodeList.add("data"+i);}redisTemplate.opsForList().leftPushAll("list",nodeList);redisTemplate.opsForList().rightPopAndLeftPush("list","list1");redisTemplate.opsForList().rightPopAndLeftPush("list","list1",1,TimeUnit.SECONDS);}public static void print() {System.out.println(redisTemplate.opsForList().range("list", 0, redisTemplate.opsForList().size("list")));} }?
轉載于:https://www.cnblogs.com/weibanggang/p/10189331.html
總結
以上是生活随笔為你收集整理的Redis(RedisTemplate)使用list链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 史蒂夫乔布斯的最后一代手机 永不落幕的的
- 下一篇: Airtest自动化测试工具介绍