SpringBoot整合redis简单例子
2019-03-19 10:37
549 查看
前言
redis是一种noSQL的数据库,以<Key, Value>的形式存储数据,其速度相比MySQL之类的数据库快,相当于内存读写与硬盘读写的差别,所以常常用作缓存。接下来就分别看看再SpringBoot项目中如何使用redis
整合前提:
前提是你已经安装redis,redis的安装这里不再赘述,有需要参考我的另一篇文章。
SpringBoot整合redis
1.引入依赖:
pom.xml
<!--redis引入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>1.7.1.RELEASE</version> </dependency> <!-- redis客户端--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
除了项目需要的依赖以及mySQL数据库需要的依赖等,还需引入以上两个即可。
注意:
注意这两个jar包的版本搭配问题,有些版本搭配可能会报错。小编尝试过这两个版本可用。
编写RedisUtil工具类
package com.silin.demo.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; /** * Create by silin * Date: 2019/3/13 12:06 */ @Component public class RedisClient { public static final long TOKEN_EXPIRES_SECOND = 1800; @Autowired private StringRedisTemplate redisTpl; /** * 向redis中设值 * @param key 使用 a:b:id的形式在使用rdm进行查看redis情况时会看到分层文件夹的展示形式,便于管理 * @param value * @return */ public boolean set(String key, String value) { boolean result = false; try { redisTpl.opsForValue().set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 向redis中设置,同时设置过期时间 * @param key * @param value * @param time * @return */ public boolean set(String key, String value, long time) { boolean result = false; try { redisTpl.opsForValue().set(key, value); expire(key, time); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 获取redis中的值 * @param key * @return */ public String get(String key) { String result = null; try { result = redisTpl.opsForValue().get(key); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 设置key的过期时间 * @param key * @param time * @return */ public boolean expire(String key, long time) { boolean result = false; try { if(time > 0) { redisTpl.expire(key, time, TimeUnit.SECONDS); result = true; } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 根据key删除对应value * @param key * @return */ public boolean remove(String key) { boolean result = false; try { redisTpl.delete(key); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
在所需要的服务层调用方法即可使用,等下还要在application.yml中配置。
3.进行配置
找到application.yml文件,配置代码如下:
spring: datasource: redis: database: 0 # Redis数据库索引(默认0) host: 127.0.0.1 # Redis服务器地址 port: 6379 # Redis服务器连接端口 password: # Redis服务器连接密码(默认为空) jedis: pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 8 # 连接池中的最大空闲连接 min-idle: 0 # 连接池中的最小空闲连接 timeout: 0 # 连接超时时间(毫秒)
相关文章推荐
- spring boot进阶(一) springboot整合redis,可操作java对象。最完整、简单易懂、详细的spring boot教程。
- springboot整合mybatis最简单的例子
- Swagger(一) SpringBoot整合Swagger2简单的例子
- Redis使用(二):SpringBoot整合Redis 配置文件及项目种简单应用
- SpringBoot2.X 整合RedisTemplate 简单实现消息队列
- springboot整合activemq简单例子,附上源码链接
- Swagger(一) SpringBoot整合Swagger2简单的例子
- Swagger(一) SpringBoot整合Swagger2简单的例子
- SpringBoot整合redis集群并使用StringRedisTemplate和RedisTemplate简单操作Redis集群
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- 【spring boot】SpringBoot初学(8)– 简单整合redis
- SpringBoot 整合 Redis 的简单案例
- Spring Boot整合集成StringRedisTemplate的简单记录
- SpringBoot 整合 Redis 的简单案例
- Spring boot整合redis小例子
- springboot与redis的简单整合实例
- 记录springboot 整合redis
- spring整合redis简单demo
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- 在Springboot中整合Ehcache以及整合Redis