springboot整合redis
2017-12-15 14:58
465 查看
上文说到springboot整合jpa和模版来实现了一个登录的功能(有兴趣的可以把工程项目中的其他功能实现),本文承接上次的项目结构,在其基础上再加入redis,在查询数据库之前先从redis中查询可以减少数据库的压力。
安装步骤
本机为32位,我使用的是32位redis。通过上面地址下载的redis解压后选择32位文件夹打开,双击
redis-server.exe启动服务,启动后窗口不要关闭,然后咱们启动客户端redis-cli.exe,下面来验证下:
输入set key1 “helloword_monkey”
get key1出现如下结果恭喜redis服务已经正常启动
打开pom.xml加入相应的redis的依赖
3.配置application.properties
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.maxIdle=10
spring.redis.maxTotal=5
spring.redis.maxWaitMillis=5000
spring.redis.testOnBorrow=false
spring.redis.index=11
4.构造redis的工具类
5.redis在代码中的运用(本次介绍查库之前的操作redis)
本次demo是直接把数据转化成json串来存入redis和取出,使用的是fastjson,内附fastjson的工具类。在查询数据库之前先对比对redis是否存在,存在则从redis中直接取出,不存在则查询数据库,同时放入redis中方便下次使用(最好设置key的有效期,以免日后成为垃圾数据)
完成这一切之后就可以验证了,启动项目,打开游览器输入:http://localhost:8888
进行登录(本次习惯使用ip地址)
代码地址:
https://gitee.com/chenghao842822530/springbootdemo
代码中还有配置多redis(生产中可能会使用redis集群),另外还有另外2中redis配置连接方式
上一篇springboot-jpa-thymeleaf:http://blog.csdn.net/helloword_monkey/article/details/78805381
下文预告:springboot+duboo配合注册中心zookeeper
redis 是什么?
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中,redis在企业项目中应用广泛,特别是在日数据流量较大的项目中。redis还可作为临时数据设置其key有效的期限。。。redis安装
使用redis需要先安装其服务,redis可去其官网下载或者自己百度,google下载。也可通过https://gitee.com/chenghao842822530/springbootdemo/attach_files,32位,64位系统根据自己系统可自行选择。安装步骤
本机为32位,我使用的是32位redis。通过上面地址下载的redis解压后选择32位文件夹打开,双击
redis-server.exe启动服务,启动后窗口不要关闭,然后咱们启动客户端redis-cli.exe,下面来验证下:
输入set key1 “helloword_monkey”
get key1出现如下结果恭喜redis服务已经正常启动
整合步骤
快速搭建springboot的工程项目(前文中有介绍)本次工程项目沿用上文的springbbot+jpa的工程打开pom.xml加入相应的redis的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.3.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
3.配置application.properties
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.maxIdle=10
spring.redis.maxTotal=5
spring.redis.maxWaitMillis=5000
spring.redis.testOnBorrow=false
spring.redis.index=11
4.构造redis的工具类
@Component public class RedisClient1 { @Resource(name="redisTemplate") private StringRedisTemplate template; //redis set数据 public void setData(String billcode,String json){ template.opsForValue().set(billcode, json); } //redis get数据 public String getData(String billcode){ String strJson = template.opsForValue().get(billcode); return strJson; } //redis 删除数据 public void delData(String billcode){ template.delete(billcode); } //其余方法省略 。。。。 }
5.redis在代码中的运用(本次介绍查库之前的操作redis)
@Autowired private RedisClient1 redisClient1; //几种redis使用根据你的情况随便使用 /*@Autowired private RedisTemplate redisTemplate;*/ String key1 = "user_login"; public User findByUserNameAndPassword(String userName, String passWord) { User user = new User(); //也可以直接在Clinet里直接写入判断是否存在的方法,这里我没有写入就直接根据结果string判断了 String userjson = redisClient1.getData(key1); if (userjson!=null) { user = JsonUtil.parse(userjson, User.class); } else { user = userDao.findByUserNameAndPassWord(userName, passWord); if(user!=null){ redisClient1.setData(key1,JsonUtil.toJSON(user)); } } return user; }
本次demo是直接把数据转化成json串来存入redis和取出,使用的是fastjson,内附fastjson的工具类。在查询数据库之前先对比对redis是否存在,存在则从redis中直接取出,不存在则查询数据库,同时放入redis中方便下次使用(最好设置key的有效期,以免日后成为垃圾数据)
完成这一切之后就可以验证了,启动项目,打开游览器输入:http://localhost:8888
进行登录(本次习惯使用ip地址)
代码地址:
https://gitee.com/chenghao842822530/springbootdemo
代码中还有配置多redis(生产中可能会使用redis集群),另外还有另外2中redis配置连接方式
上一篇springboot-jpa-thymeleaf:http://blog.csdn.net/helloword_monkey/article/details/78805381
下文预告:springboot+duboo配合注册中心zookeeper
相关文章推荐
- springboot对接第三方短信接口整合redis,ActiveMQ
- Spring Boot对Redis的整合
- SpringBoot整合Redis及Redis工具类撰写
- spring boot整合redis(单机+集群)
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- springboot整合redis
- spring-boot整合redis作为缓存(2)——spring-boot的缓存
- 基于SpringBoot整合redis及shiro
- spring boot redis 整合
- spring-boot 整合redis作为数据缓存
- spring boot整合redis
- springboot2.0整合redis案例
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- 整合spring-boot-starter-data-redis报错解决
- Redis学习(3.1)spring boot整合Redis速成
- spring-boot整合redis作为mysql二级缓存
- SpringBoot整合redis哨兵主从服务
- spring boot 整合mybatis、redis、 spring mvc基本配置
- spring-boot 整合redis作为数据缓存
- Spring Boot 整合 Redis 实现缓存操作