1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | | package com.lyh.redis.test;
import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class SingleRedisTest2 {
public Jedis init(){ //一个Jedis对象对应这个一个Redis的实例 Jedis jedis = new Jedis("192.168.29.160",6379); return jedis; } public void destroy(Jedis j){ //在关闭时断开连接,测试发现在操作之前调用这个方法后Jedis实例依然可以使用 j.disconnect(); } @Test public void testList1(){ System.out.println("1S========================================="); Jedis j = init(); j.lpush("Friut","apple","banana","orange","xxx"); System.out.println("llen(Friut):"+j.llen("Friut")); List<String> r = j.lrange("Friut",0,-1); for(String v :r){ System.out.println("1range:"+v); } System.out.println("1E========================================="); System.out.println("2S========================================="); //从左边弹出一个元素,从左边进从组边出类似于一个栈 String lpopValue = j.lpop("Friut"); System.out.println("lpopValue:"+lpopValue+" llen(Friut):"+j.llen("Friut")); List<String> rx = j.lrange("Friut",0,-1); for(String v :rx){ System.out.println("2range:"+v); } System.out.println("2E=========================================");
System.out.println("3S========================================="); //从Friut的右边弹出来一个元素并放入到另一个链表中(这个链表可以是存在的也可以是不存在的) j.rpoplpush("Friut","newList"); List<String> lrange = j.lrange("newList",0,-1); for(String v : lrange){ System.out.println("lrange:"+v); } System.out.println("3E========================================="); // System.out.println("4S========================================="); //获取一个元素 这个元素依然在链表中 String lastElement = j.lindex("newList",j.llen("newList")-1); System.out.println("lastElement:"+lastElement); System.out.println("4E========================================="); // System.out.println("5S========================================="); j.lset("newList", j.llen("newList")-1,"lsetValue"); System.out.println("newList的最后一个值被设置为:"+j.rpop("newList")); System.out.println("5E========================================="); System.out.println("6S========================================="); //从左到有删除 apple元素 2次 j.lrem("Friut",2,"apple"); List<String> r1 = j.lrange("Friut",0,-1); for(String v :r1){ System.out.println("1range:"+v); } System.out.println("6E========================================="); System.out.println("7S========================================="); //移除第4个到倒数第二个之外的所有元素 j.ltrim("Friut",3,j.llen("Friut")-2); List<String> r3 = j.lrange("Friut",0,-1); for(String v :r3){ System.out.println("1range:"+v); } System.out.println("7E========================================="); destroy(j); } @Test public void testList2(){ Jedis j = init(); String keyName = "r2"; j.lpush(keyName,"A","B"); int count = 3; CountDownLatch latch = new CountDownLatch(count); //由于在前面的队列中放入了2个元素现在要循环取3次所以必然会阻塞住 //当手动使用命令往r2列表中添加元素后进程会结束 Bget b = new Bget(j,count,TimeUnit.SECONDS,keyName, latch); new Thread(b).start(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); }finally{ destroy(j); } } }
/** * 模拟Blpop * @author MECHREVO */ class Bget implements Runnable{ private Jedis j; private int count; private TimeUnit unit; private String keyName; private CountDownLatch latch; public Bget(){} public Bget(Jedis j,int count,TimeUnit unit,String keyName,CountDownLatch latch){ this.j = j; this.count = count; this.unit = unit; this.keyName = keyName; this.latch = latch; } @Override public void run() { for(int i=0;i<count;i++){ try{ unit.sleep(1); //阻塞式获取如果keyName列表中没有元素则等待直到超时或者有新元素被添加 List<String> blpop = j.blpop(100,keyName); if(null != blpop){ System.out.println(blpop.get(0)+"-----"+blpop.get(1)); latch.countDown(); } }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println("run over...."); } } |