feat: 添加 spring-boot-redis starter.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
<mybatis-plus-generator.version>3.5.3.1</mybatis-plus-generator.version>
|
||||
<velocity.version>2.3</velocity.version>
|
||||
<druid.version>1.2.16</druid.version>
|
||||
<redisson.version>3.18.0</redisson.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -91,6 +92,11 @@
|
||||
<artifactId>orion-ops-spring-boot-starter-websocket</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-redis</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- websocket -->
|
||||
<dependency>
|
||||
@@ -161,6 +167,19 @@
|
||||
<version>${velocity.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- redis -->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
<version>${redisson.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>orion-ops-spring-boot-starter-redis</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>项目 redis 配置包</description>
|
||||
<url>https://github.com/lijiahangmax/orion-ops-pro</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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> T
|
||||
* @return RedisTemplate
|
||||
*/
|
||||
@Bean
|
||||
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, T> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String> 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<String> scanKeys(RedisTemplate<?, ?> redisTemplate, ScanOptions scanOptions) {
|
||||
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
|
||||
Set<String> keys = new HashSet<>();
|
||||
Cursor<byte[]> cursor = connection.scan(scanOptions);
|
||||
while (cursor.hasNext()) {
|
||||
keys.add(new String(cursor.next()));
|
||||
}
|
||||
Streams.close(cursor);
|
||||
return keys;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.orion.ops.framework.redis.config.OrionRedisAutoConfiguration
|
||||
@@ -24,6 +24,7 @@
|
||||
<module>orion-ops-spring-boot-starter-mybatis</module>
|
||||
<module>orion-ops-spring-boot-starter-job</module>
|
||||
<module>orion-ops-spring-boot-starter-websocket</module>
|
||||
<module>orion-ops-spring-boot-starter-redis</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -52,6 +52,10 @@
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- orion-ops biz-modules -->
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -60,6 +60,10 @@ spring:
|
||||
wall:
|
||||
config:
|
||||
multi-statement-allow: true
|
||||
cache:
|
||||
type: REDIS
|
||||
redis:
|
||||
time-to-live: 1h
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
|
||||
Reference in New Issue
Block a user