Browse Source

del jetcache

GITZYY 7 months ago
parent
commit
d04bf01727

+ 6 - 2
egress-gateway-service-infrastructure/pom.xml

@@ -55,8 +55,12 @@
             <artifactId>mybatis-plus-boot-starter</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-redis</artifactId>
+            <groupId>com.alicp.jetcache</groupId>
+            <artifactId>jetcache-starter-redis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
         </dependency>
     </dependencies>
 </project>

+ 0 - 48
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/config/redis/FastJson2RedisSerializer.java

@@ -1,48 +0,0 @@
-package com.hrsk.cloud.eg.infrastructure.config.redis;
-
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONReader;
-import com.alibaba.fastjson2.JSONWriter;
-import com.alibaba.fastjson2.filter.Filter;
-import org.apache.commons.lang3.ArrayUtils;
-import org.springframework.data.redis.serializer.RedisSerializer;
-import org.springframework.data.redis.serializer.SerializationException;
-
-/**
- * @description:  fast2Json 序列化类
- * @param:
- * @return:
- * @author zhangyy
- * @date: 2024/8/29 10:30
- */
-public class FastJson2RedisSerializer<T> implements RedisSerializer<T> {
-
-    static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(
-            // 按需加上需要支持自动类型的类名前缀,范围越小越安全
-            "com.***.***"
-    );
-
-    private final Class<T> clazz;
-
-    public FastJson2RedisSerializer(Class<T> clazz) {
-        super();
-        this.clazz = clazz;
-    }
-
-    @Override
-    public byte[] serialize(T t) throws SerializationException {
-        if (t == null) {
-            return new byte[0];
-        }
-        return JSON.toJSONBytes(t, JSONWriter.Feature.WriteClassName);
-    }
-
-    @Override
-    public T deserialize(byte[] bytes) throws SerializationException {
-        if (ArrayUtils.isEmpty(bytes)) {
-            return null;
-        }
-
-        return JSON.parseObject(bytes, clazz, AUTO_TYPE_FILTER);
-    }
-}

+ 0 - 51
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/config/redis/RedisConfig.java

@@ -1,51 +0,0 @@
-package com.hrsk.cloud.eg.infrastructure.config.redis;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.data.redis.connection.RedisConnectionFactory;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.serializer.StringRedisSerializer;
-
-
-/**
- * Created zhangyy
- */
-@Configuration
-public class RedisConfig {
-
-    /**
-     * @SuppressWarnings 该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默
-     *
-     *    关键字         用途
-     *    deprecation   使用了不赞成使用的类或方法时的警告
-     *    unchecked     执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。
-     *    fallthrough   当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。
-     *    path          在类路径、源文件路径等中有不存在的路径时的警告。
-     *    serial        当在可序列化的类上缺少 serialVersionUID 定义时的警告。
-     *    finally       任何 finally 子句不能正常完成时的警告。
-     *    all           关于以上所有情况的警告。
-     */
-
-//    注入 Spring 容器中,忽略所有警告
-    @Bean
-    @SuppressWarnings("all")
-    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
-        RedisTemplate<String, Object> template = new RedisTemplate<>();
-        template.setConnectionFactory(factory);
-        FastJson2RedisSerializer<Object> fastJson2RedisSerializer = new FastJson2RedisSerializer<>(Object.class);
-        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
-//        key 采用 String 的序列化方式
-        template.setKeySerializer(stringRedisSerializer);
-//        hash 的 key 也采用 String 的序列化方式
-        template.setHashKeySerializer(stringRedisSerializer);
-//        value 的序列化方式采用 JSON
-        template.setValueSerializer(fastJson2RedisSerializer);
-//        hash value 的序列化方式也采用 JSON
-        template.setHashValueSerializer(fastJson2RedisSerializer);
-        template.afterPropertiesSet();
-        return template;
-    }
-
-}
-
-

+ 0 - 96
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/utils/RedisUtil.java

@@ -1,96 +0,0 @@
-package com.hrsk.cloud.eg.infrastructure.utils;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
-import javax.annotation.Resource;
-import java.util.concurrent.TimeUnit;
-
-/**
- * @author zhangyy
- * @version 1.0
- * @description: RedisUtil
- * @date 2024/8/29 10:19
- */
-@Slf4j
-@Component
-public class RedisUtil {
-    @Resource
-    private RedisTemplate<String, Object> redisTemplate;
-
-    /**
-     * 批量删除对应的value
-     *
-     * @param keys
-     */
-    public void remove(final String... keys) {
-        for (String key : keys) {
-            remove(key);
-        }
-    }
-    /**
-     * 删除对应的value
-     *
-     * @param key
-     */
-    public void remove(final String key) {
-        if (exists(key)) {
-            redisTemplate.delete(key);
-        }
-    }
-
-    /**
-     * 判断缓存中是否有对应的value
-     *
-     * @param key
-     * @return
-     */
-    public boolean exists(final String key) {
-        return redisTemplate.hasKey(key);
-    }
-
-    /**
-     * @description:  原子设置(过期时间必须填写)
-     * @param:  long 过期时间 秒为单位
-     * @return:
-     * @author zhangyy
-     * @date: 2024/8/29 10:35
-     */
-    public Boolean atomSet(final String key, final Object value,final long time) {
-        return redisTemplate.opsForValue().setIfAbsent(key, value,time, TimeUnit.SECONDS);
-    }
-
-    /**
-     * @description: 从redis 获取信息 k-v (v简单对象)
-     * @param:
-     * @return:
-     * @author zhangyy
-     * @date: 2024/8/29 10:38
-     */
-    public Object get(final String key) {
-        if (exists(key)) {
-            return redisTemplate.opsForValue().get(key);
-        }
-        return new Object();
-    }
-
-    /**
-     * @description:
-     * @param: long time 秒
-     * @return:
-     * @author zhangyy
-     * @date: 2024/8/29 10:43
-     */
-    public Boolean set(final String key, final Object value,final long time) {
-         try {
-             redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
-             return true;
-         } catch (Exception e) {
-             log.error("redis set error",e);
-             return false;
-         }
-    }
-
-
-
-
-}

+ 12 - 0
pom.xml

@@ -27,6 +27,8 @@
         <mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version>
         <fast2json.version>2.0.7</fast2json.version>
         <guva.version>33.2.1-jre</guva.version>
+        <jetcache.version>2.7.0</jetcache.version>
+        <jedis.version>4.2.2</jedis.version>
     </properties>
 
     <dependencies>
@@ -142,6 +144,16 @@
                 <artifactId>guava</artifactId>
                 <version>${guva.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.alicp.jetcache</groupId>
+                <artifactId>jetcache-starter-redis</artifactId>
+                <version>${jetcache.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>redis.clients</groupId>
+                <artifactId>jedis</artifactId>
+                <version>${jedis.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

+ 1 - 5
start/src/main/java/com/hrsk/cloud/eg/Application.java

@@ -1,18 +1,14 @@
 package com.hrsk.cloud.eg;
 
 
-import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
-import com.alicp.jetcache.anno.config.EnableMethodCache;
 import lombok.extern.slf4j.Slf4j;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @Slf4j
-@SpringBootApplication(scanBasePackages = {"com.alicp.jetcache.autoconfigure"})
-@EnableCreateCacheAnnotation
+@SpringBootApplication
 @MapperScan("com.hrsk.cloud.eg.infrastructure.repository.database.mapper")
-@EnableMethodCache(basePackages = "com.hrsk.cloud.eg.infrastructure.cache")
 public class Application {
     /**
      * main函数

+ 26 - 12
start/src/main/resources/application-dev.yaml

@@ -1,16 +1,5 @@
 spring:
   application:
-  redis:
-    database: 0
-    host: 192.168.1.169
-    port: 6379
-    password: meloinfo2023!
-    lettuce:
-      pool:
-        max-idle: 300
-        max-active: 600
-        max-wait: 1000s
-    timeout: 5s
   datasource:
     druid:
       type: com.alibaba.druid.pool.DruidDataSource
@@ -35,4 +24,29 @@ spring:
       testOnReturn: false
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
-      logSlowSql: true
+      logSlowSql: true
+#jetcache:
+#  statIntervalMinutes: 15 # 指定统计间隔,以分钟为单位。0表示没有统计数据。
+#  areaInCacheName: true # jetcache-anno使用缓存名称作为远程缓存密钥前缀,在jetcache 2.4.3 和之前的版本中,它总是在缓存名称中添加区域名称,从2.4.4开始我们有这个配置项,为兼容原因,默认值为true。
+#  local:
+#    default:
+#      keyConvertor: fastjson # key 通过 fastjson 转换为 json
+#      type: linkedhashmap
+#      poolConfig:
+#        minIdle: 5 # 连接池中的最小空闲连接数
+#        maxIdle: 20 # 连接池中的最大空闲连接数
+#        maxTotal: 50 # 连接池中的最大连接数
+#  remote:
+#    default:
+#      keyConvertor: fastjson # key 通过 fastjson 转换为 json
+#      valueEncoder: java #全局配置值编码器只需要远程缓存。两个内置valueEncoder是java和kryo
+#      valueDecoder: java #全局配置值解码器只需要远程缓存。两个内置valueEncoder是java和kryo
+#      type: redis # 类型,远程缓存类型有redis和tair
+#      host: 192.168.1.169 # 远程缓存地址
+#      port: 6379 # 远程缓存端口
+#      password: meloinfo2023! # 缓存密码
+#      database: 0
+#      poolConfig:
+#        minIdle: 5 # 连接池中的最小空闲连接数
+#        maxIdle: 20 # 连接池中的最大空闲连接数
+#        maxTotal: 50 # 连接池中的最大连接数

+ 26 - 11
start/src/main/resources/application-pre.yaml

@@ -20,14 +20,29 @@ spring:
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
       logSlowSql: true
-  redis:
-    database: 8
-    host: 47.96.139.31
-    port: 6380
-    password: IICdQIBA#DANB%gkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK8f%
-    lettuce:
-      pool:
-        max-idle: 300
-        max-active: 600
-        max-wait: 1000s
-    timeout: 5s
+
+#jetcache:
+#  statIntervalMinutes: 15 # 指定统计间隔,以分钟为单位。0表示没有统计数据。
+#  areaInCacheName: true # jetcache-anno使用缓存名称作为远程缓存密钥前缀,在jetcache 2.4.3 和之前的版本中,它总是在缓存名称中添加区域名称,从2.4.4开始我们有这个配置项,为兼容原因,默认值为true。
+#  local:
+#    default:
+#      keyConvertor: fastjson # key 通过 fastjson 转换为 json
+#      type: linkedhashmap
+#      poolConfig:
+#        minIdle: 5 # 连接池中的最小空闲连接数
+#        maxIdle: 20 # 连接池中的最大空闲连接数
+#        maxTotal: 50 # 连接池中的最大连接数
+#  remote:
+#    default:
+#      keyConvertor: fastjson # key 通过 fastjson 转换为 json
+#      valueEncoder: java #全局配置值编码器只需要远程缓存。两个内置valueEncoder是java和kryo
+#      valueDecoder: java #全局配置值解码器只需要远程缓存。两个内置valueEncoder是java和kryo
+#      type: redis # 类型,远程缓存类型有redis和tair
+#      host: 47.96.139.31 # 远程缓存地址
+#      port: 6380 # 远程缓存端口
+#      database: 8
+#      password: IICdQIBA#DANB%gkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK8f% # 缓存密码
+#      poolConfig:
+#        minIdle: 5 # 连接池中的最小空闲连接数
+#        maxIdle: 20 # 连接池中的最大空闲连接数
+#        maxTotal: 50 # 连接池中的最大连接数

+ 2 - 2
start/src/test/java/StartTest.java

@@ -1,10 +1,9 @@
 import com.hrsk.cloud.eg.Application;
 import com.hrsk.cloud.eg.constant.BusinessPrefixEnum;
-import com.hrsk.cloud.eg.infrastructure.utils.RedisUtil;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import javax.annotation.Resource;
@@ -19,6 +18,7 @@ import java.util.Map;
  */
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = Application.class)
+@ComponentScan(basePackages={"com.hrsk.cloud.eg.infrastructure.utils"})
 public class StartTest {
 
     @Resource