|
@@ -0,0 +1,51 @@
|
|
|
+package edu.travel.cache.config;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
|
+import org.springframework.data.redis.core.script.RedisScript;
|
|
|
+import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+import org.springframework.scripting.support.ResourceScriptSource;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class RedisConfig {
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private RedisConnectionFactory redisConnectionFactory;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
|
|
|
+ redisTemplate.setConnectionFactory(factory);
|
|
|
+ Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
|
|
|
+ redisTemplate.setDefaultSerializer(serializer);
|
|
|
+
|
|
|
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
+
|
|
|
+ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
|
|
+
|
|
|
+ return redisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisMessageListenerContainer redisContainer() {
|
|
|
+ final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
|
+ container.setConnectionFactory(redisConnectionFactory);
|
|
|
+ return container;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisScript<Long> limitRedisScript() {
|
|
|
+ DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
|
|
|
+ redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/limit.lua")));
|
|
|
+ redisScript.setResultType(Long.class);
|
|
|
+ return redisScript;
|
|
|
+ }
|
|
|
+}
|