1.虚拟机中docker中下载redis镜像,并且开启镜像。
2.windows系统中,使用客户端连接docker
3.springboot中引入starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
4.配置文件配置redis地址
5.测试操作数据
@Test
public void test4(){
Employee emp = employeeMapper.getEmpById(1);
redisTemplate.opsForValue().set("emp_01",emp);
}
使用了jdk序列化机制,将序列化后的数据保存到了redis
6.改变序列化后的数据为json
使用自定义的序列化器,改变默认的序列化规则
/**
* @author 孙一鸣 on 2020/3/13
*/
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate
(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template;template.setDefaultSerializer(serializer);
return
}
}
重新测试:
@Autowired
RedisTemplate<Object, Employee> empRedisTemplate;//自定义的序列化器
@Test
public void test5(){
Employee emp = employeeMapper.getEmpById(1);
empRedisTemplate.opsForValue().set("emp_01",emp);
}
7.原理解析
1、自动配置类;CacheAutoConfiguration
2.缓存的配置类
org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration
org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默认】
org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
3、哪个配置类默认生效:SimpleCacheConfiguration;
4.默认给容器中注册了一个CacheManager:ConcurrentMapCacheManager 可以获取和创建ConcurrentMapCache类型的缓存组件;他的作用将数据保存在ConcurrentMap中;
5.因为引入了redis的stater
容器中保存了redisCacheManager,因为此时的缓存管理器使用得是序列化后的Template,我们需要自定义redisCacheManager,使用我们自己的Template
8.配置文件
package com.sun.springboot.config;
import com.sun.springboot.bean.Employee;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.net.UnknownHostException;
import java.time.Duration;
/**
* @author 孙一鸣 on 2020/3/13
*/
@Configuration
public class MyRedisConfig {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate
(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
// 配置连接工厂
template.setConnectionFactory(redisConnectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
// 值采用json序列化
template.setDefaultSerializer(serializer);
return template;
}
/**
* 缓存管理器
*/
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//设置CacheManager的值序列化方式为json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
//设置默认超过期时间是30秒
// defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
}