Redis实战-详细配置-优雅的使用Redis注解/RedisTemplate (2)

@MapperScan: 扫描mapper接口层

import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; /** * 启动类 * @author ludangxin * @date 2021/8/11 */ @EnableCaching @MapperScan(basePackages = "com.ldx.redis.mapper") @SpringBootApplication public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } } 2.4 redis配置 2.4.1 RedisConfig

我们除了在application.yaml中加入redis的基本配置外,一般还需要配置redis key和value的序列化方式,如下:

注解:

其默认的序列化方式为JdkSerializationRedisSerializer,这种方式跨语言和可读性都不太好,我们将其切换为Jackson2JsonRedisSerializer。

可以使用entryTtl()为对应的模块设置过期时长。

redisTemplate:参考redisTemplate()。

import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.ldx.redis.constant.CacheConstant; 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.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; import java.util.HashMap; import java.util.Map; /** * redis配置类 * @author ludangxin * @date 2021/8/11 */ @Configuration public class RedisConfig { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { //设置不同cacheName的过期时间 Map<String, RedisCacheConfiguration> configurations = new HashMap<>(16); // 序列化方式 Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = getJsonRedisSerializer(); RedisSerializationContext.SerializationPair<Object> serializationPair = RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer); // 默认的缓存时间 Duration defaultTtl = Duration.ofSeconds(20L); // 用户模块的缓存时间 Duration userTtl = Duration.ofSeconds(50L); // 默认的缓存配置 RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() //.entryTtl(defaultTtl) .serializeValuesWith(serializationPair); // 自定义用户模块的缓存配置 自定义的配置可以覆盖默认配置(当前的模块) configurations.put(CacheConstant.USER_CACHE_NAME, RedisCacheConfiguration.defaultCacheConfig() //.entryTtl(userTtl) .serializeValuesWith(serializationPair) ); return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory)) .cacheDefaults(redisCacheConfiguration) .withInitialCacheConfigurations(configurations) // 事物支持 .transactionAware() .build(); } @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = getJsonRedisSerializer(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jsonRedisSerializer); // 支持事物 //template.setEnableTransactionSupport(true); template.afterPropertiesSet(); return template; } /** * 设置jackson的序列化方式 */ private Jackson2JsonRedisSerializer<Object> getJsonRedisSerializer() { Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); redisSerializer.setObjectMapper(om); return redisSerializer; } } 2.4.1 CacheConstant

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxfyy.html