redis工具类

本文最后更新于1 分钟前,文中所描述的信息可能已发生改变。
java
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 不与业务打交道的redis相关接口
 */
@Service
@Slf4j
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    /**************************    Common     ***********************************/
    /**
     * 过期
     *
     * @param key
     * @param timeout
     * @return
     */
    public Boolean expire(String key, long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 根据key 获取过期时间
     *
     * @param key 键 不能为null
     * @return 时间(秒) 返回0代表为永久有效
     */
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 使用过期时间替代delete命令
     *
     * @param key
     */
    public void delete(String key) {
        expire(key, 0);
    }

    /**
     * 使用过期时间替代delete命令
     *
     * @param keys 可以为null
     * @return
     */
    public void delete(Collection<String> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            return;
        }
        for (String key : keys) {
            delete(key);
        }
    }
    /**************************  String  key-Value     ***********************************/
    /**
     * @param key        key
     * @param value      value
     * @param expiration 过期时间
     */
    public void setValue(String key, Serializable value, long expiration) {
        redisTemplate.opsForValue().set(key, value, expiration, TimeUnit.SECONDS);
    }

    public void setValue(String key, Serializable value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object getValue(String key) {
        Object value = redisTemplate.opsForValue().get(key);
        return value;
    }
    /**************************    hash (Map)    ***********************************/
    /**
     * HashGet
     *
     * @param key  键 不能为null
     * @param item 项 不能为null
     * @return
     */
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    /**
     * HashSet
     *
     * @param key
     * @param map 对应多个键值
     * @return true 成功 false 失败
     */
    public void hmset(String key, Map<String, Serializable> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * HashSet 并设置时间
     *
     * @param key
     * @param map        对应多个键值
     * @param expiration 时间(秒)
     * @return true成功 false失败
     */
    public void hmset(String key, Map<String, Serializable> map, long expiration) {
        redisTemplate.opsForHash().putAll(key, map);
        if (expiration > 0) {
            expire(key, expiration);
        }
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key
     * @param item
     * @param value
     * @return true 成功 false失败
     */
    public void hset(String key, String item, Serializable value) {
        redisTemplate.opsForHash().put(key, item, value);
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key
     * @param item
     * @param value
     * @param expiration 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
     * @return true 成功 false失败
     */
    public void hset(String key, String item, Serializable value, long expiration) {
        redisTemplate.opsForHash().put(key, item, value);
        if (expiration >= 0) {
            expire(key, expiration);
        }
    }

    /**
     * 删除hash表中的值
     *
     * @param key  键 不能为null
     * @param item 项 可以使多个 不能为null
     */
    @Deprecated
    public void hdel(String key, Serializable... item) {
        redisTemplate.opsForHash().delete(key, item);
    }

    /**
     * 判断hash表中是否有该项的值
     *
     * @param key  键 不能为null
     * @param item 项 不能为null
     * @return true 存在 false不存在
     */
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }

    /**
     * 获取hash数据中key对应的所有hkeys
     *
     * @param key
     * @return 对应的多个键值
     */
    public Set<String> hGetKeys(String key) {
        Set<String> binaryKeys = Sets.newHashSet();
        Set<Object> keys = redisTemplate.opsForHash().keys(key);
        keys.forEach(item ->
        {
            if (hasKey((String) item)) {
                binaryKeys.add((String) item);
            }
        });
        return binaryKeys;
    }

    /**
     * 获取hash数据中key对应的所有hkeys
     * 同hGetKeys
     *
     * @param key
     * @return
     */
    @Deprecated
    public Set<String> getHKeys(String key) {
        try (Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(key,
                ScanOptions.scanOptions().match("*").count(Integer.MAX_VALUE).build());) {
            Set<String> binaryKeys = Sets.newHashSet();
            while (cursor.hasNext()) {
                final String item = String.valueOf(cursor.next().getKey());
                if (hasKey(item)) {
                    binaryKeys.add(item);
                }
            }
            return binaryKeys;
        } catch (Throwable e) {
            log.error("Failed to scan key[{}] hKeys from redis.", key, e);
        }
        return new HashSet<>();
    }

}
springboot 错误请求处理类
RestClient工具类