RedisTemplate使用ListOperations專門操做list列表。首先初始化spring工廠得到redisTemplate和opsForListjava
private RedisTemplate<String,Object> redisTemplate; private ListOperations<String, Object> opsForList; @SuppressWarnings("unchecked") @Before public void before(){ @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml"); redisTemplate = (RedisTemplate<String,Object>)context.getBean("redisTemplate"); opsForList = redisTemplate.opsForList(); }
@Test public void testLeftPush(){ redisTemplate.delete("li1"); //先進後出(左進左出) opsForList.leftPush("li1", "a"); opsForList.leftPush("li1", "b"); opsForList.leftPush("li1", "c"); List<Object> range = opsForList.range("li1", 0, -1); for (Object object : range) { System.out.println(object);//c b a } opsForList.leftPush("li1", "d"); opsForList.leftPush("li1", "e"); opsForList.leftPush("li1", "f"); List<Object> range1 = opsForList.range("li1", 0, -1); for (Object object : range1) { System.out.println(object);//f e d c b a } }
@Test public void testTrim(){ redisTemplate.delete("li2"); opsForList.leftPush("li2", "a"); opsForList.leftPush("li2", "b"); opsForList.leftPush("li2", "c"); opsForList.trim("li2", 1, -1); List<Object> range = opsForList.range("li2", 0, -1); for (Object object : range) { System.out.println(object);//b a } }
@Test public void testSize(){ redisTemplate.delete("li3"); System.out.println(opsForList.size("li3"));//0 opsForList.leftPush("li3", "a"); System.out.println(opsForList.size("li3"));//1 }
@Test public void testLeftPushAll(){ redisTemplate.delete("li4"); opsForList.leftPushAll("li4", "a","b","c"); System.out.println(opsForList.size("li4"));//3 List<Object> param = new ArrayList<Object>(); param.add("d"); param.add("e"); param.add("f"); opsForList.leftPushAll("li4", param); System.out.println(opsForList.size("li4"));//6 opsForList.leftPushAll("li4", new Object[]{"g","h"}); System.out.println(opsForList.size("li4"));//8 List<Object> range = opsForList.range("li4", 0, -1); for (Object object : range) { System.out.println(object);//h g f e d c b a } opsForList.leftPush("li4", param); System.out.println(opsForList.size("li4"));//9 opsForList.leftPush("li4", new Object[]{"g","h"}); System.out.println(opsForList.size("li4"));//10 List<Object> range1 = opsForList.range("li4", 0, -1); for (Object object : range1) { System.out.println(object); /* [Ljava.lang.Object;@6d06654b [d, e, f] h g f e d c b a */ } }
@Test public void testLeftPushAllCollection(){ redisTemplate.delete("li5"); List<Object> strs = new ArrayList<Object>(); strs.add("a"); strs.add("b"); strs.add("c"); opsForList.leftPushAll("li5", strs); List<Object> range = opsForList.range("li5", 0, -1); for (Object object : range) { System.out.println(object);//c b a } }
@Test public void testLeftPushIfPresent(){ redisTemplate.delete("li6"); opsForList.leftPushIfPresent("li6", "a"); System.out.println(opsForList.size("li6"));//0 opsForList.leftPush("li6", "a"); opsForList.leftPushIfPresent("li6", "b"); System.out.println(opsForList.size("li6"));//2 }
@Test public void testRightPush(){ redisTemplate.delete("li7"); //先進先出(右進左出) opsForList.rightPush("li7", "a"); opsForList.rightPush("li7", "b"); opsForList.rightPush("li7", "c"); List<Object> range = opsForList.range("li7", 0, -1); for (Object object : range) { System.out.println(object);//a b c } }
@Test public void testRightPushAll(){ redisTemplate.delete("li8"); opsForList.rightPushAll("li8", "a","b","c"); List<Object> range = opsForList.range("li8", 0, -1); for (Object object : range) { System.out.println(object);//a b c } }
@Test public void testRightPushAllCollection(){ redisTemplate.delete("li9"); List<Object> param = new ArrayList<Object>(); param.add("a"); param.add("b"); param.add("c"); opsForList.rightPushAll("li9", param); List<Object> range = opsForList.range("li9", 0, -1); for (Object object : range) { System.out.println(object);//a b c } }
@Test public void testRightPushIfPresent(){ redisTemplate.delete("li10"); opsForList.rightPushIfPresent("li10", "a"); System.out.println(opsForList.size("li10"));//0 opsForList.rightPush("li10", "a"); opsForList.rightPushIfPresent("li10", "b"); System.out.println(opsForList.size("li10"));//2 }
@Test public void testRightPushPivot(){ redisTemplate.delete("li11"); //把value值放到key對應列表中pivot值的右面,若是pivot值存在的話 opsForList.rightPush("li11", "a","b"); System.out.println(opsForList.size("li11"));//0 opsForList.rightPush("li11", "a"); opsForList.rightPush("li11", "a","b"); List<Object> range = opsForList.range("li11", 0, -1); for (Object object : range) { System.out.println(object);//a b } }
@Test public void testSetThrowExc1(){ redisTemplate.delete("li12"); opsForList.rightPush("li12", "a"); opsForList.set("li12", 2, "h");//ERR INDEX OUT OF RANGE } @Test public void testSetThrowExc2(){ redisTemplate.delete("li13"); opsForList.set("li13", 2, "a");//ERR NO SUCH KEY } @Test public void testSet(){ redisTemplate.delete("li14"); opsForList.rightPush("li14", "a"); System.out.println(opsForList.range("li14", 0, -1));//[a] opsForList.set("li14", 0, "b"); System.out.println(opsForList.range("li14", 0, -1));//[b] }
@Test public void testRemove(){ redisTemplate.delete("li15"); opsForList.rightPush("li15", "a"); opsForList.rightPush("li15", "b"); opsForList.rightPush("li15", "c"); opsForList.rightPush("li15", "b"); opsForList.rightPush("li15", "a"); opsForList.remove("li15", 1, "b");//將刪除列表中存儲在列表中第一次出現的"b" List<Object> list = opsForList.range("li15", 0, -1); for (Object object : list) { System.out.println(object);//a c b a } }
@Test public void testIndex(){ redisTemplate.delete("li16"); opsForList.rightPush("li16", "a"); opsForList.rightPush("li16", 1); opsForList.rightPush("li16", new User()); opsForList.rightPush("li16", "c"); opsForList.rightPush("li16", "b"); Object index = opsForList.index("li16", 2); System.out.println(index);//結果:User [id=null, username=null, password=null] }
@Test public void testLeftPop(){ redisTemplate.delete("li17"); opsForList.rightPush("li17", 1); opsForList.rightPush("li17", 2); opsForList.rightPush("li17", 3); opsForList.leftPop("li17"); List<Object> range = opsForList.range("li17", 0, -1); for (Object object : range) { System.out.println(object);//結果:2 3 } }
@Test public void testLeftPopTimeout(){ //彈出隊列最左邊的元素,若是沒有則保持線程,等有了再彈出,設有超時時間,當過了超時時間,則線程終止 opsForList.leftPop("li18",5,TimeUnit.SECONDS); }
@Test public void testRightPopAndLeftPush(){ //從一個隊列的右邊彈出一個元素並將這個元素放入另外一個指定隊列的最左邊 redisTemplate.delete("li18"); redisTemplate.delete("li19"); opsForList.rightPushAll("li18", "a","b","c"); opsForList.rightPopAndLeftPush("li18", "li19"); List<Object> range = opsForList.range("li18", 0, -1); for (Object object : range) { System.out.println(object);//結果:a b } List<Object> range1 = opsForList.range("li19", 0, -1); for (Object object : range1) { System.out.println(object);//結果:c } }
這裏用一個形象的故事來呈現該API的具體場景,同窗們能夠複製源代碼粘貼到本身的電腦上運行玩一下程序員
@Test public void testRightPopAndLeftPushTimeOut(){ //與RightPopAndLeftPush類似,只是添加了超時機制 //下面用一個簡單的小故事呈現一下超時機制 redisTemplate.delete("li20"); redisTemplate.delete("li21"); try { System.out.println("一天,li21來找li20還錢。"); Thread.sleep(3000); System.out.println("可是li20當時身上全部資產的個數爲:"+opsForList.size("li20")+"個,怎麼可能有錢還"); Thread.sleep(5000); System.out.println("而此時li21身上的資產個數爲"+opsForList.size("li21")+"個,他一分錢都沒有,他是真的缺錢!"); Thread.sleep(5000); System.out.println("li20讓li21等一下子試試,也許某個傻逼程序員會施捨個東西給他,到時候就拿那個東西給li21還債!li21決定等上5秒試試。"); Thread.sleep(7000); Runnable runnable = new Runnable() { @Override public void run() { for(int i = 5;i>0;i--){ try { Thread.sleep(1000); System.out.println(i); } catch (InterruptedException e) { e.printStackTrace(); } } } }; Thread thread = new Thread(runnable); thread.start(); opsForList.rightPopAndLeftPush("li20", "li21", 5, TimeUnit.SECONDS); Thread.sleep(1000); System.out.println("5秒後,li21什麼都沒等到。此時li21的資產個數爲:"+opsForList.size("li21")+"個,li20的資產個數爲:"+opsForList.size("li20")+"個。li20讓他再等15秒試試。li21答應了!"); Thread.sleep(7000); Runnable runnable1 = new Runnable() { @Override public void run() { for(int i = 15;i>0;i--){ try { Thread.sleep(1000); if(i==11){ System.out.println("果然,還沒到15秒,傻逼程序員真來了!還塞給了li20一個a"); Thread.sleep(5000); opsForList.rightPush("li20", "a"); System.out.println("這個時候,li20把他惟一一個元素a給了li21"); return; }else{ System.out.println(i); } } catch (InterruptedException e) { e.printStackTrace(); } } } }; Thread thread1 = new Thread(runnable1); thread1.start(); opsForList.rightPopAndLeftPush("li20", "li21", 20, TimeUnit.SECONDS); Thread.sleep(5000); System.out.println("如今li20身上的元素有"+opsForList.size("li20")+"個,而li21獲得了"+opsForList.range("li21", 0, 0)+",資產變成了"+opsForList.size("li21")+"個!"); Thread.sleep(5000); System.out.println("如今li20又變得一窮二白了,固然,能夠看出,他是一個言而有信的人!li20心想:傻逼程序員真的是傻逼程序員!"); } catch (InterruptedException e) { e.printStackTrace(); } }
轉載自:https://blog.csdn.net/weixin_37490221/article/details/78134748redis