Redis使用汇总2 list命令

近期有空,汇总下redis各个数据结构的操作命令。这篇汇总list命令用于加深记忆。

BLPOP

blpop命令用于阻塞式弹出列表元素,当给定列表中没有任何元素可弹出,连接将阻塞。当给定多个key时,连接会按key的顺序依次检索各个列表并且弹出第一个非空列表的元素。返回的结果为弹出的元素列表对应的key以及被弹出的元素。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append", "a", "b", "c");
		jedis.lpush("append2", "a", "b", "c");
		jedis.lpush("append3", "a", "b", "c");
		System.out.println("blpop命令执行的结果为" + jedis.blpop(5, "append5", "append4", "append3", "append2", "append"));
                blpop命令执行的结果为[append3, c]

一般来说,使用blpop命令会同时设置一个连接超时时间,当blpop命令中的key都没有元素可以pop出来,连接会进入阻塞状态,当进入阻塞状态的时间超出设置的超时时间,redis会返回null。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append", "a", "b", "c");
		jedis.lpush("append2", "a", "b", "c");
		jedis.lpush("append3", "a", "b", "c");
		System.out.println("blpop命令执行的结果为 " + jedis.blpop(5, "append5", "append4", "append3", "append2", "append"));
		System.out.println("blpop命令执行的结果为 " + jedis.blpop(5, "append5", "append4"));
                blpop命令执行的结果为 [append3, c]
                blpop命令执行的结果为 null

BRPOP

brpop命令与blpop命令一样,唯一的区别就是从右侧弹出元素。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append", "a", "b", "c");
		jedis.lpush("append2", "a", "b", "c");
		jedis.lpush("append3", "a", "b", "c");
		System.out.println("brpop命令执行的结果为 " + jedis.brpop(5, "append5", "append4", "append3", "append2", "append"));
		System.out.println("brpop命令执行的结果为 " + jedis.brpop(5, "append5", "append4"));
                brpop命令执行的结果为 [append3, a]
                brpop命令执行的结果为 null

RPOPLPUSH

rpoplpush命令用于将目标列表的尾元素移除并存储在另一个列表的头部。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append", "a", "b", "c");
		jedis.lpush("append2", "v", "w", "z");
		System.out.println("rpoplpush命令执行的结果 " + jedis.rpoplpush("append", "append2"));
		List<String> append = jedis.lrange("append", 0, -1);
		List<String> append2 = jedis.lrange("append2", 0, -1);
		for (String string : append) {
			System.out.print(string + " ");
		}
		System.out.println();
		for (String string : append2) {
			System.out.print(string + " ");
		}
                rpoplpush命令执行的结果 a
                c b 
                a z w v 

BRPOPLPUSH

brpoplpush命令与rpoplpush命令相似,唯一的不同是当目标列表没有元素可弹出时连接会进入阻塞状态,当有元素push入列表即接触阻塞状态并执行命令,若超出设置的超时时间则返回null。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append2", "v", "w", "z");
		System.out.println("brpoplpush命令执行的结果 " + jedis.brpoplpush("append3", "append2", 5));
		List<String> append2 = jedis.lrange("append2", 0, -1);
		for (String string : append2) {
			System.out.print(string + " ");
		}
                brpoplpush命令执行的结果 null
                z w v 

LINDEX

lindex命令用于返回执行key中index对应的value,超出范围的index所获取的值为null。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append3", "a", "b", "c", "c", "d", "e");
		System.out.println("lindex命令执行结果 " + jedis.lindex("append3", 2));
		System.out.println("lindex命令执行结果 " + jedis.lindex("append3", -2));
		System.out.println("lindex命令执行结果 " + jedis.lindex("append3", 12));
                lindex命令执行结果 c
                lindex命令执行结果 b
                lindex命令执行结果 null

LINSERT

linsert命令用于在list中目标元素的前面或后面插入元素,若找不到目标元素则返回-1,命令执行成功则返回当前list的元素数量。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append4", "a", "b", "c", "d", "e");
		System.out.println("linsert命令执行结果 " + jedis.linsert("append4", LIST_POSITION.AFTER, "b", "z"));
		for (String string : jedis.lrange("append4", 0, -1)) {
			System.out.print(string + " ");
		}
		System.out.println();
		System.out.println("linsert命令执行结果 " + jedis.linsert("append4", LIST_POSITION.BEFORE, "b", "x"));
		for (String string : jedis.lrange("append4", 0, -1)) {
			System.out.print(string + " ");
		}
		System.out.println();
		System.out.println("linsert命令执行结果 " + jedis.linsert("append4", LIST_POSITION.BEFORE, "s", "x"));
                linsert命令执行结果 6
                e d c b z a 
                linsert命令执行结果 7
                e d c x b z a 
                linsert命令执行结果 -1

LLEN

llen用于返回list的长度,若key不存在则返回0。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append4", "a", "b", "c", "d", "e");
		System.out.println("llen执行的结果 " + jedis.llen("append4"));
		System.out.println("llen执行的结果 " + jedis.llen("append5"));
                llen执行的结果 5
                llen执行的结果 0

LPOP

lpop命令用于返回第一个元素,没有元素可返回则返回null。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "c", "d", "e");
		System.out.println("lpop执行的结果 " + jedis.lpop("append4"));
		System.out.println("lpop执行的结果 " + jedis.lpop("append5"));
                lpop执行的结果 e
                lpop执行的结果 null

LPUSH

lpush命令用于在列表头插入元素,可以指定多个插入的元素。执行结果返回list的长度。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		System.out.println("lpush命令执行的结果 " + jedis.lpush("append1", "a", "b", "c", "d", "e"));
		for (String string : jedis.lrange("append1", 0, -1)) {
			System.out.print(string + " ");
		}
                lpush命令执行的结果 5
                e d c b a 

LPUSHX

lpushx命令用于在表头插入数据,与lpush不同的是,只有当key存在且已包含元素的情况下才能插入成功,否则不执行操作。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "c", "d", "e");
		System.out.println("lpushx命令执行的结果 " + jedis.lpushx("append1", "f"));
		for (String string : jedis.lrange("append1", 0, -1)) {
			System.out.print(string + " ");
		}
		System.out.println();
		System.out.println("lpushx命令执行的结果 " + jedis.lpushx("append2", "f"));
		for (String string : jedis.lrange("append2", 0, -1)) {
			System.out.print(string + " ");
		}
                lpushx命令执行的结果 6
                f e d c b a 
                lpushx命令执行的结果 0

LRANGE

lrange命令用于获取list中的元素。元素的范围包含边界。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "c", "d", "e");
		for (String string : jedis.lrange("append1", 1, 3)) {
			System.out.print(string + " ");
		}
                d c b 

LREM

lrem命令用于移除指定的value。可以通过count参数指定需要移除的value数。

    count < 0 为从表尾移除count个value。

    count = 0 为移除全部value。

    count < 0 为从表头移除count个value。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "e", "e", "c", "d", "e");
		jedis.lpush("append2", "a", "b", "e", "e", "c", "d", "e");
		jedis.lpush("append3", "a", "b", "e", "e", "c", "d", "e");
		jedis.lpush("append4", "a", "b", "e", "e", "c", "d", "e");
		jedis.lpush("append5", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("lrem命令执行结果 " + jedis.lrem("append1", -2, "e"));
		System.out.println("lrem命令执行结果 " + jedis.lrem("append2", -5, "e"));
		System.out.println("lrem命令执行结果 " + jedis.lrem("append3", 0, "e"));
		System.out.println("lrem命令执行结果 " + jedis.lrem("append4", 2, "e"));
		System.out.println("lrem命令执行结果 " + jedis.lrem("append5", 5, "e"));
                lrem命令执行结果 2
                lrem命令执行结果 3
                lrem命令执行结果 3
                lrem命令执行结果 2
                lrem命令执行结果 3

LSET

lset用于设置list中的元素值。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("lset命令执行结果 " + jedis.lset("append1", 2, "x"));
		for (String string : jedis.lrange("append1", 0, -1)) {
			System.out.print(string + " ");
		}
                lset命令执行结果 OK
                e d x e e b a 

LTRIM

ltrim命令用于截取list。当start大于end则该key会被删除,当end超出最大长度则默认截取到最大长度。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("ltrim命令执行结果 " + jedis.ltrim("append1", 2, 2));
		for (String string : jedis.lrange("append1", 0, -1)) {
			System.out.print(string + " ");
		}
		System.out.println();
		jedis.lpush("append2", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("ltrim命令执行结果 " + jedis.ltrim("append2", 12, 2));
		for (String string : jedis.lrange("append2", 0, -1)) {
			System.out.print(string + " ");
		}
		System.out.println();
		jedis.lpush("append3", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("ltrim命令执行结果 " + jedis.ltrim("append3", 2, 5));
		for (String string : jedis.lrange("append3", 0, -1)) {
			System.out.print(string + " ");
		}
                ltrim命令执行结果 OK
                c 
                ltrim命令执行结果 OK

                ltrim命令执行结果 OK
                c e e b 

RPOP

rpop命令用于弹出最后一个元素。若无元素可弹出则返回null。

		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.lpush("append1", "a", "b", "e", "e", "c", "d", "e");
		System.out.println("rpop命令执行结果 " + jedis.rpop("append1"));
		System.out.println("rpop命令执行结果 " + jedis.rpop("append2"));
                rpop命令执行结果 c
                rpop命令执行结果 null

RPUSH

rpush命令用于在列表最后添加元素。返回列表长度。

		Jedis jedis = new Jedis("127.0.0.1", 6379);		
		System.out.println("rpush命令执行结果 " + jedis.rpush("append1", "a", "b", "e", "e", "c", "d", "e"));
		for (String string : jedis.lrange("append1", 0, -1)) {
			System.out.print(string + " ");
		}
                rpush命令执行结果 7
                a b e e c d e 

RPUSHX

rpushx命令与rpush命令类似,不同的是若该key不存在则不执行操作。

		Jedis jedis = new Jedis("127.0.0.1", 6379);		
		System.out.println("rpushx命令执行结果 " + jedis.rpushx("append1", "xxx"));
		jedis.rpush("append1", "sss");
		System.out.println("rpushx命令执行结果 " + jedis.rpushx("append1", "xxx"));
                rpushx命令执行结果 0
                rpushx命令执行结果 2

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值