SpringBoot 整合Redis,Redis工具类

 

package com.picchealth.special.redis.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
//@ConditionalOnProperty(
//        prefix = "spring.redis",
//        value = {"enable"},
//        havingValue = "true"
//)
public final class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    public RedisUtil() {
    }

    /**
     * 为给定 key 设置过期时间,以秒计
     *
     * @author: YaoGX
     * @Date: 2020/6/22 10:39
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0L) {
                this.redisTemplate.expire("insure_" + key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    /**
     * 获取key 设置的过期时间
     *
     * @author: YaoGX
     * @Date: 2020/6/22 10:40
     */
    public long getExpire(String key) {
        return this.redisTemplate.getExpire("insure_" + key, TimeUnit.SECONDS);
    }

    /**
     * 确定哈希hashKey是否存在
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:07
     */
    public boolean hasKey(String key) {
        try {
            return this.redisTemplate.hasKey("insure_" + key);
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    /**
     * 批量删除指定 key
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:23
     */
    public void del(String... keys) {
        if (keys != null && keys.length > 0) {

            List list = new ArrayList();
            for (int i = 0; i < keys.length; i++) {
                String key= "insure_"+ keys[i];
                list.add(key);
            }

            if (keys.length == 1) {
                this.redisTemplate.delete(list.get(0));
            } else {
                this.redisTemplate.delete(list);
            }
        }
    }

    /**
     * 根据key 获取value
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:24
     */
    public Object get(String key) {
        return key == null ? null : this.redisTemplate.opsForValue().get("insure_" + key);
    }

    /**
     * 向指定 key 赋值 value
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:26
     */
    public boolean set(String key, Object value) {
        try {
            this.redisTemplate.opsForValue().set("insure_" + key, value);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    /**
     * 向 key 中赋值 value 并设置过期时间 time ,单位 秒
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:27
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0L) {
                this.redisTemplate.opsForValue().set("insure_" + key, value, time, TimeUnit.SECONDS);
            } else {
                this.set("insure_" + key, value);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    /**
     * 给指定 key 设置自增因子
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:34
     */
    public long incr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            return this.redisTemplate.opsForValue().increment("insure_" + key, delta);
        }
    }

    /**
     * 给指定 key 设置自减因子
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:34
     */
    public long decr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递减因子必须大于0");
        } else {
            return this.redisTemplate.opsForValue().increment("insure_" + key, -delta);
        }
    }

    /****  Redis 的 hash 类型  ****/


    /**
     * hash 取值,根据 key和item 取值
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:36
     */
    public Object hget(String key, String item) {
        return this.redisTemplate.opsForHash().get("insure_" + key, item);
    }

    /**
     * 根据key 获取变量中的键值对。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:41
     */
    public Map hmget(String key) {
        return this.redisTemplate.opsForHash().entries("insure_" + key);
    }

    /**
     * 以map集合的形式添加键值对。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 11:43
     */
    public boolean hmset(String key, Map map) {
        try {
            this.redisTemplate.opsForHash().putAll("insure_" + key, map);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    /**
     * 以map集合的形式添加键值对并设置过期时间
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:32
     */
    public boolean hmset(String key, Map map, long time) {
        try {
            this.redisTemplate.opsForHash().putAll("insure_" + key, map);
            if (time > 0L) {
                this.expire("insure_" + key, time);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }


    /**
     * 新增hashMap值
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:33
     */
    public boolean hset(String key, String item, Object value) {
        try {
            this.redisTemplate.opsForHash().put("insure_" + key, item, value);
            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    /**
     * 新增hashMap值,并设置超时时间
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:33
     */
    public boolean hset(String key, String item, Object value, long time) {
        try {
            this.redisTemplate.opsForHash().put("insure_" + key, item, value);
            if (time > 0L) {
                this.expire("insure_" + key, time);
            }

            return true;
        } catch (Exception var7) {
            var7.printStackTrace();
            return false;
        }
    }

    /**
     * 删除变量中的键值对,可以传入多个参数,删除多个键值对。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:37
     */
    public void hdel(String key, Object... item) {
        this.redisTemplate.opsForHash().delete("insure_" + key, item);
    }

    /**
     * 判断变量中是否有指定的map键。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:39
     */
    public boolean hHasKey(String key, String item) {
        return this.redisTemplate.opsForHash().hasKey("insure_" + key, item);
    }

    /**
     * 使变量中的键以double值的大小进行自增长。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:40
     */
    public double hincr(String key, String item, double by) {
        return this.redisTemplate.opsForHash().increment("insure_" + key, item, by);
    }

    /**
     * 使变量中的键以double值的大小进行自减。
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:40
     */
    public double hdecr(String key, String item, double by) {
        return this.redisTemplate.opsForHash().increment("insure_" + key, item, -by);
    }

    /****   Redis set   ****/
    /**
     * 根据 key获取 值
     *
     * @author: YaoGX
     * @Date: 2020/6/22 13:42
     */
    public Set sGet(String key) {
        try {
            return this.redisTemplate.opsForSet().members("insure_" + key);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    /**
     * 检查给定的元素是否在变量中
     * @author: YaoGX
     * @Date: 2020/6/22 13:43
     */
    public boolean sHasKey(String key, Object value) {
        try {
            return this.redisTemplate.opsForSet().isMember("insure_" + key, value);
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    /**
     *  向变量中批量添加值。
     * @author: YaoGX
     * @Date: 2020/6/22 13:45
     */
    public long sSet(String key, Object... values) {
        try {
            return this.redisTemplate.opsForSet().add("insure_" + key, values);
        } catch (Exception var4) {
            var4.printStackTrace();
            return 0L;
        }
    }

    /**
     *  向变量中批量添加值并设置过期时间。
     * @author: YaoGX
     * @Date: 2020/6/22 13:45
     */
    public long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = this.redisTemplate.opsForSet().add("insure_" + key, values);
            if (time > 0L) {
                this.expire("insure_" + key, time);
            }

            return count;
        } catch (Exception var6) {
            var6.printStackTrace();
            return 0L;
        }
    }

    /**
     * 获取变量中值的个数。
     * @author: YaoGX
     * @Date: 2020/6/22 13:47
     */
    public long sGetSetSize(String key) {
        try {
            return this.redisTemplate.opsForSet().size("insure_" + key);
        } catch (Exception var3) {
            var3.printStackTrace();
            return 0L;
        }
    }

    /**
     * 根据key 批量移除变量中的元素。
     * @author: YaoGX
     * @Date: 2020/6/22 13:49
     */
    public long setRemove(String key, Object... values) {
        try {
            Long count = this.redisTemplate.opsForSet().remove("insure_" + key, values);
            return count;
        } catch (Exception var4) {
            var4.printStackTrace();
            return 0L;
        }
    }

    /**** Redis  List  ****/
    /**
     *  根据key 获取指定区间的值
     * @author: YaoGX
     * @Date: 2020/6/22 13:49
     */
    public List lGet(String key, long start, long end) {
        try {
            return this.redisTemplate.opsForList().range("insure_" + key, start, end);
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    /**
     *  根据key 获取 list集合的长度
     * @author: YaoGX
     * @Date: 2020/6/22 13:57
     */
    public long lGetListSize(String key) {
        try {
            return this.redisTemplate.opsForList().size("insure_" + key);
        } catch (Exception var3) {
            var3.printStackTrace();
            return 0L;
        }
    }

    /**
     *  根据key 获取集合指定位置的值
     * @author: YaoGX
     * @Date: 2020/6/22 13:58
     */
    public Object lGetIndex(String key, long index) {
        try {
            return this.redisTemplate.opsForList().index("insure_" + key, index);
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    /**
     *  向集合最右边添加元素。
     * @author: YaoGX
     * @Date: 2020/6/22 13:59
     */
    public boolean lSet(String key, Object value) {
        try {
            this.redisTemplate.opsForList().rightPush("insure_" + key, value);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    /**
     *  向集合最右边添加元素并设置过期时间。
     * @author: YaoGX
     * @Date: 2020/6/22 14:00
     */
    public boolean lSet(String key, Object value, long time) {
        try {
            this.redisTemplate.opsForList().rightPush("insure_" + key, value);
            if (time > 0L) {
                this.expire("insure_" + key, time);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    /**
     *  向集合右边批量添加元素。
     * @author: YaoGX
     * @Date: 2020/6/22 14:00
     */
    public boolean lSet(String key, List value) {
        try {
            this.redisTemplate.opsForList().rightPushAll("insure_" + key, value);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    /**
     *  向集合右边批量添加元素并设置过期时间。
     * @author: YaoGX
     * @Date: 2020/6/22 14:00
     */
    public boolean lSet(String key, List value, long time) {
        try {
            this.redisTemplate.opsForList().rightPushAll("insure_" + key, value);
            if (time > 0L) {
                this.expire("insure_" + key, time);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    /**
     *  在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错。
     * @author: YaoGX
     * @Date: 2020/6/22 14:06
     */
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            this.redisTemplate.opsForList().set("insure_" + key, index, value);
            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    /**
     * 从存储在键中的列表中删除等于值的元素的第一个计数事件。
     * count> 0:删除等于从左到右移动的值的第一个元素;
     * count = 0:删除等于value的所有元素。
     * @author: YaoGX
     * @Date: 2020/6/22 14:18
     */
    public long lRemove(String key, long count, Object value) {
        try {
            Long remove = this.redisTemplate.opsForList().remove("insure_" + key, count, value);
            return remove;
        } catch (Exception var6) {
            var6.printStackTrace();
            return 0L;
        }
    }
}
 
  

 

你可能感兴趣的:(redis)