天天看点

Java操作redis的工具类

目录

准备工作

代码

通用操作

String(字符串)类型操作

Hash(哈希)操作

List(列表)操作

Set(集合)操作

准备工作

首先引入pom依赖

​
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>1.8.4.RELEASE</version>
</dependency>
           

然后在类中注入StringRedisTemplate类

@Autowired
    private StringRedisTemplate redis;
           

代码

通用操作

/**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param expireTime
     * @return
     */
    public boolean set(final String key, Long expireTime) {
        boolean result = false;
        try {
            redis.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            log.error("调用redis【set】方法异常:{}", e);
        }
        return result;
    }
           
/**
     * 删除key
     *
     * @param key
     * @return
     */
    public boolean delKey(final String key) {
        boolean result = false;
        try {
            redis.delete(key);
            result = true;
        } catch (Exception e) {
            log.error("调用redis【delKey】方法异常:{}", e);
        }
        return result;
    }
           
/**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void removeAll(final String... keys) {
        try {
            Set<String> keySet = new HashSet<>();
            for (String key : keys) {
                keySet.add(key);
            }
            redis.delete(keySet);
        } catch (Exception e) {
            log.error("调用redis【removeAll】方法异常:{}", e);
        }
    }
           
/**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        try {
            if (exists(key)) {
                redis.delete(key);
            }
        } catch (Exception e) {
            log.error("调用redis【remove】方法异常:{}", e);
        }
    }
           
/**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        try {
            return redis.hasKey(key);
        } catch (Exception e) {
            log.error("调用redis【exists】方法异常:{}", e);
        }
        return false;
    }
           

String(字符串)类型操作

/**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, String value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<String, String> operations = redis.opsForValue();
            operations.set(key, value);
            redis.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            log.error("调用redis【set】方法异常:{}", e);
        }
        return result;
    }
           
/**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String getString(final String key) {
        try {
            String result = null;
            ValueOperations<String, String> operations = redis.opsForValue();
            result = operations.get(key);
            return result;
        } catch (Exception e) {
            log.error("调用redis【getString】方法异常:{}", e);
        }
        return null;
    }
           

Hash(哈希)操作

/**
     * 哈希添加并设置时效
     * @param key
     * @param hashKey
     * @param value
     * @param expireTime
     * @return
     */
    public void hashSet(String key, Object hashKey, Object value, Long expireTime) {
        try {
            HashOperations<String, Object, Object> hash = redis.opsForHash();
            hash.put(key, hashKey, value);
            redis.expire(key, expireTime, TimeUnit.MINUTES);
        } catch (Exception e) {
            log.error("调用redis【hashSet】方法异常:{}", e.getMessage());
        }
    }
           
/**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public String hmGet(String key, Object hashKey) {
        try {
            HashOperations<String, String, String> hash = redis.opsForHash();
            return hash.get(key, hashKey);
        } catch (Exception e) {
            log.error("调用redis【hmGet】方法异常:{}", e);
        }
        return null;
    }
           
/**
     * 哈希 删除
     *
     * @param key
     * @param hashKey
     */
    public void hmDel(String key, String... hashKey) {
        try {
            HashOperations<String, Object, Object> hash = redis.opsForHash();
            hash.delete(key, hashKey);
        } catch (Exception e) {
            log.error("调用redis【hmDel】方法异常:{}", e);
        }
    }
           
/**
     * 哈希获取数据
     *
     * @param key
     * @return
     */
    public Map<Object, Object> hGetAll(String key) {
        try {
            HashOperations<String, Object, Object> hash = redis.opsForHash();
            return hash.entries(key);
        } catch (Exception e) {
            log.error("调用redis【hGetAll】方法异常:{}", e);
        }
        return null;
    }
           

List(列表)操作

/**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lPush(String k, String v) {
        try {
            ListOperations<String, String> list = redis.opsForList();
            list.leftPush(k, v);
        } catch (Exception e) {
            log.error("调用redis【lPush】方法异常:{}", e);
        }
    }
           
/**
     * 列表获取
     *
     * @param k
     */
    public String lPop(String k) {
        try {
            ListOperations<String, String> list = redis.opsForList();
            return list.leftPop(k);
        } catch (Exception e) {
            log.error("调用redis【lPop】方法异常:{}", e);
        }
        return null;
    }
           
/**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<String> lRange(String k, long l, long l1) {
        try {
            ListOperations<String, String> list = redis.opsForList();
            return list.range(k, l, l1);
        } catch (Exception e) {
            log.error("调用redis【lRange】方法异常:{}", e);
        }
        return null;
    }
           
/**
     * 获取list长度
     *
     * @param k
     */
    public long llen(String k) {
        try {
            ListOperations<String, String> list = redis.opsForList();
            return list.size(k);
        } catch (Exception e) {
            log.error("调用redis【llen】方法异常:{}", e);
        }
        return 0L;
    }
           

Set(集合)操作

/**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(String key, String value) {
        try {
            SetOperations<String, String> set = redis.opsForSet();
            set.add(key, value);
        } catch (Exception e) {
            log.error("调用redis【add】方法异常:{}", e);
        }
    }
           
/**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<String> setMembers(String key) {
        try {
            SetOperations<String, String> set = redis.opsForSet();
            return set.members(key);
        } catch (Exception e) {
            log.error("调用redis【setMembers】方法异常:{}", e);
        }
        return null;
    }
           

继续阅读