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等理解不到位,后续理解再进行补充
相关文章推荐
- Spring Boot 整合 Redis 实现缓存操作
- SpringBoot整合redis集群并使用StringRedisTemplate和RedisTemplate简单操作Redis集群
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- 封装springboot整合redis的操作工具包
- Spring Boot2.0整合Redis以及常用操作介绍(附封装好的方法)
- 关于springboot整合redis(使用RedisTemplate操作redis)
- Spring Boot整合Redis的完整步骤
- redis集群 与springboot 整合步骤 (linux版本)
- Spring Boot 整合 Redis 实现缓存操作
- springboot整合redis(使用redis templete)详细步骤
- Spring Boot 整合 Redis 实现缓存操作
- springboot整合redis进行数据操作(推荐)
- spring boot进阶(一) springboot整合redis,可操作java对象。最完整、简单易懂、详细的spring boot教程。
- Spring boot整合Mybatis实现级联一对多CRUD操作的完整步骤
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- SpringBoot 中使用Redis整合步骤
- Spring Boot 整合 Redis 实现缓存操作