https://blog.csdn.net/weixin_43704975/article/details/97914158
此文采用redis作文缓存中间件
demo
https://github.com/fastmaybe/cache
springboot采用的2.16版本
org.springframework.boot
spring-boot-starter-data-redis
pom文件的properties
UTF-8
UTF-8
1.8
127.0.0.1
3306
root
123456
yml
spring:
datasource:
url: jdbc:mysql://@mysql_ip@:@mysql_port@/clouddb01?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username: @mysql_username@
password: @mysql_password@
redis:
host: 127.0.0.1
port: 6379
password: 123456
logging:
level:
com.springboot.cache.mapper: debug #打印sql
背景:解决redis存入的是进制文件,我们需要看到直观的json格式
先说说RedisTemplete和StringRedisTemplete区别
最主要常见区别是序列化不同
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//设置采用Jackson2JsonRedisSerializer序列化方式,可以直观看到json
template.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class));
return template;
}
}
此时如果是自己使用RedisTemplete编码使用缓存,存入的值可以是json展示的。
针对springboot1.x和2.x版本区别比较大
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class));
return template;
}
/**
如果参数类型所对应的实例在spring容器中只有一个,则默认选择这个实例。如果有多个,
则需要根据参数名称来选择(参数名称就相当于是spring的配置文件中的bean的id)
*
*/
@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate){
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
// 使用前缀,默认会将CacheName作为key的前缀
redisCacheManager.setUsePrefix(true);
return redisCacheManager;
}
}
@Configuration
public class MyRedisCacheConfig {
/**
* 方式一 建造者 使用GenericJackson2JsonRedisSerializer
* GenericJackson2JsonRedisSerializer序列化后格式:
*
* {
* "@class": "com.springboot.cache.pojo.User",
* "id": 6,
* "name": "瑞文",
* "email": "ruiwen"
* }
*
* @param factory
* @return
*/
// @Bean
public RedisCacheManager cacheManager_one(RedisConnectionFactory factory) {
RedisCacheConfiguration cacheConfiguration =
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();
}
/**
* 方式二 构造器 使用Jackson2JsonRedisSerializer
* Jackson2JsonRedisSerializer序列化后格式:
*
* {
* "id": 7,
* "name": "亚索",
* "email": "yasuo"
* }
*
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisCacheManager cacheManager_two(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//设置CacheManager的值序列化方式为json序列化
Jackson2JsonRedisSerializer objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair
.fromSerializer(objectJackson2JsonRedisSerializer);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
//设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
return redisCacheManager;
}
}
GenericJackson2JsonRedisSerializer:
{
"@class": "com.springboot.cache.pojo.User",
"id": 6,
"name": "瑞文",
"email": "ruiwen"
}
Jackson2JsonRedisSerializer:
{
"id": 6,
"name": "瑞文",
"email": "ruiwen"
}
在没有导入缓存中间件的时候,CacheManager为SimpleCacheManager,,并且使用ConcurrentMap来存储数据。
在yml配置文件设置 debug= true在项目启动的时候,可以看到其子类只有SimpleCacheManager。
Boolean b = (Boolean) redisTemplate.execute(new RedisCallback() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
RedisSerializer keySerializer = redisTemplate.getKeySerializer();
Object obj = connection.execute("set", keySerializer.serialize("aaaaa"),
valueSerializer.serialize("bbbb"),
SafeEncoder.encode("NX"),
SafeEncoder.encode("EX"),
Protocol.toByteArray(20L));
return obj != null;
}
});
System.out.println(b);