【redis缓存】Redis封装类

Redis封装Hash&String

对redisTemplate简单封装,方便使用

  1. Hash类型的
public class HashRedisService<K, HK, HV> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置有效期
     *
     * @param key
     * @param timeout
     * @param unit
     */
    public void expire(String key, long timeout, TimeUnit unit) {
        if (unit == null) {
            unit = TimeUnit.SECONDS;
        }
        redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 获取所有匹配成功的key set集合
     *
     * @param pattern
     * @return
     */
    public Set<K> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 向名称为key的hash中添加元素hashKey
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hSet(K key, HK hashKey, HV value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 向名称为key的hash中添加元素hashKey, 若hashKey对应value有值则不覆盖
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hSetIfAbsent(K key, HK hashKey, HV value) {
        redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
    }

    /**
     * 返回名称为key的hash中hashKey对应的value
     *
     * @param key
     * @param hashKey
     * @return
     */
    public HV hGet(K key, HK hashKey) {
        return (HV) redisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 向名称为key的hash中添加元素hashMap
     *
     * @param key
     * @param hashMap
     */
    public void hMSet(K key, Map<? extends HK, ? extends HV> hashMap) {
        redisTemplate.opsForHash().putAll(key, hashMap);
    }

    /**
     * 返回名称为key的hash中hashKeys i对应的value 键值对
     *
     * @param key
     * @param hashKeys
     * @return
     */
    public Map<HK, HV> hMGet(K key, Collection<HK> hashKeys) {
        Map<HK, HV> hashMap = new HashMap<HK, HV>();
        List<HV> valueList = redisTemplate.opsForHash().multiGet(key, hashKeys);

        int i = 0;
        for (HK hashKey : hashKeys) {
            hashMap.put(hashKey, valueList.get(i));
            i++;
        }
        return hashMap;
    }

    /**
     * 返回名称为key的hash中hashKeys i对应的value 集合
     *
     * @param key
     * @param hashKeys
     * @return
     */
    public List<HV> hMGet2List(final K key, final Collection<HK> hashKeys) {
        return redisTemplate.opsForHash().multiGet(key, hashKeys);
    }

    /**
     * 返回名称为key的hash中所有键对应的value 键值对
     *
     * @param key
     * @return
     */
    public Map<HK, HV> hVals(K key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 返回名称为key的hash中所有键对应的value 集合
     *
     * @param key
     * @return
     */
    public List<HV> hVals2List(K key) {
        return redisTemplate.opsForHash().values(key);
    }

    /**
     * 删除名称为key的hash中键为hashKey的域
     *
     * @param key
     * @param hashKeys
     */
    public void hDel(K key, HK... hashKeys) {
        redisTemplate.opsForHash().delete(key, hashKeys);
    }

    /**
     * 返回名称为key的hash中元素个数
     *
     * @param key
     * @return
     */
    public Long hLen(K key) {
        return redisTemplate.opsForHash().size(key);
    }

    /**
     * 名称为key的hash中是否存在键为hashKey的域
     *
     * @param key
     * @param hashKey
     * @return
     */
    public boolean hasKey(K key, HK hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    /**
     * 名称为key的hash是否存在
     *
     * @param key
     * @return
     */
    public boolean hasKey(K key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 返回名称为key的hash中所有键
     *
     * @param key
     * @return
     */
    public Set<HK> hKeys(K key) {
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     * 将名称为key的hash中hashKey的value增加Long
     *
     * @param key
     * @param hashKey
     * @param delta 变量增量
     * @return
     */
    public Long hIncrBy(K key, HK hashKey, Long delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    /**
     * 将名称为key的hash中hashKey的value增加Double
     *
     * @param key
     * @param hashKey
     * @param stepSize
     * @return
     */
    public Double hIncrBy(K key, HK hashKey, Double stepSize) {
        return redisTemplate.opsForHash().increment(key, hashKey, stepSize);
    }


}
  1. String类型的数据
public class StringRedisService {

    @Autowired
    private RedisTemplate<String, String> stringRedisTemplate;

    /**
     * 设置有效期
     *
     * @param key
     * @param timeout
     * @param unit
     */
    public void expire(String key, long timeout, TimeUnit unit) {
        if (unit == null) {
            unit = TimeUnit.SECONDS;
        }
        stringRedisTemplate.expire(key, timeout, unit);
    }

    /**
     * 获取所有匹配成功的key set集合
     *
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern) {
        return stringRedisTemplate.keys(pattern);
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 插入字符串key value
     *
     * @param key
     * @param val
     */
    public void set(String key, String val) {
        stringRedisTemplate.opsForValue().set(key, val);
    }

    /**
     * 插入key, 并设置有效期(单位s秒)
     *
     * @param key
     * @param val
     * @param timeout
     */
    public void set(String key, String val, long timeout) {
        stringRedisTemplate.opsForValue().set(key, val, timeout, TimeUnit.SECONDS);
    }

    /**
     * 插入json格式的对象
     *
     * @param key
     * @param val
     */
    public void setJsonObj(String key, Object val) throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val));
    }

    /**
     * 插入json格式的对象, 并设置有效期(单位s秒)
     *
     * @param key
     * @param val
     * @param timeout
     */
    public void setJsonObj(String key, Object val, long timeout) throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val), timeout, TimeUnit.SECONDS);
    }

    /**
     * 获取字符串, by key
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 获取json字符串, 并转换为参数指定的对象
     *
     * @param key
     * @param clazz
     * @param <T>
     */
    public <T> T getJson(String key, Class<T> clazz) {
        String val = stringRedisTemplate.opsForValue().get(key);
        return JsonUtils.fromJson(val, clazz);
    }

    /**
     * 获取key 并且截取字符串
     * @param key
     * @param start
     * @param end
     * @return
     */
    public String substr(String key, Long start, Long end){
        return stringRedisTemplate.opsForValue().get(key, start,end);
    }

    /**
     * 获取老的值并设置新的值
     * @param key
     * @param val
     * @return
     */
    public String getAndSet(String key, String val){
        return stringRedisTemplate.opsForValue().getAndSet(key, val);
    }

    public List<String> multiGet(Collection<String> keys){
        return stringRedisTemplate.opsForValue().multiGet(keys);
    }

    public void setIfAbsent(String key, String val){
        stringRedisTemplate.opsForValue().setIfAbsent(key,val);
    }

    public Long strlen(String key){
        return stringRedisTemplate.opsForValue().size(key);
    }

    public void append(String key, String val){
        stringRedisTemplate.opsForValue().append(key,val);
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值