wujianwei
2026-01-05 23bd50fb3957688de0c12abc73b6ca939b1d1f7b
common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
@@ -6,7 +6,10 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
@@ -23,16 +26,21 @@
public class RedisCache
{
    @Autowired
    public RedisTemplate redisTemplate;
    private RedisTemplate redisTemplate;
    @Value("${spring.redis.keyprefix}")
    private String keyPrefix;
    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value)
    public <T> void setCacheObject( String key, final T value)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        redisTemplate.opsForValue().set(key, value);
    }
@@ -44,8 +52,11 @@
     * @param timeout 时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    public <T> void setCacheObject( String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }
@@ -56,8 +67,11 @@
     * @param timeout 超时时间
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout)
    public boolean expire( String key, final long timeout)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return expire(key, timeout, TimeUnit.SECONDS);
    }
@@ -69,31 +83,12 @@
     * @param unit 时间单位
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit)
    public boolean expire( String key, final long timeout, final TimeUnit unit)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.expire(key, timeout, unit);
    }
    /**
     * 获取有效时间
     *
     * @param key Redis键
     * @return 有效时间
     */
    public long getExpire(final String key)
    {
        return redisTemplate.getExpire(key);
    }
    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key)
    {
        return redisTemplate.hasKey(key);
    }
    /**
@@ -102,8 +97,11 @@
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public <T> T getCacheObject(final String key)
    public <T> T getCacheObject(String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }
@@ -113,21 +111,37 @@
     *
     * @param key
     */
    public boolean deleteObject(final String key)
    public boolean deleteObject( String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.delete(key);
    }
    /**
     * 删除集合对象
     *
     * @param collection 多个对象
     * @return
     */
    public boolean deleteObject(final Collection collection)
    public long deleteObject(final Collection collection)
    {
        return redisTemplate.delete(collection) > 0;
        return redisTemplate.delete(collection);
    }
    /**
     * 删除单个对象
     *
     * @param key
     */
    public Long deleteMapValue( String key,String hashKey)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.opsForHash().delete(key,hashKey);
    }
    /**
     * 缓存List数据
@@ -136,8 +150,11 @@
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    public <T> long setCacheList(final String key, final List<T> dataList)
    public <T> long setCacheList( String key, final List<T> dataList)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }
@@ -148,8 +165,11 @@
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     */
    public <T> List<T> getCacheList(final String key)
    public <T> List<T> getCacheList( String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.opsForList().range(key, 0, -1);
    }
@@ -160,8 +180,11 @@
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     */
    public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
    public <T> BoundSetOperations<String, T> setCacheSet( String key, final Set<T> dataSet)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext())
@@ -177,8 +200,11 @@
     * @param key
     * @return
     */
    public <T> Set<T> getCacheSet(final String key)
    public <T> Set<T> getCacheSet( String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.opsForSet().members(key);
    }
@@ -188,8 +214,11 @@
     * @param key
     * @param dataMap
     */
    public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
    public <T> void setCacheMap( String key, final Map<String, T> dataMap)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
@@ -201,8 +230,11 @@
     * @param key
     * @return
     */
    public <T> Map<String, T> getCacheMap(final String key)
    public <T> Map<String, T> getCacheMap( String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.opsForHash().entries(key);
    }
@@ -213,8 +245,11 @@
     * @param hKey Hash键
     * @param value 值
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value)
    public <T> void setCacheMapValue( String key, final String hKey, final T value)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        redisTemplate.opsForHash().put(key, hKey, value);
    }
@@ -225,8 +260,11 @@
     * @param hKey Hash键
     * @return Hash中的对象
     */
    public <T> T getCacheMapValue(final String key, final String hKey)
    public <T> T getCacheMapValue( String key, final String hKey)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }
@@ -238,8 +276,11 @@
     * @param hKeys Hash键集合
     * @return Hash对象集合
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
    public <T> List<T> getMultiCacheMapValue( String key, final Collection<Object> hKeys)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }
@@ -265,4 +306,18 @@
    {
        return redisTemplate.keys(pattern);
    }
    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key)
    {
        if(StringUtils.isNotNull(keyPrefix)){
            key = keyPrefix + ":"+key;
        }
        return redisTemplate.hasKey(key);
    }
}