Redis学习笔记——SpringDataRedis的使用

与Spring集成

我需要哪些jar包?

<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

如何配置spring配置文件?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置IP地址与端口号,连接redis服务器--><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="127.0.0.1" p:port="6379" p:usePool="true"/><!--配置redisTemplate--><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="jedisConnectionFactory"/><!--配置stringRedisTemplate--><bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connectionFactory-ref="jedisConnectionFactory"/>
</beans>

一般情况,只需要配置RedisTemplate或StringRedisTemplate其中一个就行,常用的为StringRedisTemplate。

RedisTemplate与StringRedisTemplate的区别?

StringRedisTemplate:

public class StringRedisTemplate extends RedisTemplate<String, String> {public StringRedisTemplate() {StringRedisSerializer stringSerializer = new StringRedisSerializer();this.setKeySerializer(stringSerializer);this.setValueSerializer(stringSerializer);this.setHashKeySerializer(stringSerializer);this.setHashValueSerializer(stringSerializer);}public StringRedisTemplate(RedisConnectionFactory connectionFactory) {this();this.setConnectionFactory(connectionFactory);this.afterPropertiesSet();}protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {return new DefaultStringRedisConnection(connection);}
}

从上面的代码可以看出,StringRedisTemplate继承了键值类型都为String的RedisTemplate,且使用StringRedisSerializer作为序列化工具。所以StringRedisTemplate能使用的方法,RedisTemplate都能使用,下面的例子只会展示StringRedisTemplate的方法。

StringRedisSerializer:

public class StringRedisSerializer implements RedisSerializer<String> {private final Charset charset;public StringRedisSerializer() {this(Charset.forName("UTF8"));}public StringRedisSerializer(Charset charset) {Assert.notNull(charset);this.charset = charset;}public String deserialize(byte[] bytes) {return bytes == null?null:new String(bytes, this.charset);}public byte[] serialize(String string) {return string == null?null:string.getBytes(this.charset);}
}

可以看出,StringRedisSerializer使用UTF8字符集处理字符串。

使用API操作基本redis基本数据类型


spring提供哪些接口操作redis基本数据?

第一组

ValueOperations:字符串类型操作
ListOperations:列表类型操作
SetOperations:集合类型操作
ZSetOperations:有序集合类型操作
HashOperations:散列操作

第二组

BoundValueOperations:字符串类型操作
BoundListOperations:列表类型操作
BoundSetOperations:集合类型操作
BoundZSetOperations:有序集合类型操作
BoundHashOperations:散列操作

如何获得接口的实现?

第一组

ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();
HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();

第二组

BoundValueOperations<String, String> valueOperations = stringRedisTemplate.boundValueOps("key");
BoundListOperations<String, String> listOperations = stringRedisTemplate.boundListOps("key");
BoundSetOperations<String, String> setOperations = stringRedisTemplate.boundSetOps("key");
BoundZSetOperations<String, String> zSetOperations = stringRedisTemplate.boundZSetOps("key");
BoundHashOperations<String, Object, Object> hashOperations = stringRedisTemplate.boundHashOps("key");

从上面两组实现可以发现,第二组API只是在第一组API的上面将key值的绑定放在获得接口时了,此举方便了每次操作基本数据类型的时候不用反复的去填写key值,只需要操作具体的value就行了。

具体有哪些数据操作方式,如ValueOperations的get与set,ListOperations的push与pop等可以参照:Redis学习笔记(2)-Redis数据类型。他们的方法签名与客户端redis-cli操作redis时的签名是一样的。

使用API操作消息队列


使用API前需要了解

Redis学习笔记(3)-Redis事务,过期时间,队列

如何发送消息队列?

RedisTemplate template = template.convertAndSend("channel", "message");

第一个参数为发送的消息的频道,第二个参数为消息本身。

如何接受队列中的消息?

public interface MessageDelegate {void handleMessage(String message);void handleMessage(Map message); void handleMessage(byte[] message);void handleMessage(Serializable message);void handleMessage(Serializable message, String channel);
}

首先,需要一个符合MessageDelegate 接口方法签名的类,这个类是自定义的,可以使用MessageDelegate 中的一个或多个签名方式,如:

public class UserMessageDelegate {public void handleMessage(String message) {System.out.println(message);}
}

得到此类后,将此类注册到消息监听容器中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:redis="http://www.springframework.org/schema/redis"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/redishttp://www.springframework.org/schema/redis/spring-redis.xsd"><redis:listener-container><redis:listener ref="listener" method="handleMessage" topic="chatroom"/></redis:listener-container><bean id="listener" class="com.hzw.redis.listener.UserMessageDelegate"/></beans>

其中,topic就是你要监听的频道。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/556647.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中海达数据怎么转rinex_cors账号网最新实战教程,中海达 F61 Plus RTK连接千寻cors账号的方法...

RTK在工程测量工作中越来越流行&#xff0c;在这股潮流中&#xff0c;中海达品牌的F61 Plus因为融合了中海达多年的成熟建站技术HD-CORS&#xff0c;在工程测量工作中也受到了很大欢迎。它可以配套各种cors账号使用&#xff0c;使用过程中只需一台RTK移动站即可进行数据采集、施…

Shiro之UsernamePasswordTokenRememberMeAuthenticationTokenAuthenticationToken

继承关系 先看一下三者的继承关系&#xff0c;会有一个比较清楚的认识 AuthenticationToken AuthenticationToken 用于收集用户提交的身份&#xff08;如用户名&#xff09;及凭据&#xff08;如密码&#xff09;。Shiro会调用CredentialsMatcher对象的doCredentialsMatch方法对…

华为手机滑动速度设置_华为手机打字速度慢?开启这个设置,一分钟就能打200字...

很多朋友都说自己的打字速度非常慢&#xff01;每次使用手机聊天、记录工作事项大半天还没打一段字来。其实使用华为手机就能非常快速的码字&#xff0c;不管聊天还是工作&#xff0c;使用这个方法&#xff0c;一分钟就能打200字。1.聊天快速打字手机键盘因为体积的原因&#x…

SpringBoot 使用AOP功能

RPC&#xff0c;AOP都会用到代理&#xff0c;代理的技术有jdk的Proxy代理(必须实现接口)&#xff0c;cglib(可以不实现接口&#xff0c;直接实现类)&#xff0c;Javassist(jboss )而Spring boot本身也在方方面面使用了代理技术,在Spring中有两种动态代理方式&#xff0c;分别为…

cognos报表导出excel_有了这个报表工具,一键生成自定义的各种报表,还可以导出Excel...

EasyReport是一个简单易用的Web报表工具,它的主要功能是把SQL语句查询出的数据转换成报表页面&#xff0c; 同时支持表格的跨行(RowSpan)与跨列(ColSpan)配置。 同时它还支持报表Excel导出、图表显示及固定表头与左边列的功能。功能介绍本工具从数据库(MySQL,Oracle,SQLServer,…

java自定义注解实现日志功能

一、spring aop的通知类型 1、前置通知&#xff08;Before&#xff09;&#xff1a;在连接点前执行&#xff0c;不会影响连接点的执行&#xff0c;除非抛异常&#xff1b; 2、后置通知&#xff08;AfterReturning&#xff09;&#xff1a;在连接点正常执行完成后执行&#xff0…

java自定义注解annotation记录操作日志

说到注解我们平常用的可以说非常多啦&#xff0c;说几个常用的的注解 RestController Service Autowired 这些都是我们平常使用spring框架最常见的注解了&#xff0c;我们只知道它们非常好用&#xff0c;使用RestController 就能构建一个restful的控制器,Service 这个是我们常用…

数组concat_js 标准二维数组变一维数组的方法

问题&#xff1a;[[1, 1], [2, 3], [4, 5]] -> [1, 1, 2, 3, 4, 5]&#xff1f;方法一利用es5的arr.reduce(callback[, initialValue])实现var arr1 [[0, 1], [2, 3], [4, 5]]; var arr2 arr1.reduce(function (a, b) { return a.concat(b)} ); // arr2 [0, 1, 2, 3, 4, 5…

安卓手机小说阅读器_乐小说阅读器下载手机版-乐小说阅读器app下载安装

乐小说阅读器app&#xff0c;一款非常不错的小说阅读器&#xff0c;这里的小说资源不仅非常的丰富&#xff0c;而且用户们可以免费任意阅读。小说内容非常的优质&#xff0c;阅读界面非常的清爽简洁&#xff0c;无任何广告弹出。喜欢看小说的就下载试试吧&#xff01;关于乐小说…

java 自定义注解+AOP实现日志记录

ssm版本&#xff1a; 1、首先自定义一个注解&#xff0c;该注解有两个属性&#xff0c;一个是模块名&#xff0c;一个是操作的内容。该注解是用来修饰Service层中的方法的。 2、创建一个切面类&#xff0c;该切面使用Aspect和Component注解修饰&#xff0c;该页面需要注入一个…

是人是谁_谁是白鹤滩最可爱的人

白鹤滩水电站是全球在建第一大水电站&#xff0c;主要特性指标均位居世界水电工程前列&#xff0c;2021年7月&#xff0c;首批机组投产发电将是白鹤滩工程为建党一百周年献礼的重大壮举。建设中的白鹤滩水电站工程建设不停步&#xff0c;白鹤滩水电站未来的运行管理者——白鹤滩…

使用@Order注解调整配置类加载顺序

Order 1、Spring 4.2 利用Order控制配置类的加载顺序&#xff0c; 2、Spring在加载Bean的时候&#xff0c;有用到order注解。 3、通过Order指定执行顺序&#xff0c;值越小&#xff0c;越先执行 4、Order注解常用于定义的AOP先于事物执行 1.Order的注解源码解读 注解类&am…

potplayer 多个进程_进程组、会话、控制终端概念,如何创建守护进程?

守护进程概念&#xff1a;守护进程&#xff0c;也就是通常所说的Daemon进程&#xff0c;是Linux中的后台服务进程。周期性的执行某种任务或等待处理某些发生的事件。Linux系统有很多守护进程&#xff0c;大多数服务都是用守护进程实现的。比如&#xff1a;像我们的tftp&#xf…

hadoop ubantu环境搭建_ubuntu hadoop学习 环境搭建

单机模式下载hadoop-2.7.3.tar.gz 并解压缩安装java环境sudo apt install openjdk-8-jdk设置环境变量vim /etc/profileexport JAVA_HOME/usr/lib/jvm/java-8-openjdk-amd64export CLASSPATH.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATHexport HADOOP_HOME/usr/local/hadoop…

详细介绍mysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUE

文章目录Normal 普通索引Unique 唯一索引Full Text 全文索引SPATIAL 空间索引btree索引和hash索引的区别在实际操作过程中&#xff0c;应该选取表中哪些字段作为索引&#xff1f;Normal 普通索引 表示普通索引&#xff0c;大多数情况下都可以使用 Unique 唯一索引 表示唯一的…

acs880 用户手册_华中数控、广州数控系统用户手册

数控加工仿真系统 广州数控系统用户手册上海宇龙软件工程有限公司2004 年 5 月华中数控、广州数控系统用户手册 目录I目录第一章 基本操作 ............................................................................................ 1 1.1 项目文件 ...................…

自学java去哪找工作比较好_如何自学java?什么程度可以找工作?

我个人学习Java就是自学的&#xff0c;所以还是有一些发言权&#xff0c;我是非计算机专业&#xff0c;上大三的时候想做程序员&#xff0c;因为感觉本专业不挣钱&#xff0c;用了一年的时间在学校自学了Java&#xff0c;学习的过程中无非就是两个字最重要&#xff1a;坚持。因…

mysql索引类型 normal, unique, full text

问题1&#xff1a;mysql索引类型normal&#xff0c;unique&#xff0c;full text的区别是什么&#xff1f; normal&#xff1a;表示普通索引 unique&#xff1a;表示唯一的&#xff0c;不允许重复的索引&#xff0c;如果该字段信息保证不会重复例如身份证号用作索引时&#x…

亚马逊出的平板电脑_美国最畅销的安卓平板电脑,还只有2GB内存

在美国除了iPad&#xff0c;谁家的平板电脑卖得最好&#xff1f;不是华为小米&#xff0c;也不是微软或谷歌&#xff0c;而是Amazon亚马逊。主打入门级定位的亚马逊Fire系列平板电脑&#xff0c;在北美的平板市场上&#xff0c;有着举足轻重的地位。今天&#xff0c;亚马逊正式…

MySQL普通索引与唯一索引__mysql中唯一索引和普通索引的用途及区别

MySQL普通索引与唯一索引 索引作用&#xff1a; 提高查询效率&#xff0c;一般加在经常查询或者排序的字段上。 普通索引&#xff1a; 允许字段值重复 唯一索引&#xff1a; 保证数据记录唯一性 如何选择&#xff1a; 查询过程&#xff1a; 对普通索引来说&#xff0c…