前言:redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合),zset(sorted set:有序集合)。
一、redis去官网https://redis.io/download下载后解压
然后点击里面的redis-server.exe(windows平台)即可正常启动
二、在项目中添加redis依赖
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
三、redis的使用
在需要使用redis的地方通过注解注入,比如在controller中添加
@restcontroller
public class rediscontroller {
@autowired
private stringredistemplate stringredistemplate;//只支持redis五大类型中的字符串类型
@autowired
private redistemplate redistemplate;//支持redis五大类型中的所有类型
}
四、字符串类型存取(k,v)
@requestmapping("/setstring")
public object setstring(){
stringredistemplate.boundvalueops("s").set("辣椒");//存入
return stringredistemplate.boundvalueops("s").get();//读取
}
访问接口查看结果:
五、存取对象(k,object)
使用你自己的对象进行存取,我这里使用person对象
package com.star.pojo;
import java.io.serializable;
public class person implements serializable {
private string name;
private int age;
private string add;
public person() {
}
public person(string name, int age, string add) {
this.name = name;
this.age = age;
add = add;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public string getadd() {
return add;
}
public void setadd(string add) {
add = add;
}
@override
public string tostring() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
", add='" + add + '\'' +
'}';
}
}
person.java
@requestmapping("/setperson")
public object setperson(){
person person = new person("鱼er", 151, "北京");
redistemplate.boundvalueops("p").set(person);//存入
return redistemplate.boundvalueops("p").get();//读取
}
六、list类型(k,list)
@requestmapping("/setlist")
public list<string> setlistredis(){
list<string> list=new arraylist<>();
list.add("手头");
list.add("苹果");
list.add("辣椒");
this.redistemplate.boundvalueops("listk").set(list);//存入
return (list<string>) this.redistemplate.boundvalueops("listk").get();//读取
}
七、hash类型(k,k,value)
@requestmapping("/sethash")
public list<string> sethash(){
list<string> list=new arraylist<>();
list.add("大书");
list.add("酸菜");
list.add("鸡蛋");
this.redistemplate.boundhashops("thing").put("h",list);//存入
return (list<string>) this.redistemplate.boundhashops("thing").get("h");//读取
}
八、然后打开redis desktop manager工具可以看到你存储的数据
其安装及连接方式不用多说,基本是个人都会,实在不会可以去这里瞧瞧。
九、如果想要在单元测试中进行存取
添加测试依赖,junit必须4.12以上
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
</dependency>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.12</version>
<scope>test</scope>
</dependency>
在要进行存取的类上添加注解@runwith、@springboottest,意思是启动单元测试时启动当前项目的启动类,因为启动类里面的@springbootapplication里面包含了包扫描@componentscan,不然注入stringredistemplate或redistemplate时注入失败报空指针,当然也可以在启动类里面返回new stringredistemplate或new redistemplate并且加注解@bean的方式处理注入失败问题,这里直接通过加注解的方式处理。
@runwith(value = springjunit4classrunner.class)
//redisapp为启动类名字
@springboottest(classes = {redisapp.class})
public class redisapptest {
@autowired
private stringredistemplate stringredistemplate;
@autowired
private redistemplate redistemplate;
@test
public void setstringredis(){
this.stringredistemplate.boundvalueops("name2").set("熊大");
system.out.println("ok");
}
@test
public void getstringredis(){
string name = this.stringredistemplate.boundvalueops("name2").get();
system.out.println("ok:"+name);
}
}
在redis desktop manager工具中可以看到存储成功了,在控制台也可以读取
九、实际使用思路
@autowired
private redistemplate redistemplate;
/**
* 首次访问时,如果redis没有数据,就访问数据库,然后把访问到的数据存到redis
* 后续访问时,直接查询redis
*/
@override
public list<person> findbypersonid(long id) {// 先查看缓存中有没有
list<person> list = (list<person>) redistemplate.boundvalueops(id).get(); if(list==null){
system.out.println("redis中没有,开始从数据库中获取");
......... //查询数据库得到list<person> list =xxxxxx;
redistemplate.boundvalueops(id).set(list);//将从数据库查到的数据添加到redis中以备下次查找
}else{
system.out.println("redis中存在,list是直接从缓存中获取的,没查数据库");
}
return list;
}
到此这篇关于springboot/springcloud项目中集成redis进行存取的文章就介绍到这了,更多相关springcloud集成redis存取内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!