目录
- springboot集成redis
- 1、概述
- 2、测试redis
- 3、自定义redistemplate
springboot集成redis
1、概述
redis是什么?
redis(remote dictionary server ),即远程字典服务。
是一个开源的使用ansi c语言编写、支持网络、可基于内存亦可持久化的日志型、key-value数据库,并提供多种语言的api。
与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
redis能该干什么?
内存存储、持久化,内存是断电即失的,所以需要持久化(rdb、aof)高效率、用于高速缓冲发布订阅系统地图信息分析计时器、计数器(eg:浏览量)… …
特性
多样的数据类型
持久化
集群
事务
…
2、测试redis
springboot操作数据,spring-data、 jbdc、redis… …
springdata与springboot齐名的项目!
说明:在springboot2.x之后,原来使用的jedis被替换为lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,需使用jedis pool连接池!像bio模式
lettuce:采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据,更像nio模式
新建一个项目
注意:
查看底层
源码分析:
@bean
@conditionalonmissingbean( //如果未注入组件条件,我们自己可以定义一个redistemplate来替换这个默认的
name = {"redistemplate"}
)
public redistemplate<object, object> redistemplate(redisconnectionfactory redisconnectionfactory) throws unknownhostexception {
//默认的 redistemplate 没有过多的设置 redis 都是需要序列化的 !
//两个泛型都是 object object的类型,我们往后使用需要强制转换<string,string>
redistemplate<object, object> template = new redistemplate();
template.setconnectionfactory(redisconnectionfactory);
return template;
}
@bean
@conditionalonmissingbean //由于string 是redis 中最常用的类型 所有说单独提出来一个bean!
public stringredistemplate stringredistemplate(redisconnectionfactory redisconnectionfactory) throws unknownhostexception {
stringredistemplate template = new stringredistemplate();
template.setconnectionfactory(redisconnectionfactory);
return template;
}
1、导入依赖
2、配置连接
# springboot 所有的配置类 都有一个自动配置类 redisautoconfiguration # 自动配置类都会绑定一个 properties 配置文件 redisproperties #配置 redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis
3、测试!
package com.kk;
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.data.redis.connection.redisconnection;
import org.springframework.data.redis.core.redistemplate;
@springboottest
class redis01springbootapplicationtests {
@autowired
private redistemplate redistemplate;
@test
void contextloads() {
/*
redistemplate
opsforvalue 操作字符串的 类似string
opsforlist 操作list 类似list
opsforset
opsforhash
opsforzset
opsforgeo
opsforhyperloglog
除了基本的操作 ,我们常用的方法都可以直接通过redistemplate 比如事务和基本的crud
*/
//获取redis的连接对象
// redisconnection connection = redistemplate.getconnectionfactory().getconnection();
// connection.flushdb();
// connection.flushall();
redistemplate.opsforvalue().set("kk1","kk2");
system.out.println(redistemplate.opsforvalue().get("kk1"));
}
}
3、自定义redistemplate
首先先建一个实体类,测试
user类
package com.kk.pojo;
import lombok.allargsconstructor;
import lombok.data;
import lombok.noargsconstructor;
import org.springframework.stereotype.component;
import java.io.serializable;
@component
@data
@allargsconstructor
@noargsconstructor
//在企业中,我们所有的pojo都会序列化
public class user implements serializable {
private string name;
private int age;
}
测试:
@test
public void test() throws jsonprocessingexception {
//真实的开发一般都使用json来传递对象
user user = new user("kk", 17);
string jsonuser = new objectmapper().writevalueasstring(user);//这样就变成了一个json对象了
redistemplate.opsforvalue().set("user",jsonuser);
system.out.println(redistemplate.opsforvalue().get("user"));
}
r = new objectmapper().writevalueasstring(user);//这样就变成了一个json对象了 redistemplate.opsforvalue().set(“user”,jsonuser); system.out.println(redistemplate.opsforvalue().get(“user”)); }
==注意:如果不在user类中实现序列化,它会报错==
到此这篇关于springboot集成redis的文章就介绍到这了,更多相关springboot集成redis内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!