Implementation:A616567126 GPT WEB JAVA RedisUtil
| Knowledge Sources | |
|---|---|
| Domains | Caching, Redis |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete Redis cache utility providing static methods for object, list, set, and hash operations with FastJSON serialization, used throughout the application for caching system config, tokens, and session data.
Description
The RedisUtil class wraps Spring's `RedisTemplate` with static convenience methods. It initializes key/value/hash serializers in `@PostConstruct` (GenericToStringSerializer for keys, GenericFastJsonRedisSerializer for values). It provides operations for: setting/getting objects with optional TTL, deleting keys, expiring keys, caching lists/sets/maps, hash operations, key pattern matching, atomic increment/decrement, and `setIfAbsent` for distributed locking.
Usage
Call the static methods directly (e.g., `RedisUtil.setCacheObject(key, value)`) from anywhere in the application. The utility self-initializes via `@PostConstruct`.
Code Reference
Source Location
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/utils/sys/RedisUtil.java
- Lines: 1-294
Signature
@Component
public class RedisUtil {
// Object operations
public static <T> void setCacheObject(final String key, final T value);
public static <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit);
public static <T> T getCacheObject(final String key);
public static boolean deleteObject(final String key);
// List operations
public <T> long setCacheList(final String key, final List<T> dataList);
public <T> List<T> getCacheList(final String key);
// Set operations
public static <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet);
public static <T> Set<T> getCacheSet(final String key);
// Map/Hash operations
public static <T> void setCacheMap(final String key, final Map<String, T> dataMap);
public static <T> Map<String, T> getCacheMap(final String key);
public static <T> void setCacheMapValue(final String key, final String hKey, final T value);
public static <T> T getCacheMapValue(final String key, final String hKey);
// Key operations
public static Set<String> getKeys(final String pattern);
public boolean expire(final String key, final long timeout, final TimeUnit unit);
// Atomic operations
public Long incr(final String key);
public boolean setIfAbsent(String var1, int var2, long var3, TimeUnit var5);
public boolean exists(String key);
}
Import
import com.intelligent.bot.utils.sys.RedisUtil;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | String | Yes | Redis cache key |
| value | T | Yes (set) │ Value to cache (any serializable type) | |
| timeout | Long | No │ TTL duration | |
| timeUnit | TimeUnit | No │ Time unit for TTL |
Outputs
| Name | Type | Description |
|---|---|---|
| getCacheObject | T | Cached value or null |
| getCacheList | List<T> | Cached list or empty |
| getCacheSet | Set<T> │ Cached set | |
| getCacheMap | Map<String, T> │ Cached map | |
| getKeys | Set<String> | Matching key patterns |
| incr | Long | Incremented value |
Usage Examples
Cache System Config
import com.intelligent.bot.utils.sys.RedisUtil;
// Cache system config with no TTL
RedisUtil.setCacheObject(CommonConst.SYS_CONFIG, sysConfig);
// Retrieve it
SysConfig config = RedisUtil.getCacheObject(CommonConst.SYS_CONFIG);
Cache with TTL
// Cache Baidu access token for 29 days
RedisUtil.setCacheObject(
CommonConst.BAIDU_TO_EXAMINE_REDIS_KEY,
accessToken,
29L,
TimeUnit.DAYS
);
Hash Operations
// Store hash entry
RedisUtil.setCacheMapValue("user:123", "name", "Alice");
RedisUtil.setCacheMapValue("user:123", "age", 25);
// Retrieve hash entry
String name = RedisUtil.getCacheMapValue("user:123", "name");
Key Pattern Matching
// Find all keys matching pattern
Set<String> tokens = RedisUtil.getKeys("TOKEN:*");