Redis工具类

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**

  • Redis工具类

  • @author DuKang
    /
    @Component
    public final class RedisUtil
    {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    // =common
    /
    *

    • 指定缓存失效时间
    • @param key 键
    • @param time 时间(秒)
    • @return
      */
      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;
      }
      }

    /**

    • 根据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) {
      try {
      return redisTemplate.hasKey(key);
      } catch (Exception e) {
      e.printStackTrace();
      return false;
      }
      }

    /**

    • 删除缓存
    • @param key 可以传一个值 或多个
      */
      @SuppressWarnings(“unchecked”)
      public void del(String… key) {
      if (key != null && key.length > 0) {
      if (key.length == 1) {
      redisTemplate.delete(key[0]);
      } else {
      redisTemplate.delete(CollectionUtils.arrayToList(key));
      }
      }
      }

    // String=
    /**

    • 普通缓存获取
    • @param key 键
    • @return 值
      */
      public Object get(String key) {
      return key == null ? null : redisTemplate.opsForValue().get(key);
      }

    /**

    • 普通缓存放入
    • @param key 键
    • @param value 值
    • @return true成功 false失败
      */
      public boolean set(String key, Object value) {
      try {
      redisTemplate.opsForValue().set(key, value);
      return true;
      } ca
  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值