社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
官网:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
集群文档:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
在SpringBoot2.0中集成redis
springboot整合redis相关依赖引入
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
相关配置文件的引入(yml配置方式),如果密码没有,默认就是空,我这里有密码就加上了密码,连接最大时间最好不要设置成-1永不超时,因为有时候某一个命令数据量特别大,那么就会卡在那了
redis:
# redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突
database: 0
# redis服务器地址(默认为localhost)
host: 192.168.17.128
# redis端口(默认为6379)
port: 6379
# redis访问密码(默认为空)
password: 123456
# redis连接超时时间(单位为毫秒)
timeout: 3000
#=========redis线程池设置=========
pool:
# 连接池中的最大空闲连接,默认值也是8。
max-active: 8
# 连接池中的最小空闲连接,默认值也是0。
max-idle: 8
#如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
min-idle: 2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
max-wait: 1000
写controller进行测试,这里我就举了最简单的set,get设置,存取字符串例子:
package net.xdclass.base_project.controller;
import net.xdclass.base_project.domain.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/test/redis")
public class RdisTestController {
@Autowired
private StringRedisTemplate redisTpl; //jdbcTemplate
@GetMapping(value="add")
public Object add(){
//opsForValue : Returns the operations performed on simple values (or Strings in Redis terminology).
redisTpl.opsForValue().set("name", "xdclass2018");
return JsonData.buildSuccess();
}
@GetMapping(value="get")
public Object get(){
String value = redisTpl.opsForValue().get("name");
return JsonData.buildSuccess(value);
}
}
这里主要注入了StringRedisTemplate这个模板类进行redis的操作,类似于jdbcTemplate,redisTpl能点出许多方法,对不同的redis数据类型进行操作,如下图所示:
下面启动项目进行测试:
测试成功!
上面代码中直接操作redis,许多代码是重复的,比较麻烦,下面我们就封装一个redis工具类,以简化开发,这里只是简单封装了几个方法,其他的方法有需要可以再加,可在开发中使用,注意:在工具类上要加上@Component注解,纳入容器管理
package net.xdclass.base_project.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* redis工具类
*/
@Component
public class RedisClient {
@Autowired
private StringRedisTemplate redisTpl; //jdbcTemplate
/**
* 功能描述:设置key-value到redis中
* @param key
* @param value
* @return
*/
public boolean set(String key ,String value){
try{
redisTpl.opsForValue().set(key, value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* 功能描述:通过key获取缓存里面的值
* @param key
* @return
*/
public String get(String key){
return redisTpl.opsForValue().get(key);
}
/**
* 功能描述:设置某个key过期时间
* @param key
* @param time
* @return
*/
public boolean expire(String key,long time){
try {
if(time>0){
redisTpl.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 功能描述:根据key 获取过期时间
* @param key
* @return
*/
public long getExpire(String key){
return redisTpl.getExpire(key,TimeUnit.SECONDS);
}
/**
* 递增
* @param key 键
* @return
*/
public long incr(String key, long delta){
return redisTpl.opsForValue().increment(key, delta);
}
/**
* 递减
* @param key 键
* @param delta 要减少几
* @return
*/
public long decr(String key, long delta){
return redisTpl.opsForValue().increment(key, -delta);
}
//==============Map结构=====================
//==============List结构=====================
}
这时候我们操作redis只要使用工具类就行了,我们在刚刚的controller中写两个接口测试一下工具类:
@GetMapping(value="save_user")
public Object saveUser(){
User user = new User(5,"zhangsan","13122223333",11,new Date());
String userStr = JsonUtils.obj2String(user);
boolean flag = redis.set("base:user:11", userStr);
return JsonData.buildSuccess(flag);
}
@GetMapping(value="find_user")
public Object findUser(){
String userStr = redis.get("base:user:11");
User user = JsonUtils.string2Obj(userStr, User.class);
return JsonData.buildSuccess(user);
}
注意:使用工具类先要将工具类注入进来:
进行测试,成功!
源码地址:https://gitee.com/xuxinsunqizheng/SpringBoot2.0.git
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!