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中的列表.发布订阅.慢查询.监视器等功能均用到 ...
随机推荐
- java 记录
1.spring ide 的安装图解 https://blog.csdn.net/u012369373/article/details/55097380 2.ssm框架配置内容 http://www. ...
- flask之flask-sqlalchemy(一)
一 安装flask-sqlalchemy pip install flask-sqlalchemy 二 导入相关模块和对象 from flask_sqlalchemy import SQLAlchem ...
- 解决vue移动端适配问题
1,先看看网上关于移动端适配讲解 再聊移动端页面适配,rem和vw适配方案! 基础点:rem相对根节点字体的大小.所以不用px; 根字体:字体的大小px; px:你就当成cm(厘米)这样的东西吧: 基 ...
- avalon2简单数据绑定(自定义属性绑定)
<!DOCTYPE html> <html> <head> <title>项目</title> <meta charset=" ...
- Java中由Calendar类获取的月、天和小时的简单处理
在Java中,Calendar是日期处理的一个重要的类.但是,我们使用Calendar获取的月份,天,小时等可能需要进行简单的处理才能满足我们的需要.比如,月份范围是0-11,而我们可能需要的是1-1 ...
- ToolBar 简单使用
ToolBar 简单使用 ToolBar 是在 android 5.0之后推出的一款用来替代 ActionBar 的 View.ActionBar 是Activity的一部分,不能用在其他视图层次上( ...
- Java基础之Map的遍历
遍历Map集合,有四种方法: public static void main(String[] args) { Map<String, String> map = new HashMa ...
- sqlserver2008 insert语句性能
在sqlserver2008中“新建查询”,执行批量添加语句的执行时间: declare @i int ) begin INSERT INTO [xxx].[dbo].[北京万奇亚讯科技_QueryL ...
- Android分享图文到朋友圈代码。
分享到微信朋友圈代码.不好用,最后选择了shareSdk. private static void shareToTimeLine(File file) { Intent intent = new I ...
- ComboBox赋值ItemsSource数据源的时候会触发SelectionChanged改变事件的解决办法
我用的方法是设置开关 bool flag = false;//默认开关关闭(全局变量) flag = false;在赋值数据源之前设置关闭box.ItemsSource = lstProperty;/ ...