SpringBoot默认开启的缓存管理器是ConcurrentMapCacheManager,创建缓存组件是ConcurrentMapCache,将缓存数据保存在一个个的ConcurrentMap
1、整合Redis
a、引入Redis的starter
<dependency>
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
引入redis的starter之后,会在容器中加入redis相关的一些bean,其中有两个跟操作redis相关的:RedisTemplate和StringRedisTemplate(用来操作字符串:key和value都是字符串),template中封装了操作各种数据类型的操作(stringRredisTemplate.opsForValue()、stringRredisTemplate.opsForList()等)
@Configuration
@ConditionalOnClass({JedisConnection.class, RedisOperations.class, Jedis.class})@EnableConfigurationProperties({RedisProperties.class})public class RedisAutoConfigurationRedisAutoConfiguration {public RedisAutoConfiguration() {}@Configurationprotected static class RedisConfigurationRedisConfiguration {protected RedisConfiguration() {}@Bean@ConditionalOnMissingBean(name = {"redisTemplate"})public RedisTemplate<ObjectObject, Object Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {RedisTemplate<ObjectObject, Object Object> template = new RedisTemplateRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}@Bean@ConditionalOnMissingBean({StringRedisTemplate.class})public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplateStringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}}//...}
b、配置redis:只需要配置redis的主机地址(端口默认即为6379,因此可以不指定)
spring.redis.host=192.168.2.107
c、测试
@RunWith(SpringRunner.class)
@SpringBootTestpublic class SpringbootCacheApplicationTestsSpringbootCacheApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void contextLoads() {stringRedisTemplate.opsForValue().append("msg","hengheng");redisTemplate.opsForValue().set("a","aa");}}
使用redis存储对象时,该对象必须可序列化(实现Serializable接口),否则会报错,此时存储的结果在redis的管理工具中查看如下:由于序列化的原因值和键都变为了另外一种形式
SpringBoot默认采用的是JDK的对象序列化方式,我们可以切换为使用JSON格式进行对象的序列化操作,这时需要我们自定义序列化规则(当然我们也可以使用Json工具先将对象转化为Json格式之后再保存至redis,这样就无需自定义序列化),参考RedisAutoConfiguration向容器中加入RedisTemplate:修改下泛型参数
@Configuration
public class MyRedisConfigMyRedisConfig {@Beanpublic RedisTemplate<ObjectObject, Employee Employee> empRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {RedisTemplate<ObjectObject, Employee Employee> template = new RedisTemplateRedisTemplate();template.setConnectionFactory(redisConnectionFactory);//这里选用Json格式的对象序列化器Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializerJackson2JsonRedisSerializer(Employee.class);template.setDefaultSerializer(serializer);//设置默认的序列化器为Json序列化器return template;}}
在使用时将新加入IOC容器的bean注入作为redisTemplate即可:
@RunWith(SpringRunner.class)
@SpringBootTestpublic class SpringbootCacheApplicationTestsSpringbootCacheApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate RedisTemplate<ObjectObject, Employee Employee> empRedisTemplate;@Autowiredprivate EmployeeMapper employeeMapper;@Testpublic void contextLoads() {Employee empById = employeeMapper.getEmpById(1);//使用JSon格式序列化empRedisTemplate.opsForValue().set("emp-01",empById);redisTemplate.opsForValue().set("emp-01",empById);}}