网上的资料多是springboot2.0以下的版本,导致出现了很多坑,该文章用来记录整合过程及其作用
package com.example.demo.redis;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.*;
import org.springframework.data.redis.serializer.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jacksonSeial);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
//设置默认超过期时间是1天
defaultCacheConfig.entryTtl(Duration.ofDays(1));
//初始化RedisCacheManager
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
/**
* retemplate相关配置
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
//RedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
当前选用的序列化为JSON序列化
选用JSON序列化的好处是可以进行交换数据
摘自 @xuzuning 用户在某贴的回复
二进制序列化 是将对象的内存映射抽取出来形成字符串,还原时只有一个重新分配内存的过程。
如果不指定序列化方式,默认使用JDK自带的序列化,即二进制序列化
package com.example.demo.service.imp;
import com.example.demo.mapper.BigDataMapper;
import com.example.demo.pojo.BigData;
import com.example.demo.service.BigDataService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
@CacheConfig(cacheNames = "bigdata")
public class BigDataServiceimp implements BigDataService {
@Resource
BigDataMapper bigDataMapper;
@Autowired
RedisTemplate redisTemplate;
@Override
public List<BigData> getAll() {
return bigDataMapper.getAll();
}
@Override
public PageInfo<BigData> findPage(int start, int end) {
return null;
}
@Override
@Cacheable(key = "'id:'+#id")
public BigData getOne(int id) {
return bigDataMapper.getOne(id);
}
@Override
public BigData getOne_notCache(int id){
ValueOperations v=redisTemplate.opsForValue();
BigData b= (BigData) v.get("bigdata::id:"+id);
if(null!=b){
//缓存存在直接从缓存中拿
return b;
}else{
//如果缓存中不存在 则 从数据库里读取 并 将读取的数据放置缓存
b=bigDataMapper.getOne(id);
System.out.println(b);
if (null!=b)v.set("bigdata::id:"+id,b);
}
return b;
}
@Override
public int addOne(BigData bigData) {
return bigDataMapper.addBigData(bigData);
}
}
其中
@CacheConfig(cacheNames = "bigdata")
指定使用CacheXXX 在缓存中存放的前缀
按理 id 存放在Redis中应该是 bigdata:id:#{id} 这种格式,但是不知道什么原因存放的格式为 bigdata::id:#{id} 这种格式
所以 RedisTemplate 中存放的格式也加入: