1 添加redis支持
在pom.xml中添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
2 redis配置
package com.wisely.ij.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.lang.reflect.Method; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{ @Bean public KeyGenerator wiselyKeyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean public CacheManager cacheManager( @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { return new RedisCacheManager(redisTemplate); } @Bean public RedisTemplate<String, String> redisTemplate( RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
3 redis服务器配置
# REDIS (RedisProperties) spring.redis.database= # database name spring.redis.host=localhost # server host spring.redis.password= # server password spring.redis.port=6379 # connection port spring.redis.pool.max-idle=8 # pool settings ... spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.sentinel.master= # name of Redis server spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
4 应用
测试两个实体类
package com.wisely.ij.domain; public class Address { private Long id; private String province; private String city; public Address(Long id,String province, String city) { this.id = id; this.province = province; this.city = city; } public Address() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
package com.wisely.ij.domain; public class User { private Long id; private String firstName; private String lastName; public User(Long id,String firstName, String lastName) { this.id = id ; this.firstName = firstName; this.lastName = lastName; } public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
使用演示
package com.wisely.ij.service; import com.wisely.ij.domain.Address; import com.wisely.ij.domain.User; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * Created by wisely on 2015/5/25. */ @Service public class DemoService { @Cacheable(value = "usercache",keyGenerator = "wiselyKeyGenerator") public User findUser(Long id,String firstName,String lastName){ System.out.println("无缓存的时候调用这里"); return new User(id,firstName,lastName); } @Cacheable(value = "addresscache",keyGenerator = "wiselyKeyGenerator") public Address findAddress(Long id,String province,String city){ System.out.println("无缓存的时候调用这里"); return new Address(id,province,city); } }
package com.wisely.ij.web; import com.wisely.ij.domain.Address; import com.wisely.ij.domain.User; import com.wisely.ij.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by wisely on 2015/5/25. */ @Controller public class DemoController { @Autowired DemoService demoService; @RequestMapping("/test") @ResponseBody public String putCache(){ demoService.findUser(1l,"wang","yunfei"); demoService.findAddress(1l,"anhui","hefei"); System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); return "ok"; } @RequestMapping("/test2") @ResponseBody public String testCache(){ User user = demoService.findUser(1l,"wang","yunfei"); Address address =demoService.findAddress(1l,"anhui","hefei"); System.out.println("我这里没执行查询"); System.out.println("user:"+"/"+user.getFirstName()+"/"+user.getLastName()); System.out.println("address:"+"/"+address.getProvince()+"/"+address.getCity()); return "ok"; } }
5 检验
先访问http://localhost:8080/test 保存缓存
再访问http://localhost:8080/test2 调用缓存里的数据
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存是个不错的选择,因此本文将介绍如何在Spring Boot的缓存支持中使用Redis进行数据缓存...
本篇文章主要介绍了详解Spring Boot使用redis实现数据缓存,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring boot集成redis做为通用缓存的实战demo,帮助大家彻底掌握s-cache-practice
Spring boot 集成 redis (解决String类型乱码) 缓存用redis
主要给大家介绍了关于Spring Boot中使用Redis做缓存的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2294942
Spring Boot 使用 Redis 实现缓存操作,用demo学习。欢迎下载留言。 使用参考:https://blog.csdn.net/x_san3/article/details/81634699
Spring Boot 整合mybatis redis netty 缓存 logback 项目 maven
Redis 是目前业界使用最广泛的内存数据存储。相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化。...本文介绍 Redis 在 Spring Boot 中两个典型的应用场景。
spring boot+redis+shiro+mybatis+thymeleaf整合框架,引入了缓存,可以使整个项目能快速访问资源,并且提供shiro安全框架保证了资源的安全性。spring boot,mybatis则是现在开发的最新框架技术。并且引入了...
主要介绍了Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot),需要的朋友可以参考下
spring中使用redis,将想要缓存的信息存入redis中,想取的时候取出来,想删的时候删除掉。
系统缓存管理 将redis的操作可视化,提供对redis的基本操作 Sql监控 采用 druid 监控数据库访问性能 技术栈 基础框架:Spring Boot 2.1.0.RELEASE 持久层框架:Spring boot Jpa 安全框架:Spring Security 缓存框架...
3.1、spring boot redis注解缓存Cacheable (value) 1
spring-boot-redis 该demo使用redis做缓存,mybatis作为ORM,查询数据后缓存在redis中
1.38 Spring Boot集成Redis实现缓存机制 1.39 Spring Boot Cache理论篇 1.40 Spring Boot集成EHCache实现缓存机制 1.41 Spring Boot分布式Session状态保存Redis 1.42 Spring Boot Shiro权限管理 1.43 Spring Boot ...
spring boot —— redis 缓存注解使用教程 示例项目地址: 依赖 在pom文件添加如下依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis 配置 在application.yml配置文件添加...
java Web现代化开发:Spring Boot + Mybatis + Redis二级缓存
编写Redis缓存操作示例 4.1 添加缓存注解 4.2 获取缓存数据 4.3 更新缓存数据 4.4 删除缓存数据 使用Redis实现分布式锁 5.1 设置分布式锁 5.2 释放分布式锁 Redis常用操作 6.1 字符串操作 6.2 哈希操作 6.3 列表操作...
主要介绍了spring boot+spring cache实现两级缓存(redis+caffeine),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧