diff --git a/orion-ops-dependencies/pom.xml b/orion-ops-dependencies/pom.xml index 96cee127..ada92004 100644 --- a/orion-ops-dependencies/pom.xml +++ b/orion-ops-dependencies/pom.xml @@ -25,6 +25,7 @@ 3.5.3.1 2.3 1.2.16 + 3.18.0 @@ -91,6 +92,11 @@ orion-ops-spring-boot-starter-websocket ${revision} + + com.orion.ops + orion-ops-spring-boot-starter-redis + ${revision} + @@ -161,6 +167,19 @@ ${velocity.version} + + + org.redisson + redisson-spring-boot-starter + ${redisson.version} + + + org.springframework.boot + spring-boot-starter-actuator + + + + diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/pom.xml b/orion-ops-framework/orion-ops-spring-boot-starter-redis/pom.xml new file mode 100644 index 00000000..79d371c8 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/pom.xml @@ -0,0 +1,41 @@ + + + + com.orion.ops + orion-ops-framework + ${revision} + + + 4.0.0 + orion-ops-spring-boot-starter-redis + ${project.artifactId} + jar + + 项目 redis 配置包 + https://github.com/lijiahangmax/orion-ops-pro + + + + com.orion.ops + orion-ops-common + + + + org.redisson + redisson-spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-cache + + + + io.netty + netty-all + + + + \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/config/OrionRedisAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/config/OrionRedisAutoConfiguration.java new file mode 100644 index 00000000..95774be8 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/config/OrionRedisAutoConfiguration.java @@ -0,0 +1,73 @@ +package com.orion.ops.framework.redis.config; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.cache.CacheProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * 缓存配置类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/28 14:44 + */ +@AutoConfiguration +@EnableCaching +@EnableConfigurationProperties({CacheProperties.class}) +public class OrionRedisAutoConfiguration { + + /** + * @param redisConnectionFactory factory + * @param T + * @return RedisTemplate + */ + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + redisTemplate.setKeySerializer(RedisSerializer.string()); + redisTemplate.setValueSerializer(RedisSerializer.json()); + redisTemplate.setHashKeySerializer(RedisSerializer.string()); + redisTemplate.setHashValueSerializer(RedisSerializer.json()); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + + /** + * @param cacheProperties properties + * @return 缓存配置 + */ + @Bean + @Primary + public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) { + // 设置使用 JSON 序列化方式 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); + config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json())); + // 设置 CacheProperties.Redis 的属性 + CacheProperties.Redis redisProperties = cacheProperties.getRedis(); + if (redisProperties.getTimeToLive() != null) { + config = config.entryTtl(redisProperties.getTimeToLive()); + } + if (redisProperties.getKeyPrefix() != null) { + config = config.prefixCacheNameWith(redisProperties.getKeyPrefix()); + } + if (!redisProperties.isCacheNullValues()) { + config = config.disableCachingNullValues(); + } + if (!redisProperties.isUseKeyPrefix()) { + config = config.disableKeyPrefix(); + } + return config; + } + + +} + diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/utils/RedisUtils.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/utils/RedisUtils.java new file mode 100644 index 00000000..5167d6be --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/utils/RedisUtils.java @@ -0,0 +1,58 @@ +package com.orion.ops.framework.redis.utils; + +import com.orion.lang.utils.io.Streams; +import org.springframework.data.redis.core.Cursor; +import org.springframework.data.redis.core.RedisCallback; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ScanOptions; + +import java.util.HashSet; +import java.util.Set; + +/** + * redis 工具类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2021/11/6 11:09 + */ +public class RedisUtils { + + private RedisUtils() { + } + + /** + * 扫描key + * + * @param redisTemplate redisTemplate + * @param match 匹配值 + * @param count 数量 + * @return keys + */ + public static Set scanKeys(RedisTemplate redisTemplate, String match, int count) { + return scanKeys(redisTemplate, ScanOptions.scanOptions() + .match(match) + .count(count) + .build()); + } + + /** + * 扫描key + * + * @param redisTemplate redisTemplate + * @param scanOptions scan + * @return keys + */ + public static Set scanKeys(RedisTemplate redisTemplate, ScanOptions scanOptions) { + return redisTemplate.execute((RedisCallback>) connection -> { + Set keys = new HashSet<>(); + Cursor cursor = connection.scan(scanOptions); + while (cursor.hasNext()) { + keys.add(new String(cursor.next())); + } + Streams.close(cursor); + return keys; + }); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..f837db78 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.orion.ops.framework.redis.config.OrionRedisAutoConfiguration \ No newline at end of file diff --git a/orion-ops-framework/pom.xml b/orion-ops-framework/pom.xml index 7b9a53f9..5f22f1c4 100644 --- a/orion-ops-framework/pom.xml +++ b/orion-ops-framework/pom.xml @@ -24,6 +24,7 @@ orion-ops-spring-boot-starter-mybatis orion-ops-spring-boot-starter-job orion-ops-spring-boot-starter-websocket + orion-ops-spring-boot-starter-redis \ No newline at end of file diff --git a/orion-ops-launch/pom.xml b/orion-ops-launch/pom.xml index b294efb3..a2db5d83 100644 --- a/orion-ops-launch/pom.xml +++ b/orion-ops-launch/pom.xml @@ -52,6 +52,10 @@ com.orion.ops orion-ops-spring-boot-starter-websocket + + com.orion.ops + orion-ops-spring-boot-starter-redis + diff --git a/orion-ops-launch/src/main/resources/application-dev.yaml b/orion-ops-launch/src/main/resources/application-dev.yaml index 431b52b8..d56199ec 100644 --- a/orion-ops-launch/src/main/resources/application-dev.yaml +++ b/orion-ops-launch/src/main/resources/application-dev.yaml @@ -7,6 +7,12 @@ spring: initial-size: 0 min-idle: 1 max-active: 5 + redis: + host: 127.0.0.1 + port: 6379 + database: 0 + password: lijiahang + timeout: 3000 mybatis-plus: configuration: diff --git a/orion-ops-launch/src/main/resources/application-prod.yaml b/orion-ops-launch/src/main/resources/application-prod.yaml index 20249af4..2394437f 100644 --- a/orion-ops-launch/src/main/resources/application-prod.yaml +++ b/orion-ops-launch/src/main/resources/application-prod.yaml @@ -17,6 +17,12 @@ spring: filter: stat: enabled: true + redis: + host: 127.0.0.1 + port: 6379 + database: 0 + password: lijiahang + timeout: 3000 springdoc: api-docs: diff --git a/orion-ops-launch/src/main/resources/application.yaml b/orion-ops-launch/src/main/resources/application.yaml index fa60aad9..2dbf30ee 100644 --- a/orion-ops-launch/src/main/resources/application.yaml +++ b/orion-ops-launch/src/main/resources/application.yaml @@ -60,6 +60,10 @@ spring: wall: config: multi-statement-allow: true + cache: + type: REDIS + redis: + time-to-live: 1h mybatis-plus: configuration: