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列表:

  • List<V> range(K key, long start, long end);
    返回存储在键中的列表的指定元素。偏移开始和停止是基于零的索引,其中0是列表的第一个元素(列表的头部),1是下一个元素
 
使用:System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
  • void trim(K key, long start, long end);
    修剪现有列表,使其只包含指定的指定范围的元素,起始和停止都是基于0的索引
 
使用: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#]
  • Long size(K key);
    返回存储在键中的列表的长度。如果键不存在,则将其解释为空列表,并返回0。当key存储的值不是列表时返回错误。
 
使用:System.out.println(template.opsForList().size("list"));
结果:6
  • Long leftPush(K key, V value);
    将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从左边插入)
 
使用:template.opsForList().leftPush("list","java");
template.opsForList().leftPush("list","python");
template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
1
2
3
  • Long leftPushAll(K key, V... values);
    批量把一个数组插入到列表中
 
使用:String[] stringarrays = new String[]{"1","2","3"};
template.opsForList().leftPushAll("listarray",stringarrays);
System.out.println(template.opsForList().range("listarray",0,-1));
结果:[3, 2, 1]
  • Long leftPushAll(K key, Collection<V> values);
    批量把一个集合插入到列表中
 
使用: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]
  • Long leftPushIfPresent(K key, V value);
    只有存在key对应的列表才能将这个value值插入到key所对应的列表中
 
使用: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
0
==========分割线===========
1
2
  • Long leftPush(K key, V pivot, V value);
    把value值放到key对应列表中pivot值的左面,如果pivot值存在的话
 
使用:template.opsForList().leftPush("list","java","oc");
System.out.print(template.opsForList().range("list",0,-1));
结果:[c++, python, oc, java, c#, c#]
  • Long rightPush(K key, V value);
    将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从右边插入)
 
使用:template.opsForList().rightPush("listRight","java");
template.opsForList().rightPush("listRight","python");
template.opsForList().rightPush("listRight","c++");
结果:
1
2
3
  • Long rightPushAll(K key, V... values);
 
使用:String[] stringarrays = new String[]{"1","2","3"};
template.opsForList().rightPushAll("listarrayright",stringarrays);
System.out.println(template.opsForList().range("listarrayright",0,-1));
结果:[1, 2, 3]
  • Long rightPushAll(K key, Collection<V> values);
 
使用: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]
  • Long rightPushIfPresent(K key, V value);
    只有存在key对应的列表才能将这个value值插入到key所对应的列表中
 
使用: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
0
==========分割线===========
1
2
  • Long rightPush(K key, V pivot, V value);
    把value值放到key对应列表中pivot值的右面,如果pivot值存在的话
 
使用: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++]
  • void set(K key, long index, V value);
    在列表中index的位置设置value值
 
使用: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++]
  • Long remove(K key, long count, Object value);
    从存储在键中的列表中删除等于值的元素的第一个计数事件。
    计数参数以下列方式影响操作:
    count> 0:删除等于从头到尾移动的值的元素。
    count <0:删除等于从尾到头移动的值的元素。
    count = 0:删除等于value的所有元素。
 
使用: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++]
  • V index(K key, long index);
    根据下表获取列表中的值,下标是从0开始的
 
使用:System.out.println(template.opsForList().range("listRight",0,-1));
System.out.println(template.opsForList().index("listRight",2));
结果:[java, oc, c++]
c++
  • V leftPop(K key);
    弹出最左边的元素,弹出之后该值在列表中将不复存在
 
使用: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#]
  • V leftPop(K key, long timeout, TimeUnit unit);
    移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
 
使用:用法与 leftPop(K key);一样
  • V rightPop(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#]
  • V rightPop(K key, long timeout, TimeUnit unit);
    移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
使用:用法与 rightPop(K key);一样
  • V rightPopAndLeftPush(K sourceKey, K destinationKey);
    用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。
 
使用: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#]
  • V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
    用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

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

作者:DreamerRzc
链接:https://www.jianshu.com/p/7bf5dc61ca06
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

如何使用RedisTemplate访问Redis数据结构之list的更多相关文章

  1. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  2. RedisTemplate访问Redis数据结构(介绍和常用命令)

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  3. 如何使用RedisTemplate访问Redis数据结构之字符串操作

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  4. 如何使用RedisTemplate访问Redis数据结构

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  5. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  6. RedisTemplate访问Redis数据结构(一)——String

    当对String数据结构进行操作时,推荐直接使用spring-data-redis提供的StringRedisTemplate,其配置如下 <bean id="stringRedisT ...

  7. RedisTemplate访问Redis数据结构(前言)

    Redis五种基本数据结构 redis提供键值对的形式对数据进行存储.支持五种数据类型:String(字符串),List(链表),Hash(散列),Set(无序集合),ZSet(有序集合).下面是网上 ...

  8. 如何使用RedisTemplate访问Redis数据结构之Zset

    Redis的ZSet数据结构 Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...

  9. RedisTemplate访问Redis数据结构(五)——ZSet

    Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员.不同的是每个元素都会关联一个double类型的分数.有序集合的成员是唯一的,但分数(score)却可以重复.red ...

随机推荐

  1. csp-s模拟测试77+78(lrd day1&2)

    RP-=inf....... 一场考试把rp败光...由于本次考试本人在考试中乱说自己AK导致rp--,本人当选为机房倒数第二没素质 不过AK一次还挺开心的... 达哥出的题还是比较简单的. T1:考 ...

  2. 数据结构实验之二叉树三:统计叶子数 SDUT 3342

    #include <stdio.h> #include <string.h> struct node { char data; struct node *l,*r; }; st ...

  3. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  4. MySQL备忘点(下)

    联结表 创建联结 FROM 表1,表2 与内连接作用相同类似:如果失去WHERE子句,会出现笛卡尔积现象 内联结 INNER JOIN 高级联结 自联结 例子:SELECT 字段b FROM 表 WH ...

  5. redis之基础命令

    一.redis介绍 1.redis特性 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 redis是c语言编写的,支持数据持久化,是key-val ...

  6. fluent中统计颗粒信息【转载】

    转载自:http://mp.weixin.qq.com/s?__biz=MjM5MDkyMjA1Nw==&mid=502657725&idx=1&sn=120703a69e56 ...

  7. 分享一个seata demo,讲两个个问题

    Seata,阿里开源的分布式事务框架,多的我就不介绍了,了解详细介绍,请看官网.seata spring boot入门,可以看我上一篇博客<Spring boot微服务如何集成fescar解决分 ...

  8. kafka学习汇总系列(一)kafka概述

    一.kafka概述 在流式计算中,kafka是用来缓存数据的,storm通过消费kafka的数据进行计算.kafka的初心是,为处理实时数据提供一个统一.高通量.低等待的平台: 1.kafka是一个分 ...

  9. ArcGIS超级工具SPTOOLS-SHP转数据库,批量数据库转数据库,栅格彩色转黑白

    1.1  SHP转数据库 把一个文件夹的数据,转到数据库,或者另一个文件夹,不含字文件夹 1.2  批量数据库转数据库 把一个文件夹下GDB或者MDB,转到另一个文件夹为MDB,GDB,并实现版本的转 ...

  10. 从UDP的”连接性”说起–告知你不为人知的UDP

    原文地址:http://bbs.utest.qq.com/?p=631 很早就计划写篇关于UDP的文章,尽管UDP协议远没TCP协议那么庞大.复杂,但是,要想将UDP描述清楚,用好UDP却要比TCP难 ...