rename com.imyeyu.server to com.imyeyu.api
This commit is contained in:
@@ -0,0 +1,268 @@
|
||||
package com.imyeyu.api.config;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import com.imyeyu.api.modules.blog.entity.ArticleRanking;
|
||||
import com.imyeyu.api.modules.common.entity.Multilingual;
|
||||
import com.imyeyu.spring.bean.RedisConfigParams;
|
||||
import com.imyeyu.spring.config.AbstractRedisConfig;
|
||||
import com.imyeyu.spring.util.Redis;
|
||||
import com.imyeyu.spring.util.RedisSerializers;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.serializer.support.DeserializingConverter;
|
||||
import org.springframework.core.serializer.support.SerializingConverter;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* Redis 配置
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-02-23 21:36
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@EnableAutoConfiguration
|
||||
@ConfigurationProperties(prefix = "spring.redis")
|
||||
public class RedisConfig extends AbstractRedisConfig {
|
||||
|
||||
// ---------- 连接配置 ----------
|
||||
|
||||
/** 地址 */
|
||||
private String host;
|
||||
|
||||
/** 端口 */
|
||||
private int port;
|
||||
|
||||
/** 密码 */
|
||||
private String password;
|
||||
|
||||
/** 超时(毫秒) */
|
||||
private int timeout;
|
||||
|
||||
/** 连接池 */
|
||||
private Lettuce lettuce;
|
||||
|
||||
/** 数据库 */
|
||||
private Database database;
|
||||
|
||||
/**
|
||||
* 连接池
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-08-21 16:23
|
||||
*/
|
||||
@Data
|
||||
public static class Lettuce {
|
||||
|
||||
/** 配置 */
|
||||
private Pool pool;
|
||||
|
||||
/**
|
||||
* 配置
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-08-21 16:23
|
||||
*/
|
||||
@Data
|
||||
public static class Pool {
|
||||
|
||||
/** 最大活跃连接 */
|
||||
private int maxActive;
|
||||
|
||||
/** 最小空闲连接 */
|
||||
private int minIdle;
|
||||
|
||||
/** 最大空闲连接 */
|
||||
private int maxIdle;
|
||||
|
||||
/** 最大等待时间(秒) */
|
||||
private int maxWait;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-08-21 16:25
|
||||
*/
|
||||
@Data
|
||||
public static class Database {
|
||||
|
||||
/** 分布式锁 */
|
||||
private int locker;
|
||||
|
||||
/** 多语言环境 */
|
||||
private int language;
|
||||
|
||||
/** 多语言键环境 */
|
||||
private int languageMap;
|
||||
|
||||
/** 文章排位 */
|
||||
private int articleRanking;
|
||||
|
||||
/** 文章阅读记录 */
|
||||
private int articleRead;
|
||||
|
||||
/** 用户登录令牌 */
|
||||
private int userToken;
|
||||
|
||||
/** 用户经验值标记 */
|
||||
private int userExpFlag;
|
||||
|
||||
/** 用户邮箱验证 */
|
||||
private int userEmailVerify;
|
||||
|
||||
/** 用户重置密码验证 */
|
||||
private int userResetPWVerify;
|
||||
|
||||
/** 访问频率控制 */
|
||||
private int rateLimit;
|
||||
|
||||
/** 系统配置 */
|
||||
private int setting;
|
||||
|
||||
/** Minecraft 登录 */
|
||||
private int fmcPlayerToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisConfigParams configParams() {
|
||||
return new RedisConfigParams() {{
|
||||
setHost(host);
|
||||
setPort(port);
|
||||
setPassword(password);
|
||||
setTimeout(timeout);
|
||||
setMaxActive(lettuce.pool.maxActive);
|
||||
setMinIdle(lettuce.pool.minIdle);
|
||||
setMaxIdle(lettuce.pool.maxIdle);
|
||||
}};
|
||||
}
|
||||
|
||||
/** @return 连接池配置 */
|
||||
@Bean
|
||||
@Override
|
||||
public GenericObjectPoolConfig<?> getPoolConfig() {
|
||||
GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
|
||||
config.setMaxTotal(lettuce.pool.maxActive);
|
||||
config.setMinIdle(lettuce.pool.minIdle);
|
||||
config.setMaxIdle(lettuce.pool.maxIdle);
|
||||
config.setMaxWait(Duration.ofMillis(lettuce.pool.maxWait));
|
||||
return config;
|
||||
}
|
||||
|
||||
/** @return key 生成策略 */
|
||||
@Bean
|
||||
@Override
|
||||
public KeyGenerator keyGenerator() {
|
||||
return (target, method, params) -> {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(target.getClass().getName());
|
||||
sb.append(method.getName());
|
||||
for (Object obj : params) {
|
||||
sb.append(obj.toString());
|
||||
}
|
||||
return sb.toString();
|
||||
};
|
||||
}
|
||||
|
||||
/** @return 分布式锁, ID: 尝试加锁次数 */
|
||||
@Bean("redisLocker")
|
||||
public Redis<String, Integer> getLockerRedisTemplate() {
|
||||
return getRedis(database.locker, RedisSerializers.STRING, RedisSerializers.INTEGER);
|
||||
}
|
||||
|
||||
/** @return 多语言环境,ID: {@link Multilingual} */
|
||||
@Bean("redisLanguage")
|
||||
public Redis<Long, Multilingual> getLanguageRedisTemplate() {
|
||||
return getRedis(database.language, RedisSerializers.LONG, new RedisSerializer<>() {
|
||||
|
||||
public Multilingual deserialize(@Nullable byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return (Multilingual) new DeserializingConverter().convert(bytes);
|
||||
} catch (Exception var3) {
|
||||
throw new SerializationException("Cannot deserialize", var3);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] serialize(@Nullable Multilingual multilingual) {
|
||||
if (multilingual == null) {
|
||||
return new byte[0];
|
||||
} else {
|
||||
try {
|
||||
return new SerializingConverter().convert(multilingual);
|
||||
} catch (Exception var3) {
|
||||
throw new SerializationException("Cannot serialize", var3);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @return 文章访问记录,IP: [文章 ID] */
|
||||
@Bean("redisLanguageMap")
|
||||
public Redis<String, Long> getLanguageMapRedisTemplate() {
|
||||
return getRedis(database.languageMap, RedisSerializers.STRING, RedisSerializers.LONG);
|
||||
}
|
||||
|
||||
/** @return 文章访问统计,文章 ID: {@link ArticleRanking}(JSON) */
|
||||
@Bean("redisArticleRanking")
|
||||
public Redis<Long, ArticleRanking> getArticleRankingRedisTemplate() {
|
||||
return getRedis(database.articleRanking, RedisSerializers.LONG, RedisSerializers.gsonSerializer(ArticleRanking.class));
|
||||
}
|
||||
|
||||
/** @return 文章访问记录,IP: [文章 ID] */
|
||||
@Bean("redisArticleRead")
|
||||
public Redis<String, Long> getArticleReadRedisTemplate() {
|
||||
return getRedis(database.articleRead, RedisSerializers.STRING, RedisSerializers.LONG);
|
||||
}
|
||||
|
||||
/** @return 用户登录经验标记,UID: NULL,暂时没有值,数据死亡时间为次日零时 */
|
||||
@Bean("redisUserExpFlag")
|
||||
public Redis<Long, String> getUserExpFlagRedisTemplate() {
|
||||
return getRedis(database.userExpFlag, RedisSerializers.LONG, RedisSerializers.STRING);
|
||||
}
|
||||
|
||||
/** @return 用户邮箱验证密钥,密钥: UID */
|
||||
@Bean("redisUserEmailVerify")
|
||||
public Redis<String, Long> getUserEmailVerifyRedisTemplate() {
|
||||
return getRedis(database.userEmailVerify, RedisSerializers.STRING, RedisSerializers.LONG);
|
||||
}
|
||||
|
||||
/** @return 用户重置密码密钥,密钥: UID */
|
||||
@Bean("redisUserResetPWVerify")
|
||||
public Redis<String, Long> getUserResetPWVerifyRedisTemplate() {
|
||||
return getRedis(database.userResetPWVerify, RedisSerializers.STRING, RedisSerializers.LONG);
|
||||
}
|
||||
|
||||
/** @return 接口访问控制,IP#方法: 生命周期内访问次数 */
|
||||
@Bean("redisRateLimit")
|
||||
public Redis<String, Integer> getRateLimitRedisTemplate() {
|
||||
return getRedis(database.rateLimit, RedisSerializers.STRING, RedisSerializers.INTEGER);
|
||||
}
|
||||
|
||||
/** @return 系统配置,Key 枚举: String 配置值 */
|
||||
@Bean("redisSetting")
|
||||
public Redis<String, String> getSettingRedisTemplate() {
|
||||
return getRedis(database.setting, RedisSerializers.STRING, RedisSerializers.STRING);
|
||||
}
|
||||
|
||||
/** @return Minecraft 登录,令牌: 玩家 ID */
|
||||
@Bean("redisMCPlayerToken")
|
||||
public Redis<String, Long> getMCPlayerLoginRedisTemplate() {
|
||||
return getRedis(database.fmcPlayerToken, RedisSerializers.STRING, RedisSerializers.LONG);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user