chenjiahe
2022-04-02 0344dc66783353faff118a0ea91f7d2aa07bbd4a
src/main/java/com/hx/redis/RedisUtil.java
@@ -4,10 +4,13 @@
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/** 文件处理工具
/**
 * 文件处理工具
 *
 * @author wangrenhuang
 * @Date 2021-10-19
 */
@@ -20,9 +23,9 @@
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * [判断key是否存在]
     *
     * @param key 键
     * @return true 存在 false不存在
     */
@@ -36,14 +39,17 @@
    /**
     * [普通缓存获取]
     *
     * @param key 键
     * @return 值
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }
    /**
     * [普通缓存删除]
     *
     * @param key 键
     * @return 值
     */
@@ -51,13 +57,15 @@
        try {
            Boolean aBoolean = redisTemplate.hasKey(key);
            return aBoolean == false ? true : redisTemplate.delete(key);
        }catch (Exception e){
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * [普通缓存放入]
     * @param key 键
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
@@ -73,9 +81,10 @@
    /**
     * 普通缓存放入并设置时间
     * @param key 键
     *
     * @param key   键
     * @param value 值
     * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time) {
@@ -92,5 +101,110 @@
        }
    }
}
    /**
     * 26
     * 指定缓存失效时间
     * 27
     *
     * @param key  键
     *             28
     * @param time 时间(秒)
     *             29
     * @return 30
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * hash 删除操作
     *
     * @param key   键
     * @param valueKey 值
     * @return true成功 false 失败
     */
    public boolean hashDel(String key, Object... valueKey) {
        try {
            redisTemplate.opsForHash().delete(key,valueKey);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * hash 设置hashMapt操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSetMap(String key, Map<String,Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * hash 设置单个hashMap操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSet(String hashKey, String key, Object value) {
        try {
            redisTemplate.opsForHash().put(hashKey,key,value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * hash 获取整个hashKey数据
     *
     * @param hashKey   键
     * @return true成功 false 失败
     */
    public Map<Object, Object> hashGetAll(String hashKey) {
        try {
            return redisTemplate.opsForHash().entries(hashKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * hash 获取单个hashKey
     *
     * @param hashKey   键
     * @return true成功 false 失败
     */
    public Object hashGet(String hashKey,String key) {
        try {
            return redisTemplate.opsForHash().get(hashKey,key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}