common工程增加Redis工具

This commit is contained in:
jiangh277
2025-12-24 14:14:59 +08:00
parent aa42f35254
commit 3eb445291f
3 changed files with 134 additions and 2 deletions

View File

@@ -20,6 +20,7 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@@ -50,9 +51,27 @@
<version>1.2.83</version> <version>1.2.83</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- Redis 工具支撑 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
@@ -75,4 +94,4 @@
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -1,5 +1,10 @@
package com.timeline.common.constants; package com.timeline.common.constants;
/**
* 通用常量类,定义系统中使用的各种常量值
* 包括关系类型、删除状态、用户状态、故事权限、好友状态等常量
* @author adm
*/
public class CommonConstants { public class CommonConstants {
private static final int RELATION_USER_AND_STORY = 1; private static final int RELATION_USER_AND_STORY = 1;
public static final int RELATION_USER_AND_STORY_ITEM = 2; public static final int RELATION_USER_AND_STORY_ITEM = 2;
@@ -15,7 +20,48 @@ public class CommonConstants {
public static final int NOT_DELETED = 0; public static final int NOT_DELETED = 0;
public static final String LOW_RESOLUTION_PREFIX = "low_res_"; public static final String LOW_RESOLUTION_PREFIX = "low_res_";
// 用户状态 /**
* 正常用户状态
*/
public static final int USER_STATUS_NORMAL = 0; public static final int USER_STATUS_NORMAL = 0;
/**
* 禁用用户状态
*/
public static final int USER_STATUS_DISABLED = 1; public static final int USER_STATUS_DISABLED = 1;
// 故事权限
/**
* 所有者权限
*/
public static final int STORY_PERMISSION_TYPE_OWNER = 1;
/**
* 仅查看权限
*/
public static final int STORY_PERMISSION_TYPE_READ = 2;
/**
* 可编辑权限
*/
public static final int STORY_PERMISSION_TYPE_WRITE = 3;
/**
* 管理权限
*/
public static final int STORY_PERMISSION_TYPE_ADMIN = 4;
/**
* 好友状态
*/
public static final String FRIENDSHIP_PENDING = "pending";
/**
* 已接收
*/
public static final String FRIENDSHIP_ACCEPTED = "accepted";
/**
* 已拒绝
*/
public static final String FRIENDSHIP_REJECTED = "rejected";
/**
* 默认sortKey
*/
public static final Integer DEFAULT_SORT_KEY = 999;
} }

View File

@@ -0,0 +1,67 @@
package com.timeline.common.utils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* Redis 公共工具:基于 StringRedisTemplate 的常用操作封装。
*/
@Component
public class RedisUtils {
private final StringRedisTemplate stringRedisTemplate;
public RedisUtils(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
/** 设置值(无过期时间) */
public void set(@NonNull String key, @NonNull String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
/** 设置值并指定过期(毫秒) */
public void set(@NonNull String key, @NonNull String value, long timeoutMs) {
stringRedisTemplate.opsForValue().set(key, value, timeoutMs, TimeUnit.MILLISECONDS);
}
/** 设置值并指定过期Duration */
public void set(@NonNull String key, @NonNull String value, @NonNull Duration ttl) {
stringRedisTemplate.opsForValue().set(key, value, ttl);
}
/** 获取值 */
public String get(@NonNull String key) {
return stringRedisTemplate.opsForValue().get(key);
}
/** 删除键 */
public Boolean del(@NonNull String key) {
return stringRedisTemplate.delete(key);
}
/** 自增 */
public Long incr(@NonNull String key) {
return stringRedisTemplate.opsForValue().increment(key);
}
/** 自减 */
public Long decr(@NonNull String key) {
return stringRedisTemplate.opsForValue().decrement(key);
}
/** 设置过期(秒) */
public Boolean expire(@NonNull String key, long seconds) {
return stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
/** 判断键是否存在 */
public Boolean hasKey(@NonNull String key) {
return stringRedisTemplate.hasKey(key);
}
}