基础配置介绍已经在前面的《RedisTemplate常用集合使用说明(一)》中已经介绍了,现在我们直接介绍boundListOps()方法的使用:
首先要定义一个BoundListOperations
//定义绑定的键
BoundListOperations boundListOperations = redisTemplate.boundListOps("lk");
1.leftPush(V value)和rightPush(V value)
在绑定键中添加值
//在绑定的键中添加值
boundListOperations.leftPush("h");
boundListOperations.leftPush("i");
boundListOperations.rightPush("a");
boundListOperations.rightPush("b");
2.range(long start, long end)
获取绑定键中给定的区间值
//获取绑定的键中的值
boundListOperations.range(0,-1).forEach(v -> System.out.println("获取绑定的键中的值" + v));
3.index(long index)
获取给定位置的值
//获取特定位置的值
System.out.println("获取特定位置的值:" + boundListOperations.index(0));
4.leftPop()
弹出左边的值
//弹出左边的值
System.out.println("弹出左边的值:" + boundListOperations.leftPop());
5.rightPop()
弹出右边的值
//弹出右边的值
System.out.println("弹出右边的值:" + boundListOperations.rightPop());
在指定的第一个值出现的左边添加值
//在指定的出现第一个值左边添加值
boundListOperations.leftPush("i","w");
boundListOperations.range(0,-1).forEach(v -> System.out.println("在指定的出现第一个值左边添加值:" + v));
在指定的第一个值出现的右边添加值
//在指定的出现第一个值右边添加值
boundListOperations.rightPush("i","x");
boundListOperations.range(0,-1).forEach(v -> System.out.println("在指定的出现第一个值左边添加值:" + v));
8.leftPop(long timeout, TimeUnit unit)
在指定的时间后弹出左边的值
//在指定的时间过后弹出左边的值
boundListOperations.leftPop(1, TimeUnit.SECONDS);
9.rightPop(long timeout, TimeUnit unit)
在指定的时间后弹出右边的值
//在指定的时间过后弹出右边的值
boundListOperations.rightPop(1, TimeUnit.SECONDS);
10.leftPushAll(V... values)
在左边批量添加值
//在左边批量添加值
boundListOperations.leftPushAll("y","g");
boundListOperations.range(0,-1).forEach(v -> System.out.println("在左边批量添加值:" + v));
11.rightPushAll(V... values)
在右边批量添加值
//在右边批量添加值
boundListOperations.rightPushAll("b","r");
boundListOperations.range(0,-1).forEach(v -> System.out.println("在右边批量添加值:" + v));
12.leftPushIfPresent(V value)
在左边添加不存在的值
//向左边添加不存在的值
boundListOperations.leftPushIfPresent("k");
boundListOperations.leftPushIfPresent(";");
boundListOperations.range(0,-1).forEach(v -> System.out.println("向左边添加不存在的值:" + v));
13.rightPushIfPresent(V value)
在右边添加不存在的值
//向右边添加不存在的值
boundListOperations.rightPushIfPresent("k");
boundListOperations.rightPushIfPresent(";");
System.out.println("向右边添加不存在的值:" + boundListOperations.range(0,-1).toString());
14.remove(long count, Object value)
移除指定个数的值
//移除指定值的个数
long removeCount = boundListOperations.remove(2,"i");
System.out.println("移除指定值的个数:" + removeCount);
System.out.println("移除指定值的个数后剩余的值:" + boundListOperations.range(0,-1).toString());
在指定位置添加值
//在指定位置设置值
boundListOperations.set(0,"j");
System.out.println("在指定位置设置值:" + boundListOperations.range(0,-1).toString());
16.trim(long start, long end)
截取原来区间的值为新值
//截取指定区间位置的值
boundListOperations.trim(1,3);
System.out.println("截取指定区间位置的值:" + boundListOperations.range(0,-1).toString());