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. Spring.Net IOC基本应用和在MVC4中的应用

    1.Spring.Net的IOC简单应用 新建一个解决方案添加一个控制台应用程序和一个业务层一个业务层的接口层,通过配置,让控制台应用程序调业务层的方法 1)新建如下图所示,BLL为业务层,通过Spr ...

  2. K2.ActivityInstanceDestination.User is NULL

    his one caught me by surprise! I needed to build up a CSV list of email addresses for all previous t ...

  3. request.getRequestURL()和request.getRequestURI()的区别

    request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...

  4. smarty assign 赋值

    assign赋值 void assign (mixed var) void assign (string varname, mixed var) This is used to assign valu ...

  5. No.2一步步学习vuejs 实例demo篇

    简单应用Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM 的系统: <div id="app"> {{ message }} </d ...

  6. Spring系列之Alias标签的解析与使用

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. flask之flask-sqlalchemy(一)

    一 安装flask-sqlalchemy pip install flask-sqlalchemy 二 导入相关模块和对象 from flask_sqlalchemy import SQLAlchem ...

  8. Live2D 博客页面添加板娘

    偶然看到了live2d,神奇的二次元呀 在页脚Html代码中添加如下代码即可: <link rel="stylesheet" type="text/css" ...

  9. react 使用fortawesome字体图标

    fontawesome 官方使用教程=>点我 npm i --save @fortawesome/fontawesome-svg-core@prerelease \ npm i --save @ ...

  10. Excellent JD

    Job description About the role We are looking for a talented engineer who has excellent cloud skills ...