25、springboot与缓存整合Redis
作者:互联网
默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中
docker: 安装Redis:
查看官方文档: 添加约束
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
此时redis就引入再容器中 可以查看自动配置的类:RedisAutoConfiguration.class
public class RedisAutoConfiguration { public RedisAutoConfiguration() { } @Bean @ConditionalOnMissingBean( name = {"redisTemplate"} ) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
在配置文件中引入redis的地址:
测试: 此时两个操作redis的类都在容器中:
//操作kv键值对的 @Autowired RedisTemplate redisTemplate; //操作kv都是字符串 @Autowired StringRedisTemplate stringRedisTemplate;
测试:
测试添加对象: 对象实现类需要实现序列化
public class Employee implements Serializable {
//测试保存对象 @Test public void test2(){ //保存的是emp的对象 Employee emp = employeeMapper.getEmpById(1); //保存的是employee的对象 //默认如果使用保存对象,使用jdk序列化机制,序列化后的数据保存在redis中 redisTemplate.opsForValue().set("emp01",emp); }
//测试天对保存对象2 首先是自动一序列化器
@Configuration public class redisConfig { //专门序列化Employee @Bean public RedisTemplate<Object, Employee> redisTemplateEmp(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Employee> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Employee> json = new Jackson2JsonRedisSerializer<Employee>(Employee.class); template.setDefaultSerializer(json); return template; } }
@Autowired RedisTemplate redisTemplateEmp; //测试保存对象 @Test public void test2(){ //保存的是emp的对象 Employee emp = employeeMapper.getEmpById(1); //将数据以json的方式 //实现redisTemplate默认的序列化规则,改变默认的序列化规则 redisTemplateEmp.opsForValue().set("emp1",emp); }
测试缓存:
原理: 1、引入redis的starter,容器自动导入的是RedisCacheManage 2、RedisCacheManager帮我们自动创建RedisCache, redis通过操作redis缓存数据的RedisCacheConfiguration.class @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader())); List<String> cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet(cacheNames)); } return (RedisCacheManager)this.customizerInvoker.customize(builder.build()); }
protected Collection<RedisCache> loadCaches() { List<RedisCache> caches = new LinkedList(); Iterator var2 = this.initialCacheConfiguration.entrySet().iterator(); while(var2.hasNext()) { Entry<String, RedisCacheConfiguration> entry = (Entry)var2.next(); caches.add(this.createRedisCache((String)entry.getKey(), (RedisCacheConfiguration)entry.getValue())); } return caches; }查询之后。再点击刷新依然是这个页面
3、默认保存数据都是k-v都是object,利用序列化来保存 查看缓存:
4、让保存的数据为json 1.引入redis的starter,cacheManager变为RedisCacheManager 2.默认创建的RedisCacheManager,再操作数据的 RedisConnectionFactory RedisCacheConfiguration
@Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader())); List<String> cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet(cacheNames)); } return (RedisCacheManager)this.customizerInvoker.customize(builder.build()); }
public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean( name = {"redisTemplate"} ) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
3.默认使用的是JdkSerializationRedisSerializer
RedisTemplate.java public void afterPropertiesSet() { super.afterPropertiesSet(); boolean defaultUsed = false; if (this.defaultSerializer == null) { this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader()); } }
4.自定义 springboot的1.5
标签:25,RedisCacheManager,springboot,redis,redisConnectionFactory,Redis,template,publ 来源: https://www.cnblogs.com/Mrchengs/p/10435316.html