RedisTemlale操作List测试类

来源:互联网 发布:中国云计算大会 2017 编辑:程序博客网 时间:2024/05/21 15:06
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath*:/applicationContext-test.xml"})
public classRestListTestextendsUnitilsJUnit4{

@Resource(name= "redisTemplate")
privateListOperations<String,Long>opsForList;
@Autowired
privateRedisCacheUtilredisCacheUtil;

@Test
public voidtest1() {
//问题下的回答id列表;
List<Long>answerIdsByQuestion= newArrayList<>();
Stringkey ="test_questionId"+ 3;
redisCacheUtil.delete(key);
Listlist2 = newArrayList();//注意这里虽然是Long类型但是不能用泛型标注为Long,不然会报错;
list2.add(11L);
list2.add(12L);
list2.add(13L);
list2.add(14L);
list2.add(17L);
list2.add(15L);
list2.add(16L);
list2.add(17L);
list2.add(18L);
list2.add(17L);
list2.add(19L);
opsForList.rightPushAll(key,list2);//批量添加;
opsForList.leftPush(key,9L);//左边添加一位元素;
opsForList.rightPush(key,21L);//列表的右边添加1位元素
List<Long>range1 = opsForList.range(key,0, -1);// 0,-1获取所有的元素,可以用来做分页;
LongaLong1 = opsForList.leftPop(key);//弹出左边的元素,(获取并移除)
List<Long>range2 = opsForList.range(key,0, -1);
LongaLong2 = opsForList.rightPop(key);//弹出右边的元素,(获取并移除)
List<Long>range3 = opsForList.range(key,0, -1);
Longremove1 = opsForList.remove(key,1,16L);//移除从左到右第一个值为16的元素
Longremove2 = opsForList.remove(key,2,17L);//移除从左到右两个值为17的元素
Longindex = opsForList.index(key,2);//获取索引为2的元素
Longsize = opsForList.size(key);
//获取分页数据 第2,每页3条数据
intpageNo = 2;
intpageSize = 3;
List<Long>range4 = opsForList.range(key,(pageNo- 1)* pageSize,(pageNo* pageSize)- 1);

List<Long>range5 = opsForList.range(key,0, -1);
opsForList.trim(key,3,5);//移除索引3-5以外的所有元素;
List<Long>range6 = opsForList.range(key,0, -1);
}

}
原创粉丝点击