RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html

package com.wbg.springRedis.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.data.redis.core.RedisTemplate; import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; public class TestList {
static RedisTemplate redisTemplate = null; public static void main(String[] args) throws UnsupportedEncodingException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-redis.xml");
redisTemplate = applicationContext.getBean(RedisTemplate.class);
//删除链表
if (redisTemplate.hasKey("list"))
redisTemplate.delete("list");
//那node3插入到list链表
redisTemplate.opsForList().leftPush("list", "node1");
print();
List<String> list = new ArrayList<>();
for (int i = 2; i < 5; i++) {
list.add("node" + i);
}
//相当lpush把多个值从左插入到链表
redisTemplate.opsForList().leftPushAll("list", list);
print();
//右边插入一个节点
redisTemplate.opsForList().rightPushAll("list", "node6");
//获取下标为0的节点
System.out.println(redisTemplate.opsForList().index("list", 0));
//获取链表的长度
System.out.println(redisTemplate.opsForList().size("list"));
//弹出(删除)左边一个节点
System.out.println(redisTemplate.opsForList().leftPop("list"));
//弹出(删除)右边一个节点
System.out.println(redisTemplate.opsForList().rightPop("list"));
print(); //需要使用更为底层的命令才能操作linsert命令
//在node2前插入before_node节点 RedisListCommands.Position.BEFORE,
redisTemplate.getConnectionFactory().getConnection().lInsert(
"list".getBytes("utf-8"),
RedisListCommands.Position.BEFORE,
"node2".getBytes("utf-8"),
"before_node".getBytes("utf-8")
);
print();
//在node2后插入after_node节点 RedisListCommands.Position.AFTER,
redisTemplate.getConnectionFactory().getConnection().lInsert(
"list".getBytes("utf-8"),
RedisListCommands.Position.AFTER,
"node2".getBytes("utf-8"),
"after_node".getBytes("utf-8")
);
print();
//如果list存在 左边插入
redisTemplate.opsForList().leftPushIfPresent("list", "leftEx");
//如果list存在 右边插入
redisTemplate.opsForList().rightPushIfPresent("list", "rightEx");
//左到右 下获取标从0-10节点元素
list = redisTemplate.opsForList().range("list", 0, 5);
System.out.println(list);
//左到右删除多个node节点
redisTemplate.opsForList().remove("list", 3, "node");
//给链表下标设置新值
redisTemplate.opsForList().set("list", 0, "new_value");
print(); /*-------------------Spring对Redis阻塞命令的操作--------*/
//清空数据
redisTemplate.delete("list");
redisTemplate.delete("list1");
List<String> nodeList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodeList.add("node"+i);
} redisTemplate.opsForList().leftPushAll("list1",nodeList);
//相当与blpop命令 可以设置时间参数
redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);
redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);
nodeList.clear();
for (int i = 0; i < 2; i++) {
nodeList.add("data"+i);
}
redisTemplate.opsForList().leftPushAll("list",nodeList);
redisTemplate.opsForList().rightPopAndLeftPush("list","list1");
redisTemplate.opsForList().rightPopAndLeftPush("list","list1",1,TimeUnit.SECONDS); } public static void print() {
System.out.println(redisTemplate.opsForList().range("list", 0, redisTemplate.opsForList().size("list")));
}
}

Redis(RedisTemplate)使用list链表的更多相关文章

  1. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  2. Redis学习之底层链表源码分析

    Redis底层链表的源码分析: 一.链表结点的结构(单个结点): // listNode 双端链表节点 typedef struct listNode { // 前置节点 struct listNod ...

  3. 图解Redis之数据结构篇——链表

    前言     Redis链表为双向无环链表!     图解Redis之数据结构篇--简单动态字符串SDS提到Redis使用了简单动态字符串,链表,字典(散列表),跳跃表,整数集合,压缩列表这些数据结构 ...

  4. spring mvc Spring Data Redis RedisTemplate [转]

    http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...

  5. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

  6. Redis(RedisTemplate)运算、算法(incr、decr、increment)

    RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...

  7. Redis(RedisTemplate)使用hash哈希

    RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...

  8. Redis设计与实现 -- 链表与字典

    1. 链表 1.1 链表的结构 在 Redis 中,链表的实现是双向链表,除此之外与常规的链表不同的是它还有三个函数指针,dup 函数用于复制链表节点所保存的值,free 函数用于释放链表节点保存的值 ...

  9. Redis 底层数据结构之链表

    文章参考:<Redis设计与实现>黄建宏 链表 链表提供了高效的节点重排能力,以及可以顺序访问,也可以通过增删节点灵活调整链表长度,Redis中的列表.发布订阅.慢查询.监视器等功能均用到 ...

随机推荐

  1. django常用封装

    #encoding:utf-8from django.shortcuts import render_to_responseimport hashlibfrom binascii import b2a ...

  2. django通用分页封装

    __author__ = 'Administrator'from django.utils.safestring import mark_safe class Page:    def __init_ ...

  3. HDU 3191 次短路长度和条数

    http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/det ...

  4. 【原创】MapReduce程序如何在集群上执行

    首先了解下资源调度管理框架Yarn. Yarn的结构(如图): Resource Manager (rm)负责调度管理整个集群上的资源,而每一个计算节点上都会有一个Node Manager(nm)来负 ...

  5. NOIP2017:列队

    Sol 考场上: 这不是送\(50\)吗,\(Q^2\)递推就好了 然后,怎么又送\(20\)分??? \(woc\),只有半个小时了,顺利没调出来只有\(50\)分 考后: 神\(TM\)一个大于号 ...

  6. 51nod 1135 原根(原根)

    题意 题目链接 Sol 可以证明素数的原根不会超过他的\(\frac{1}{4}\) 那么预处理出\(P - 1\)的所有的质因数\(p_1, p_2 \dots p_k\),暴力判断一下,如果$\e ...

  7. CentOS 7运维管理笔记(3)----Linux路由器配置

    当正在配置的Linux主机需要作为路由器使用时,通过以下步骤配置后,子网上的计算机就可以访问外网了: 1. 编辑 /etc/sysctl.conf 文件,添加 net.ipv4_ip_forward ...

  8. Android 高速录像(2)

    private void startRecordVideo() { if (index == VIDEO_1080) { if (!supported1080P120Fps) { showToast( ...

  9. Android进入页面开始就自动弹出软键盘

    EditText edittext = (EditText)findViewById(R.id.edittext);   edittext.setFocusable(true);   edittext ...

  10. ES6入门——函数的扩展

    1.函数参数的默认值 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法.现在ES6可以为函数的参数添加默认值,简洁了许多. ES5 function show(a,b){ b = b ...