springboot 接入redis作為快取
阿新 • • 發佈:2022-05-15
1.pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.1.6.RELEASE</version> </dependency>
2. 配置
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; 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.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * redis有三種模式-standalone,sentinel,cluster, * @EnableCaching 開啟快取 */ @Configuration @EnableCaching public class SpringRedisSessionConfig{ private String redisHost = "127.0.0.1"; private Integer port = 6379; @Bean("connectionFactory") public RedisConnectionFactory redisStandaloneMasterConnectionFactory() { RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(); redisConfig.setHostName(redisHost); redisConfig.setPort(port); redisConfig.setDatabase(0); RedisConnectionFactory lettuceConnectionFactory = new JedisConnectionFactory(redisConfig); return lettuceConnectionFactory; } @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration redisCacheConfiguration= RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(1000)); //entryTtl 代表過期時間 return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) .cacheDefaults(redisCacheConfiguration).build(); } // 物件序列化 @Bean public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){ RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); //使用Jackson2JsonRedisSerializer替換預設的序列化規則 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper=new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //設定value的序列化規則 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //設定key的序列化規則 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } /** * sentinel模式的通道提供 * @return */ // @Bean("redisSentinelctionFactory") // public RedisConnectionFactory redisSentinelctionFactory() { // RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration() // .master("mymaster") // .sentinel("10.70.33.238", 26379) // .sentinel("10.70.33.239", 26379) // .sentinel("10.70.33.246", 26379); // return new JedisConnectionFactory(redisSentinelConfiguration); // } /** * cluster模式的通道提供 * @return */ // @Bean("redisRedisClusterFactory") // public RedisConnectionFactory redisSentinelctionFactory() { // RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration() // RedisNode redisNode1 = new RedisNode("127.0.0.1", 6379); // 有多少個節點就寫多少個節點 // RedisNode redisNode2 = new RedisNode("127.0.0.1", 6379); // redisClusterConfiguration.addClusterNode(redisNode1); // redisClusterConfiguration.addClusterNode(redisNode2); // return new JedisConnectionFactory(redisClusterConfiguration); // } }
3.定義key 生成規則
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.lang.reflect.Method; /** * 自定義快取的 key生成 */ @Component public class MyKeyGenerator implements KeyGenerator { @Override public Object generate(Object o, Method method, Object... objects) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(o.getClass().getSimpleName()) .append("_") .append(method.getName()) .append("_") .append(StringUtils.arrayToDelimitedString(objects, "_")); return stringBuffer.toString(); } }
4. 使用