spring boot和redis的整合
2020-07-14 06:03
106 查看
Jedis方法
首先了解一下Jedis和redis的一些知识
Jedis是Redis官方对java操作redis-api的一个封装框架。redistemplate是spring框架对jedis和lettuce的封装。让spring框架体系能够更加方便的接入redis的功能。
Redis是一种非关系型数据库。在这里Redis中主要是存储用户反复查询的一些信息。
进行spring boot和redis的整合
- 添加相关的依赖
<dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-redis</artifactid> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>2.8.1</version> </dependency>
- 修改配置文件application.properties
#访问地址 redis.host=127.0.0.1 #访问端口 redis.port=6379 #注意,如果没有password,此处不设置值,但这一项要保留 redis.password= #最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。 redis.maxIdle=300 #连接池的最大数据库连接数。设为0表示无限制 redis.maxActive=600 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 redis.maxWait=1000
- 建立工具类RedisConfig类,这个类主要是获取application.properties配置文件的信息。
@Component @ConfigurationProperties(prefix = "redis") public class RedisConfig { private String host; private int port; private int timeout; // private String password; private int poolMaxTotal; 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 poolMaxldle) { this.poolMaxIdle = poolMaxIdle; } public int getPoolMaxWait() { return poolMaxWait; } public void setPoolMaxWait(int poolMaxWait) { this.poolMaxWait = poolMaxWait; } private int poolMaxIdle; private int poolMaxWait; }
其中@ConfigurationProperties(prefix = “redis”)的作用是使用配置文件application.properties中开头为redis的配置信息。
- 建立RedisPoolFactory类将获取的配置信息加入连接池的信息。
@Service public class RedisPoolFactory { @Autowired RedisConfig redisConfig; @Bean public JedisPool JedisPoolFactory(){ JedisPoolConfig poolConfig=new JedisPoolConfig(); poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle()); poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal()); poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000); JedisPool jp =new JedisPool(poolConfig,redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000); return jp; } }
- 写RedisServer类实现对缓存的增删改查。
public class RedisService { @Autowired JedisPool jedisPool; 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); } } 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(); if (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(); } } }
其中Keyprefix的作用是给key加一个前缀,防止被后面存储的覆盖掉。
注意在使用完Jedis中的对象要用jedis.close();进行关闭释放。如果没有这段代码之后在使用后会出现‘以下错误
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
相关文章推荐
- Springboot整合redis实现缓存及其缓存运行原理浅析
- 企业级 SpringBoot 教程 (九)springboot整合Redis
- 【转】springboot2.x+mybait-plus +vue整合redis
- SpringBoot 2.x版本整合Redis集群
- Spring Boot对Redis的整合
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- springboot与redis整合案例(上)
- spring boot 前后端分离整合shiro(五)整合redis并实现并发登录控制
- SpringBoot整合Redis
- Spring Boot 整合 Redis 实现缓存操作
- idea springBoot整合redis
- SpringBoot 整合 Redis 的简单案例
- springboot 整合redis
- redis+springboot+Cache缓存整合
- springboot-整合redis
- Springboot整合redis
- spring-boot整合redis作为缓存(4)——spring-boot引入Redis
- springboot 整合 redis
- spring-boot+mybatis+redis整合