Redis(RedisTemplate)使用list链表
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链表的更多相关文章
- spring data redis RedisTemplate操作redis相关用法
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...
- Redis学习之底层链表源码分析
Redis底层链表的源码分析: 一.链表结点的结构(单个结点): // listNode 双端链表节点 typedef struct listNode { // 前置节点 struct listNod ...
- 图解Redis之数据结构篇——链表
前言 Redis链表为双向无环链表! 图解Redis之数据结构篇--简单动态字符串SDS提到Redis使用了简单动态字符串,链表,字典(散列表),跳跃表,整数集合,压缩列表这些数据结构 ...
- spring mvc Spring Data Redis RedisTemplate [转]
http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
- Redis(RedisTemplate)运算、算法(incr、decr、increment)
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...
- Redis(RedisTemplate)使用hash哈希
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...
- Redis设计与实现 -- 链表与字典
1. 链表 1.1 链表的结构 在 Redis 中,链表的实现是双向链表,除此之外与常规的链表不同的是它还有三个函数指针,dup 函数用于复制链表节点所保存的值,free 函数用于释放链表节点保存的值 ...
- Redis 底层数据结构之链表
文章参考:<Redis设计与实现>黄建宏 链表 链表提供了高效的节点重排能力,以及可以顺序访问,也可以通过增删节点灵活调整链表长度,Redis中的列表.发布订阅.慢查询.监视器等功能均用到 ...
随机推荐
- No mapping found for HTTP request with URI异常的原因,<mvc:default-servlet-handler/>的作用
一.最近做的一个项目有很多静态资源文件,按照平时的配置springmvc进行配置发现访问不到静态文件,并且在我配置好controller去访问结果还是404 No mapping found for ...
- tomcat开启远程调试和热部署(jrebel)启动tomcat
@echo off set REBEL_HOME=D:\jrebel\jrebel--nosetup set JAVA_OPTS=-agentpath:%REBEL_HOME%\lib\jrebel6 ...
- 反汇编调试Android
https://code.google.com/p/android/issues/detail?id=73076 http://my.unix-center.net/~Simon_fu/?p=527 ...
- flask 继承模版的基本使用
- JS读取粘贴板内容
1.1 监听onpaste事件 1.1.1 定义和用法 npaste 事件在用户向元素中粘贴文本时触发. 注意: 虽然使用的 HTML 元素都支持 onpaste 事件,但实际上并非支持所有元 ...
- 基础架构之GitLab
Git几乎是软件开发人员的必备工具了,关于代码管理,公司都一般都会搭建自己的仓库,关于GitLab的详细介绍参见官方网站详见 https://about.gitlab.com,这篇文章主要介绍安装及使 ...
- 聊天室或文字直播间的效果(AS开发实战第二章学习笔记)
聊天室或文字直播间的效果即是新的文字消息总是加入窗口末尾,同时窗口内部的文本整体向上滚动,窗口的大小.位置保持不变聊天室用到的属性与方法说明gravity 指定文本的对齐方式,取值left|botto ...
- Ubuntu14.04下如何安装Python爬虫框架Scrapy
按照官方文档的说明,安装scrapy 需要以下程序或者库: (1).Python 2.7 (2).lxml. Most linux distributions ships PRepackaged ve ...
- SQL Server ->> 自动创建表并从文件加载数据
这个存储过程自动创建表并从文件加载数据. 有一点需要说明的是Excel 12.0驱动是兼容了Excel 97-2003和Excel 2007两者格式的Excel文件. CREATE PROCEDURE ...
- MVC中重定向几种方法
//1.Response.Redirect using System; using System.Collections.Generic; using System.Linq; using Syste ...