您好,欢迎访问一九零五行业门户网

SpringBoot集成如何使用Redis

springboot集成使用redisjedis 是 redis 官方推出的一款面向 java 的客户端,提供了很多接口供 java 语言调用。可以在 redis 官网下载. spring-data-redis 是 spring 大家族的一部分,提供了在 srping 应用中通 过简单的配置访问 redis 服务,对 reids 底层开发包(jedis, jredis, and rjc)进 行了高度封装,redistemplate 提供了 redis 各种操作
spring-data-redis 针对 jedis 提供了如下功能:
连接池自动管理,提供了一个高度封装的“redistemplate”类.
针对 jedis 客户端中大量 api 进行了归类封装,将同一类型操作封装为 operation 接口.
valueoperations:简单 k-v 操作
setoperations:set 类型数据操作
zsetoperations:zset 类型数据操作
hashoperations:针对 map 类型的数据操作
listoperations:针对 list 类型的数据操作
3.将事务操作封装,有容器控制。
4.针对数据的“序列化/反序列化”,提供了多种可选择策略(redisserializer)
jdkserializationredisserializer:pojo 对象的存取场景,使用 jdk 本身 序列化机制.
stringredisserializer:key 或者 value 为字符串的场景,根据指定的charset 对数据的字节序列编码成 string,是“new string(bytes, charset)”和 “string.getbytes(charset)”的直接封装。是最轻量级和高效的策略。
jacksonjsonredisserializer:jackson-json 工具提供了 javabean 与 json 之 间的转换能力,可以将 pojo 实例序列化成 json 格式存储在 redis 中,也可以将 json 格式的数据转换成 pojo 实例。
搭建1.导入jar包
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
2.配置连接redis
spring: redis: host: 192.168.31.100 port: 6379 password: 111 database: 0 pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 8 # 连接池中的最大空闲连接 min-idle: 0 # 连接池中的最小空闲连接 timeout: 5000ms # 连接超时时间(毫秒)
在application.yml文件中spring下添加如上配置
3.添加配置类redisconfig
package com.ffyc.back.demo.config;import com.fasterxml.jackson.annotation.jsonautodetect;import com.fasterxml.jackson.annotation.jsontypeinfo;import com.fasterxml.jackson.annotation.propertyaccessor;import com.fasterxml.jackson.databind.deserializationfeature;import com.fasterxml.jackson.databind.objectmapper;import com.fasterxml.jackson.databind.jsontype.impl.laissezfairesubtypevalidator;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.data.redis.connection.redisconnectionfactory;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.serializer.jackson2jsonredisserializer;import org.springframework.data.redis.serializer.stringredisserializer;@configurationpublic class redisconfig { /** * 序列化键,值 * @param connectionfactory * @return */ @bean public redistemplate<string, object> redistemplate(redisconnectionfactory connectionfactory) { redistemplate<string, object> redistemplate = new redistemplate<>(); redistemplate.setconnectionfactory(connectionfactory); jackson2jsonredisserializer<object> jackson2jsonredisserializer = new jackson2jsonredisserializer<object>(object.class); stringredisserializer redisserializer = new stringredisserializer(); redistemplate.setkeyserializer(redisserializer); redistemplate.sethashkeyserializer(redisserializer); redistemplate.setvalueserializer(jackson2jsonredisserializer); redistemplate.sethashvalueserializer(jackson2jsonredisserializer); return redistemplate; }}
在配置包中添加此配置
此配置类的作用是将后端将要传过去的数据json序列化,如果没有此配置的话后端传过去的格式和redis端的不符合会出现乱码情况
4.注入redistemplate
在需要使用的地方注入 注入后就可以使用了
5.测试和使用
使用实例:(1)
(2)
以上就是springboot集成如何使用redis的详细内容。
其它类似信息

推荐信息