嘟嘟程序猿鸿蒙志每天写1000字

spring-data-redis 的使用之Redis的List

2019-07-17  本文已影响3人  半子胜青天

Redis的List数据结构

这边我们把RedisTemplate序列化方式改回之前的
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
       template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

public interface ListOperations<K,V>
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)
ListOperations专门操作list列表:

使用:System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
使用:System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().trim("list",1,-1);//裁剪第一个元素
System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
[c++, python, java, c#, c#]
使用:System.out.println(template.opsForList().size("list"));
结果:6
使用:template.opsForList().leftPush("list","java");
      template.opsForList().leftPush("list","python");
      template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
2
使用:String[] stringarrays = new String[]{"1","2","3"};
      template.opsForList().leftPushAll("listarray",stringarrays);
      System.out.println(template.opsForList().range("listarray",0,-1));
结果:[3, 2, 1]
使用:List<Object> strings = new ArrayList<Object>();
      strings.add("1");
      strings.add("2");
      strings.add("3");
      template.opsForList().leftPushAll("listcollection4", strings);
      System.out.println(template.opsForList().range("listcollection4",0,-1));
结果:[3, 2, 1]
使用:System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","aa"));
      System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
==========分割线===========
System.out.println(template.opsForList().leftPush("leftPushIfPresent","aa"));
      System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
结果:
0
==========分割线===========
2
使用:template.opsForList().leftPush("list","java","oc");
      System.out.print(template.opsForList().range("list",0,-1));
结果:[c++, python, oc, java, c#, c#]
使用:template.opsForList().rightPush("listRight","java");
      template.opsForList().rightPush("listRight","python");
      template.opsForList().rightPush("listRight","c++");
结果:
2
使用:String[] stringarrays = new String[]{"1","2","3"};
      template.opsForList().rightPushAll("listarrayright",stringarrays);
      System.out.println(template.opsForList().range("listarrayright",0,-1));
结果:[1, 2, 3]
使用:List<Object> strings = new ArrayList<Object>();
      strings.add("1");
      strings.add("2");
      strings.add("3");
      template.opsForList().rightPushAll("listcollectionright", strings);
      System.out.println(template.opsForList().range("listcollectionright",0,-1));
结果:[1, 2, 3]
使用:System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","aa"));
      System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
      System.out.println("==========分割线===========");
      System.out.println(template.opsForList().rightPush("rightPushIfPresent","aa"));
      System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
结果:0
==========分割线===========
2
使用:System.out.println(template.opsForList().range("listRight",0,-1));
      template.opsForList().rightPush("listRight","python","oc");
      System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, python, c++]
[java, python, oc, c++]
使用:System.out.println(template.opsForList().range("listRight",0,-1));
      template.opsForList().set("listRight",1,"setValue");
      System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, python, oc, c++]
[java, setValue, oc, c++]
使用:System.out.println(template.opsForList().range("listRight",0,-1));
      template.opsForList().remove("listRight",1,"setValue");//将删除列表中存储的列表中第一次次出现的“setValue”。
      System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, setValue, oc, c++]
[java, oc, c++]
使用:System.out.println(template.opsForList().range("list",0,-1));
      System.out.println(template.opsForList().leftPop("list"));
      System.out.println(template.opsForList().range("list",0,-1));
结果:
[c++, python, oc, java, c#, c#]
c++
[python, oc, java, c#, c#]

使用:用法与 leftPop(K key);一样

使用:    System.out.println(template.opsForList().range("list",0,-1));
      System.out.println(template.opsForList().rightPop("list"));
      System.out.println(template.opsForList().range("list",0,-1));
结果:[python, oc, java, c#, c#]
c#
[python, oc, java, c#]

使用:用法与 rightPop(K key);一样

使用:System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().rightPopAndLeftPush("list","rightPopAndLeftPush");
  System.out.println(template.opsForList().range("list",0,-1));
  System.out.println(template.opsForList().range("rightPopAndLeftPush",0,-1));
结果:[oc, java,c#]
[oc, java]
[c#]

使用:用法与rightPopAndLeftPush(K sourceKey, K destinationKey)一样

image

万水千山总是情,点个****"关注"****行不行!!!

上一篇 下一篇

猜你喜欢

热点阅读