使用Jedis操作Redis列表
@Test
public void testList(){
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
Jedis jedis = jedisPool.getResource();
jedis.auth("123456");
String tempKey = "list";
jedis.lpush(tempKey, "aa");
System.out.println("在左边入队一个元素后:" + jedis.lrange(tempKey, 0, -1));
jedis.lpush(tempKey, "bb", "cc");
System.out.println("在左边入队两个元素后:" + jedis.lrange(tempKey, 0, -1));
jedis.rpush(tempKey, "dd");
System.out.println("在右边入队一个元素后:" + jedis.lrange(tempKey, 0, -1));
jedis.rpush(tempKey, "ee", "ff");
System.out.println("在右边入队一个元素后:" + jedis.lrange(tempKey, 0, -1));
jedis.linsert(tempKey, ListPosition.AFTER, "ee", "gg");
System.out.println("在元素ee后面插入元素gg: " + jedis.lrange(tempKey, 0, -1));
jedis.linsert(tempKey, ListPosition.BEFORE, "ee", "hh");
System.out.println("在元素ee前面插入元素hh: " + jedis.lrange(tempKey, 0, -1));
String indexValue = jedis.lindex(tempKey, 2);
System.out.println("list中索引为2处的元素为:" + indexValue);
jedis.lset(tempKey, 2, "newValue");
System.out.println("list中索引为2处的元素为:" + jedis.lindex(tempKey, 2));
jedis.lrem(tempKey, 1, "ee");
System.out.println("从list中移除1个元素:" + jedis.lrange(tempKey, 0, -1));
jedis.linsert(tempKey, ListPosition.BEFORE, "bb", "aa");
jedis.linsert(tempKey, ListPosition.AFTER, "dd", "aa");
jedis.linsert(tempKey, ListPosition.AFTER, "hh", "aa");
System.out.println("批量移除元素前:" + jedis.lrange(tempKey, 0, -1));
jedis.lrem(tempKey, 2, "aa");
System.out.println("从list中从左开始移除2个元素aa:" + jedis.lrange(tempKey, 0, -1));
String ltrim = jedis.ltrim(tempKey, 0, 3);
System.out.println(ltrim);
System.out.println("将list进行切割:" + jedis.lrange(tempKey, 0 , -1));
long lpushx = jedis.lpushx("tempList", "a", "b", "c");
System.out.println("lpushxResult: " + lpushx);
System.out.println("lpushx: " + jedis.lrange("tempList", 0, -1));
jedis.lpushx("tempList", "a", "b", "c");
System.out.println("again lpushxResult: " + lpushx);
System.out.println("again lpushx: " + jedis.lrange("tempList", 0, -1));
jedis.lpush("tempList", "aaa");
long rpushx = jedis.rpushx("rpushList", "a", "b", "c");
System.out.println("rpushxResult: " + rpushx);
System.out.println("rpushx: " + jedis.lrange("rpushList", 0, -1));
jedis.lpush("rpushList", "x");
long rpushx1 = jedis.rpushx("rpushList", "a", "b", "c");
System.out.println("rpushx1Result: " + rpushx1);
System.out.println("rpushx:" + jedis.lrange("rpushList", 0, -1));
jedis.lpop(tempKey);
System.out.println("在左边弹出一个元素后:" + jedis.lrange(tempKey, 0, -1));
jedis.rpop(tempKey);
System.out.println("在右边弹出一个元素后:" + jedis.lrange(tempKey, 0, -1));
System.out.println("list中元素数量:" + jedis.llen(tempKey));
List<String> blpop = jedis.blpop(60, tempKey);
System.out.println("阻塞弹出左边的元素:" + blpop);
List<String> brpop = jedis.brpop(60, tempKey);
System.out.println("阻塞弹出右边的元素:" + brpop);
System.out.println("list中最左边元素:" + jedis.lindex(tempKey, -1));
System.out.println("list中元素:" + jedis.lrange(tempKey, 0, -1));
System.out.println("toList中元素:" + jedis.lrange("toList", 0, -1));
jedis.lpush(tempKey, "xx", "yy");
jedis.rpoplpush(tempKey, "toList");
System.out.println("list中元素:" + jedis.lrange(tempKey, 0, -1));
System.out.println("toList中元素:" + jedis.lrange("toList", 0, -1));
String brpoplpush = jedis.brpoplpush(tempKey, "toList", 10);
System.out.println("元素:" + brpoplpush);
System.out.println("list中元素:" + jedis.lrange(tempKey, 0, -1));
System.out.println("toList中元素:" + jedis.lrange("toList", 0, -1));
}
模拟一个简单的监听 用于测试阻塞入队出队
@Test
public void testBlock(){
JedisPool tempJedisPool = new JedisPool("127.0.0.1", 6379);
Jedis tempJedis = tempJedisPool.getResource();
tempJedis.auth("123456");
while (true){
String brpoplpush = tempJedis.brpoplpush("tempList", "toList", 10);
if (brpoplpush != null){
System.out.println("阻塞弹出:" + brpoplpush);
System.out.println(tempJedis.lrange("tempList", 0, -1));
System.out.println(tempJedis.lrange("toList", 0, -1));
return;
}
}
}