Files
peipei-backend/play-common/src/main/java/com/starry/common/redis/RedisCache.java
irving d719a047d8 style: 应用 Spotless 代码格式化
- 对所有 Java 源文件应用统一的代码格式化
- 统一缩进为 4 个空格
- 清理尾随空白字符和文件末尾换行
- 优化导入语句组织
- 总计格式化 654 个 Java 文件

有问题可以回滚或者找我聊
2025-08-30 21:21:08 -04:00

97 lines
2.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.starry.common.redis;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
/**
* @author rieds 工具类
* @since 2022/8/26
*/
@Component
public class RedisCache {
@Resource
public RedisTemplate<String, Object> redisTemplate;
/**
* 获得缓存的基本对象。
*
* @param key
* 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
try {
return (T) operations.get(key);
} catch (QueryTimeoutException e) {
throw new RuntimeException("系统错误,通讯异常");
}
}
/**
* 缓存基本的对象Integer、String、实体类等
*
* @param key
* 缓存的键值
* @param value
* 缓存的值
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象Integer、String、实体类等
*
* @param key
* 缓存的键值
* @param value
* 缓存的值
* @param timeout
* 过期时间
* @param timeUnit
* 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 删除单个对象
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
* 获得缓存的基本对象列表
*
* @param pattern
* 字符串前缀
* @return 对象列表
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 删除集合对象
*
* @param collection
* 多个对象
* @return
*/
public long deleteObject(final Collection collection) {
return redisTemplate.delete(collection);
}
}