zjh
2025-04-24 672ce84d5ce6545a0e81709ea736c69cbbef8c1a
ltkj-common/src/main/java/com/ltkj/common/core/redis/RedisCache.java
@@ -8,6 +8,7 @@
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
@@ -25,13 +26,24 @@
    @Autowired
    public RedisTemplate redisTemplate;
    public RedisTemplate setDataBase(int num) {
        LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
        if (connectionFactory != null && num != connectionFactory.getDatabase()) {
            connectionFactory.setDatabase(num);
            this.redisTemplate.setConnectionFactory(connectionFactory);
            connectionFactory.resetConnection();
            connectionFactory.afterPropertiesSet();
        }
        return redisTemplate;
    }
    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key   缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value) {
    public <T> void setCacheObject(final String key, final Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
@@ -88,6 +100,10 @@
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
    public Boolean hasHKey(String key1,String key2) {
        return redisTemplate.opsForHash().hasKey(key1,key2);
    }
    /**
@@ -195,12 +211,15 @@
     *
     * @param key   Redis键
     * @param hKey  Hash键
     * @param value 值
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
    public <T> void setCacheMapValue(final String key, final String hKey, final Object value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }
    public <T> void setHashKeyExpireTime(final String key, final Long time, final TimeUnit  unit) {
        redisTemplate.expire(key,time,unit);
    }
    /**
     * 获取Hash中的数据
     *