您的位置:首页 > 数据库 > Redis

Springboot整合redis的操作步骤

2020-07-15 05:55 267 查看

springboot整合redis的过程

一.在Idea项目中加入所需的依赖

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

二.在applications.properties中配置redis

#redis
#redis的ip地址
redis.host=
redis.port=6379
redis.timeout=3
redis.poolMaxTotal=10
redis.poolMaxIdle=8
redis.poolMaxWait=3

三.建立工具类RedisConfig,其目的是为了获取到applications.propertied中redis的配置文件

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/*
Redis配置类

*/
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
private String host;
private int port;
private int timeout;   //这里定义的单位为秒
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;  //这里定义的单位为秒

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public int getTimeout() {
return timeout;
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getPoolMaxTotal() {
return poolMaxTotal;
}

public void setPoolMaxTotal(int poolMaxTotal) {
this.poolMaxTotal = poolMaxTotal;
}

public int getPoolMaxIdle() {
return poolMaxIdle;
}

public void setPoolMaxIdle(int poolMaxIdle) {
this.poolMaxIdle = poolMaxIdle;
}

public int getPoolMaxWait() {
return poolMaxWait;
}

public void setPoolMaxWait(int poolMaxWait) {
this.poolMaxWait = poolMaxWait;
}
}

注:@ConfigurationProperties(prefix = “redis”)的作用是获取配置文件中以redis开头的配置信息

四.编写RedisServer类,这里是实现对缓冲文件的增删改查

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {

@Autowired
JedisPool jedisPool;

/*
*根据键获取值
*Keyprefix的作用是给key加一个前缀,防止被后面存储的覆盖掉
*/
public <T> T get(KeyPrefix prefix, String key,Class<T>clazz){

Jedis jedis=null;
try {
jedis= jedisPool.getResource();
String realKey = prefix.getPrefix() + key;
String str = jedis.get(realKey);
T t=stringToBean(str,clazz);
return t;
}finally {
returnToPool(jedis);
}
}

private <T> T stringToBean(String str,Class<T>clazz) {
if (str ==null||str.length()<=0||clazz==null){
return null;
}
if (clazz ==int.class||clazz == Integer.class){
return (T)Integer.valueOf(str);
}else if (clazz ==String.class){
return (T)str;
}else if (clazz ==long.class||clazz== Long.class){
return (T)Long.valueOf(str);
}else{
return JSON.toJavaObject(JSON.parseObject(str),clazz);
}
}
/*
将键值设置在redis中
*/

public <T> boolean set(KeyPrefix prefix,String key,T value){

Jedis jedis=null;
try {
jedis= jedisPool.getResource();
String str =beanToString(value);
if (str == null|| str.length() <=0){
return false;
}
String realKey = prefix.getPrefix() + key;
int seconds = prefix.expireSeconds();//expireSeconds为过期时间
if (seconds <= 0) {   //seconds <= 0 也就是没有过期
jedis.set(realKey, str);
}else  {
jedis.setex(realKey, seconds, str);
}
return true;
}finally {
returnToPool(jedis);
}
}

private <T> String beanToString(T value) {
if (value == null){
return null;
}
Class<?>clazz= value.getClass();
if (clazz ==int.class||clazz== Integer.class){
return ""+value;
}else if (clazz ==String.class){
return (String)value;
}else if (clazz ==long.class||clazz== Long.class){
return ""+value;
}else{
return JSON.toJSONString(value);
}

}
/*
判断键是否存在
*/
public Boolean exists(KeyPrefix prefix, String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realPrefix = prefix.getPrefix() + key;
return jedis.exists(realPrefix);
}finally {
returnToPool(jedis);
}
}
/*
对键的值加一
*/
public Long incr(KeyPrefix prefix, String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realPrefix = prefix.getPrefix() + key;
return jedis.incr(realPrefix);
}finally {
returnToPool(jedis);
}
}
/*
对键的值减一
*/
public Long decr(KeyPrefix prefix, String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realPrefix = prefix.getPrefix() + key;
return jedis.decr(realPrefix);
}finally {
returnToPool(jedis);
}
}

private void returnToPool(Jedis jedis){
if (jedis !=null){
jedis.close();
}
}
}

五.编写RedisPoolFactory类,主要是将获取的配置信息加入到redis连接池

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Service
public class RedisPoolFactory {
@Autowired
RedisConfig redisConfig;

@Bean
public JedisPool jedisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);    //自定义的单位为秒,而源码需要的是毫秒
JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout() * 1000);  //自定义的单位为秒,而源码需要的是毫秒
return jedisPool;
}
}

目前理解的只有这些,对Baseprefix等理解不到位,后续理解再进行补充

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: