Jelajahi Sumber

新增手动创建RedisTemplate实例工具方法

woody 2 tahun lalu
induk
melakukan
22be141dcb

+ 4 - 0
framework-redis/pom.xml

@@ -25,6 +25,10 @@
             <version>${framework-base.version}</version>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>

+ 28 - 3
framework-redis/src/main/java/com/chelvc/framework/redis/util/RedisUtils.java

@@ -14,6 +14,7 @@ import java.util.concurrent.ThreadLocalRandom;
 import java.util.function.Function;
 import java.util.function.Supplier;
 
+import com.chelvc.framework.base.context.ApplicationContextHolder;
 import com.chelvc.framework.base.function.Executor;
 import com.chelvc.framework.base.util.ObjectUtils;
 import com.chelvc.framework.base.util.StringUtils;
@@ -26,14 +27,19 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.data.redis.connection.RedisConnection;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
 import org.springframework.data.redis.connection.RedisStringCommands;
 import org.springframework.data.redis.connection.ReturnType;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 import org.springframework.data.redis.core.RedisConnectionUtils;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
 import org.springframework.data.redis.core.script.DigestUtils;
 import org.springframework.data.redis.core.script.RedisScript;
 import org.springframework.data.redis.core.types.Expiration;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
@@ -154,6 +160,11 @@ public class RedisUtils implements ApplicationContextAware {
      */
     private static RedisConnectionFactory CONNECTION_FACTORY;
 
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        CONNECTION_FACTORY = applicationContext.getBean(RedisConnectionFactory.class);
+    }
+
     /**
      * 获取Redis连接工厂对象
      *
@@ -832,8 +843,22 @@ public class RedisUtils implements ApplicationContextAware {
         throw new RuntimeException("Generate random attempts limit exceeded");
     }
 
-    @Override
-    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-        CONNECTION_FACTORY = applicationContext.getBean(RedisConnectionFactory.class);
+    /**
+     * 构建RedisTemplate实例
+     *
+     * @param configuration 配置信息
+     * @return RedisTemplate实例
+     */
+    public static RedisTemplate<String, Object> template(@NonNull RedisStandaloneConfiguration configuration) {
+        RedisSerializer<?> keySerializer = new StringRedisSerializer();
+        RedisSerializer<?> valueSerializer = ApplicationContextHolder.getBean(Jackson2JsonRedisSerializer.class);
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(new JedisConnectionFactory(configuration));
+        redisTemplate.setKeySerializer(keySerializer);
+        redisTemplate.setValueSerializer(valueSerializer);
+        redisTemplate.setHashKeySerializer(keySerializer);
+        redisTemplate.setHashValueSerializer(valueSerializer);
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
     }
 }