feat(common): 添加 RedisKeyManager/RedisService/RedisConfig (T009)
This commit is contained in:
24
src/main/java/com/label/common/config/RedisConfig.java
Normal file
24
src/main/java/com/label/common/config/RedisConfig.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package com.label.common.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
|
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
StringRedisSerializer serializer = new StringRedisSerializer();
|
||||||
|
template.setKeySerializer(serializer);
|
||||||
|
template.setValueSerializer(serializer);
|
||||||
|
template.setHashKeySerializer(serializer);
|
||||||
|
template.setHashValueSerializer(serializer);
|
||||||
|
template.afterPropertiesSet();
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/com/label/common/redis/RedisKeyManager.java
Normal file
25
src/main/java/com/label/common/redis/RedisKeyManager.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package com.label.common.redis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Centralized Redis key naming conventions.
|
||||||
|
* All keys follow the pattern: prefix:{id}
|
||||||
|
*/
|
||||||
|
public final class RedisKeyManager {
|
||||||
|
|
||||||
|
private RedisKeyManager() {}
|
||||||
|
|
||||||
|
/** Session token key: token:{uuid} */
|
||||||
|
public static String tokenKey(String uuid) {
|
||||||
|
return "token:" + uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** User permission cache key: user:perm:{userId} */
|
||||||
|
public static String userPermKey(Long userId) {
|
||||||
|
return "user:perm:" + userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Task claim distributed lock key: task:claim:{taskId} */
|
||||||
|
public static String taskClaimKey(Long taskId) {
|
||||||
|
return "task:claim:" + taskId;
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/main/java/com/label/common/redis/RedisService.java
Normal file
61
src/main/java/com/label/common/redis/RedisService.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package com.label.common.redis;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RedisService {
|
||||||
|
|
||||||
|
private final RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
// String operations
|
||||||
|
|
||||||
|
public void set(String key, String value, long ttlSeconds) {
|
||||||
|
redisTemplate.opsForValue().set(key, value, ttlSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(String key) {
|
||||||
|
return redisTemplate.opsForValue().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(String key) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists(String key) {
|
||||||
|
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set if absent (NX). Returns true if key was set (lock acquired). */
|
||||||
|
public boolean setIfAbsent(String key, String value, long ttlSeconds) {
|
||||||
|
Boolean result = redisTemplate.opsForValue()
|
||||||
|
.setIfAbsent(key, value, ttlSeconds, TimeUnit.SECONDS);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Refresh TTL on an existing key (sliding expiration). */
|
||||||
|
public void expire(String key, long ttlSeconds) {
|
||||||
|
redisTemplate.expire(key, ttlSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash operations (for token storage: token:{uuid} → Hash)
|
||||||
|
|
||||||
|
public void hSetAll(String key, Map<String, String> entries, long ttlSeconds) {
|
||||||
|
redisTemplate.opsForHash().putAll(key, entries);
|
||||||
|
redisTemplate.expire(key, ttlSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Object, Object> hGetAll(String key) {
|
||||||
|
return redisTemplate.opsForHash().entries(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String hGet(String key, String field) {
|
||||||
|
Object val = redisTemplate.opsForHash().get(key, field);
|
||||||
|
return val != null ? val.toString() : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user