spring boot redis 整合
2017-08-03 17:02
609 查看
摘要: spring boot redis
又很久没有更新博客了,今天再写一点吧.spring boot 使用越来越多,redis 也是很普及的cache, 现在用把两者做一下整合.
环境: spring boot 1.4.2,redis3.0.1, maven 3.3.9, jdk1.8, IDEA2016.3.3
redis布署环境可以参考: redis 安装配置
maven 布署环境可以参考: Maven 构建web工程
spring boot 部署环境可以参考:spring boot first
IDEA 部署环境 ,我的博客里暂时没有参考.
step 1: 配置 pom.xml
倒数第三个依赖,可以看出模板用的velocity,exclusions 因为我的项目里其它的依赖已经用到slf4j,会提示重复的引用,所以这里去掉。最后2个依赖是redis需要用到的,这个项目的核心.
setp2:
application.properties 配置
没有用默认的端口,用的是7001。
step3 : 建立操作redis的service
查询时使用:@Cacheable
插入数据使用:@CachePut
删除、更新时使用:@CacheEvict
step 4:
建controller
加入查询,更新,删除页面接口,代码如下:
step5 :
为查询controller建 显示结果页面src/main/resource/templates/redisList.vm,内容:
step6 :
启动redis :start redis-server ./7001/redis.conf
打开redis client
redis-cli -h 127.0.0.1 -p 7001
step7:
run 工程
查看结果:
看一下reids 里user_1 里的数据
get user_1
1. 查询
findUser是查询的接口,查询出redis的数据
2. 更新
查看redis
数据被更新
3.删除
查看redis
补充:@Cacheable 查询时,在没有数据时,会自动创建一条记录.
又很久没有更新博客了,今天再写一点吧.spring boot 使用越来越多,redis 也是很普及的cache, 现在用把两者做一下整合.
环境: spring boot 1.4.2,redis3.0.1, maven 3.3.9, jdk1.8, IDEA2016.3.3
redis布署环境可以参考: redis 安装配置
maven 布署环境可以参考: Maven 构建web工程
spring boot 部署环境可以参考:spring boot first
IDEA 部署环境 ,我的博客里暂时没有参考.
step 1: 配置 pom.xml
<modelVersion>4.0.0</modelVersion> <groupId>spring-boottest</groupId> <artifactId>spring-boottest</artifactId> <version>1.0-SNAPSHOT</version> <!--父依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.0.RELEASE</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
倒数第三个依赖,可以看出模板用的velocity,exclusions 因为我的项目里其它的依赖已经用到slf4j,会提示重复的引用,所以这里去掉。最后2个依赖是redis需要用到的,这个项目的核心.
setp2:
application.properties 配置
# 服务端口 server.port=8081
spring.velocity.resource-loader-path=classpath:/templates/
# REDIS (RedisProperties)
spring.redis.host=127.0.0.1
spring.redis.port=7001
spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1
没有用默认的端口,用的是7001。
step3 : 建立操作redis的service
@Service public class DemoService {
@Cacheable(value = "user",key = "'user_'+#user.getId()") public User findUserCache(User user){ System.out.println("findUserCache"); return user; } @CachePut(value = "user",key = "'user_'+#user.getId()") public User editUserCache(User user){ System.out.println("editUserCache"); return user; } @CacheEvict(value = "user",key = "'user_'+#user.getId()") public User deleteUserCache(User user){ System.out.println("deleteUserCache"); return user; } }
查询时使用:@Cacheable
插入数据使用:@CachePut
删除、更新时使用:@CacheEvict
step 4:
建controller
加入查询,更新,删除页面接口,代码如下:
@RequestMapping("/findUser") // @ResponseBody public String findUserCache( @RequestParam(value="userId",required = false) long userId,Map<String, Object> model){ User user=new User(); user.setId((long)userId); User user1= demoService.findUserCache(user); System.out.println("缓存的数据(如果有Id和名字,是读的缓存,如果没有名字是新建的缓存)"); System.out.println("user:"+"/"+user1.getFirstName()+"/"+user1.getLastName()); Map<String,String> paramValues=new HashMap<String, String>(); paramValues.put("id",String.valueOf(user1.getId())); paramValues.put("firstName",user1.getFirstName()); paramValues.put("lastName",user1.getLastName()); model.put("paramValues", paramValues); return "redisList"; // return "userId:"+user1.getId().toString()+",userFirstName:"+user1.getFirstName()+",userLastName:"+user1.getLastName(); } @RequestMapping("/editUser") @ResponseBody public String editUserCache( @RequestParam(value="userId",required = false) long userId, @RequestParam(value="firstName",required = false) String firstName, @RequestParam(value="lastName",required = false) String lastName ){ User user=new User(); user.setId((long)userId); user.setFirstName(firstName); user.setLastName(lastName); User user1= demoService.editUserCache(user); return "userId:"+user1.getId().toString()+",userFirstName:"+user1.getFirstName()+",userLastName:"+user1.getLastName(); } @RequestMapping("/deleteUser") @ResponseBody public String editUserCache( @RequestParam(value="userId",required = false) long userId){ User user=new User(); user.setId((long)userId); User user1= demoService.deleteUserCache(user); return "userId:"+user1.getId().toString()+",userFirstName:"+user1.getFirstName()+",userLastName:"+user1.getLastName(); } }
step5 :
为查询controller建 显示结果页面src/main/resource/templates/redisList.vm,内容:
#foreach($param in ${paramValues.keySet()}) <tr> <th>$param:</th> <td>${paramValues.get($param)}</td> <br> </tr> #end
step6 :
启动redis :start redis-server ./7001/redis.conf
打开redis client
redis-cli -h 127.0.0.1 -p 7001
step7:
run 工程
查看结果:
看一下reids 里user_1 里的数据
get user_1
1. 查询
findUser是查询的接口,查询出redis的数据
2. 更新
查看redis
数据被更新
3.删除
查看redis
补充:@Cacheable 查询时,在没有数据时,会自动创建一条记录.
相关文章推荐
- 使用 SpringBoot 之 JPA 整合 Redis 实现缓存
- 【转载】Springboot整合 一 集成 redis
- redis学习系列(二)--spring boot整合Redis集群
- (七)Spring Boot 整合Redis
- springboot整合redis
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- SpringBoot整合Redis(单机+集群) —— 含Spring版附件文档
- springboot学习笔记-3 整合redis&mongodb【转载】
- SpringBoot整合Redis(附带序列化方式对比)
- spring boot整合redis主从sentinel
- Spring Boot学习之旅:(六)springboot 整合 redis 以及 redis 通用工具类
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- spring boot整合redis实现shiro的分布式session共享
- SpringBoot整合Redis、ApachSolr和SpringSession的示例
- springboot整合redis进行数据操作(推荐)
- spring boot 整合redis
- springboot整合redis
- SpringBoot 整合redis实现缓存 记录@CachePut值为1
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- SpringBoot整合常用技术--MyBatis、Redis、定时任务、分页插件、逆向生成