又很久没有更新博客了,今天再写一点吧.spring boot 使用越来越多,redis 也是很普及的cache, 现在用把两者做一下整合. 环境: spring boot 1.4.2,redis3.0
又很久没有更新博客了,今天再写一点吧.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
@Servicepublic 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 查询时,在没有数据时,会自动创建一条记录.