RedisConfig.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.example.config;
  2. import com.alibaba.fastjson.parser.ParserConfig;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.io.ClassPathResource;
  6. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.script.DefaultRedisScript;
  10. import org.springframework.data.redis.serializer.RedisSerializationContext;
  11. import org.springframework.data.redis.serializer.StringRedisSerializer;
  12. import org.springframework.scripting.support.ResourceScriptSource;
  13. import java.time.Duration;
  14. /**
  15. * @author: Oliver.li
  16. * @Description:
  17. * @date: 2018/6/26 10:21
  18. */
  19. @Configuration
  20. public class RedisConfig {
  21. /**
  22. * 设置redisTemplate序列化方式
  23. * @param redisConnectionFactory
  24. * @return
  25. */
  26. @Bean
  27. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  28. com.alibaba.fastjson.support.spring.FastJsonRedisSerializer<Object> fastJsonRedisSerializer =
  29. new com.alibaba.fastjson.support.spring.FastJsonRedisSerializer<>(Object.class);
  30. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  31. RedisTemplate<String, Object> template = new RedisTemplate<>();
  32. template.setConnectionFactory(redisConnectionFactory);
  33. template.setKeySerializer(stringRedisSerializer);
  34. template.setHashKeySerializer(stringRedisSerializer);
  35. template.setValueSerializer(fastJsonRedisSerializer);
  36. template.setHashValueSerializer(fastJsonRedisSerializer);
  37. template.afterPropertiesSet();
  38. return template;
  39. }
  40. /**
  41. * 设置 @Cacheable 序列化方式
  42. * 设置过期时间为1天
  43. * @return
  44. */
  45. @Bean
  46. public RedisCacheConfiguration redisCacheConfiguration(){
  47. FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<>(Object.class);
  48. ParserConfig parserConfig = ParserConfig.getGlobalInstance();
  49. parserConfig.addAccept("com.llf.demo.");
  50. parserConfig.addAccept("com.github.pagehelper.");
  51. RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
  52. configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer)).entryTtl(Duration.ofDays(1));
  53. return configuration;
  54. }
  55. /**
  56. * 分布式限流lua脚本
  57. * @return
  58. */
  59. @Bean
  60. public DefaultRedisScript<Number> rateLimitLua(){
  61. DefaultRedisScript<Number> redisScript = new DefaultRedisScript<>();
  62. redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("rateLimit.lua")));
  63. redisScript.setResultType(Number.class);
  64. return redisScript;
  65. }
  66. }