common工程增加Redis工具
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -50,9 +51,27 @@
|
||||
<version>1.2.83</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Redis 工具支撑 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
@@ -75,4 +94,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.timeline.common.constants;
|
||||
|
||||
/**
|
||||
* 通用常量类,定义系统中使用的各种常量值
|
||||
* 包括关系类型、删除状态、用户状态、故事权限、好友状态等常量
|
||||
* @author adm
|
||||
*/
|
||||
public class CommonConstants {
|
||||
private static final int RELATION_USER_AND_STORY = 1;
|
||||
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 String LOW_RESOLUTION_PREFIX = "low_res_";
|
||||
|
||||
// 用户状态
|
||||
/**
|
||||
* 正常用户状态
|
||||
*/
|
||||
public static final int USER_STATUS_NORMAL = 0;
|
||||
/**
|
||||
* 禁用用户状态
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user