springboot中redis相关配置
1、pom.xml中引入依赖
<dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>2.9.0</version> </dependency>
2、springboot的习惯优于配置。也在项目中使用了application.yml文件配置mysql的基本配置项。这里也在application.yml里面配置redis的配置项。
spring:
datasource:
# 驱动配置信息
url: jdbc:mysql://localhost:3306/spring_boot?useunicode=true&characterencoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.druiddatasource
driver-class-name: com.mysql.jdbc.driver
# 连接池的配置信息
filters: stat
maxactive: 20
initialsize: 1
maxwait: 60000
minidle: 1
timebetweenevictionrunsmillis: 60000
minevictableidletimemillis: 300000
validationquery: select 'x'
testwhileidle: true
testonborrow: false
testonreturn: false
poolpreparedstatements: true
maxopenpreparedstatements: 20
redis:
host: 127.0.0.1
port: 6379
password: pass1234
pool:
max-active: 100
max-idle: 10
max-wait: 100000
timeout: 0
springboot中redis相关类
- 项目操作redis是使用的redistemplate方式,另外还可以完全使用jedispool和jedis来操作redis。整合的内容也是从网上收集整合而来,网上整合的方式和方法非常的多,有使用注解形式的,有使用jackson2jsonredisserializer来序列化和反序列化key value的值等等,很多很多。这里使用的是我认为比较容易理解和掌握的,基于jedispool配置,使用redistemplate来操作redis的方式。
redis单独放在一个包redis里,在包里先创建redisconfig.java文件。
redisconfig.java
@configuration
@enableautoconfiguration
public class redisconfig {
@bean
@configurationproperties(prefix = "spring.redis.pool")
public jedispoolconfig getredisconfig(){
jedispoolconfig config = new jedispoolconfig();
return config;
}
@bean
@configurationproperties(prefix = "spring.redis")
public jedisconnectionfactory getconnectionfactory() {
jedisconnectionfactory factory = new jedisconnectionfactory();
factory.setusepool(true);
jedispoolconfig config = getredisconfig();
factory.setpoolconfig(config);
return factory;
}
@bean
public redistemplate<?, ?> getredistemplate() {
jedisconnectionfactory factory = getconnectionfactory();
redistemplate<?, ?> template = new stringredistemplate(factory);
return template;
}
}
- 在包里创建redisservice接口的实现类redisserviceimpl,这个类实现了接口的所有方法。
redisserviceimpl.java
@service("redisservice")
public class redisserviceimpl implements redisservice {
@resource
private redistemplate<string, ?> redistemplate;
@override
public boolean set(final string key, final string value) {
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
redisserializer<string> serializer = redistemplate.getstringserializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
@override
public string get(final string key) {
string result = redistemplate.execute(new rediscallback<string>() {
@override
public string doinredis(redisconnection connection) throws dataaccessexception {
redisserializer<string> serializer = redistemplate.getstringserializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
@override
public boolean expire(final string key, long expire) {
return redistemplate.expire(key, expire, timeunit.seconds);
}
@override
public boolean remove(final string key) {
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
redisserializer<string> serializer = redistemplate.getstringserializer();
connection.del(key.getbytes());
return true;
}
});
return result;
}
}
在这里execute()方法具体的底层没有去研究,只知道这样能实现对于redis数据的操作。
redis保存的数据会在内存和硬盘上存储,所以需要做序列化;这个里面使用的stringredisserializer来做序列化,不过这个方式的泛型指定的是string,只能传string进来。所以项目中采用json字符串做redis的交互。
到此,redis在springboot中的整合已经完毕,下面就来测试使用一下。
5. springboot项目中使用redis
在这里就直接使用springboot项目中自带的单元测试类springbootapplicationtests进行测试。
@runwith(springrunner.class)
@springboottest
public class springbootapplicationtests {
private jsonobject json = new jsonobject();
@autowired
private redisservice redisservice;
@test
public void contextloads() throws exception {
}
/**
* 插入字符串
*/
@test
public void setstring() {
redisservice.set("redis_string_test", "springboot redis test");
}
/**
* 获取字符串
*/
@test
public void getstring() {
string result = redisservice.get("redis_string_test");
system.out.println(result);
}
/**
* 插入对象
*/
@test
public void setobject() {
person person = new person("person", "male");
redisservice.set("redis_obj_test", json.tojsonstring(person));
}
/**
* 获取对象
*/
@test
public void getobject() {
string result = redisservice.get("redis_obj_test");
person person = json.parseobject(result, person.class);
system.out.println(json.tojsonstring(person));
}
/**
* 插入对象list
*/
@test
public void setlist() {
person person1 = new person("person1", "male");
person person2 = new person("person2", "female");
person person3 = new person("person3", "male");
list<person> list = new arraylist<>();
list.add(person1);
list.add(person2);
list.add(person3);
redisservice.set("redis_list_test", json.tojsonstring(list));
}
/**
* 获取list
*/
@test
public void getlist() {
string result = redisservice.get("redis_list_test");
list<string> list = json.parsearray(result, string.class);
system.out.println(list);
}
@test
public void remove() {
redisservice.remove("redis_test");
}
}
class person {
private string name;
private string sex;
public person() {
}
public person(string name, string sex) {
this.name = name;
this.sex = sex;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getsex() {
return sex;
}
public void setsex(string sex) {
this.sex = sex;
}
}
在这里先是用@autowired注解把redisservice注入进来,然后由于是使用json字符串进行交互,所以引入fastjson的jsonobject类。然后为了方便,直接在这个测试类里面加了一个person的内部类。
一共测试了:对于string类型的存取,对于object类型的存取,对于list类型的存取,其实本质都是转成了json字符串。还有就是根据key来执行remove操作。
获取字符串:
获取对象:
获取list:
redis管理客户端数据:
到此,测试完成,对于常用的一些数据类型的转换存取操作也基本调试通过。所以本文对于springboot整合redis到此结束。
到此这篇关于redis在项目中的使用(jedispool方式)的文章就介绍到这了,更多相关redis项目中使用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!