Java封装Redis常用操作

package com.advance.Redis;

import org.apache.log4j.Logger;
import org.testng.annotations.Test;
import redis.clients.jedis.Jedis;

import java.util.*;

/**

  • @Author: 谷天乐

  • @Date: 2019/2/25 17:17

  • @Gratitude: wudalang_gd

  • @Description: 根据原生 Jedis 封装常用工具
    */
    public class RedisUtil{
    private static Logger logger = Logger.getLogger(RedisUtil.class);
    private static final String ip = "localhost";
    private static final Integer port = 6379;
    //获取连接
    public Jedis connection() throws Exception{
    Jedis jedis
    = new Jedis(ip,port);
    return jedis;
    }

    //为 string 添加元素
    public void set(String key, String value) throws Exception {
    Jedis jedis
    = connection();
    jedis.set(key,value);

    }

    //获取 string
    public String get(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.get(key);
    }

    //追加 string
    public void append(String key, String value) throws Exception {
    Jedis jedis
    = connection();
    jedis.append(key,value);
    }

    //添加 set
    public void sadd(String key, Set<String> value) throws Exception {
    Jedis jedis
    = connection();
    for(String str: value){
    jedis.sadd(key, str);
    }
    }

    //set 删除指定元素
    public void srem(String key, Set<String> value) throws Exception {
    Jedis jedis
    = connection();
    Iterator
    <String> it = value.iterator();
    while(it.hasNext()){
    String str
    = it.next();
    jedis.srem(key, str);
    }
    }

    //获取 key 对应的 value 总数
    public Long scard(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.scard(key);
    }

    //获取 key 对应的所有 value
    public Set<String> smembers(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.smembers(key);
    }

    //判断 set 是否存在
    public boolean sismember(String key, String value) throws Exception {
    Jedis jedis
    = connection();
    return jedis.sismember(key,value);
    }

    //随机获取数据
    public String srandmember(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.srandmember(key);
    }

    //向 list 添加元素
    public void lpush(String key, List<String> list) throws Exception {
    Jedis jedis
    = connection();
    for(String s: list){
    jedis.lpush(key,s);
    }
    }

    //获取 list
    public List<String> lrange(String key, Integer start, Integer end)
    throws Exception {
    Jedis jedis
    = connection();
    return jedis.lrange(key, start, end);
    }

    //删除任意类型的 key
    public void del(String key) throws Exception {
    Jedis jedis
    = connection();
    jedis.del(key);
    }

    //设置 map
    public void hmset(String key, Map<String, String> map) throws Exception {
    Jedis jedis
    = connection();
    jedis.hmset(key,map);
    }

    //获取 map 的 key 的个数
    public Long hlen(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.hlen(key);
    }

    //获取 map 中所有 key
    public Set<String> hkeys(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.hkeys(key);
    }

    //获取 map 中所有 value
    public List<String> hvals(String key) throws Exception {
    Jedis jedis
    = connection();
    return jedis.hvals(key);
    }

    //获取 map 中的指定 key 的 value
    public List<String> hmget(String key, String... params)
    throws Exception {
    Jedis jedis
    = connection();
    if (null == params || params.length == 0) {
    throw new RuntimeException(this.getClass().getSimpleName()+ "::"
    + new Exception().getStackTrace()[0].getMethodName()+"参数不能为空");
    }
    return jedis.hmget(key,params);
    }

    //获取 map 所有的 key 和 value
    public Map<String, String> hgetAll(String key)
    throws Exception {
    Jedis jedis
    = connection();
    return jedis.hgetAll(key);
    }

    //删除指定 key 的 map
    public void hdel(String key, String... params) throws Exception {
    Jedis jedis
    = connection();
    if (null == params || params.length == 0) {
    throw new RuntimeException(this.getClass().getSimpleName()+ "::"
    + new Exception().getStackTrace()[0].getMethodName()+"参数不能为空");
    }
    jedis.hdel(key,params);
    }

    //测试 string
    @Test
    public void testString() throws Exception{
    RedisUtil r
    = new RedisUtil();
    r.set(
    "z", "wb");
    String s
    = r.get("z");
    logger.debug(s);
    }

    //测试 set
    @Test
    public void testList() throws Exception{
    RedisUtil r
    = new RedisUtil();
    List
    <String> list = new ArrayList<>();
    list.add(
    "w");
    list.add(
    "b");
    r.lpush(
    "list",list);
    List
    <String> t = r.lrange("list",0,-1);
    logger.debug(t);
    }

    //测试 set
    @Test
    public void testSet() throws Exception{
    RedisUtil r
    = new RedisUtil();
    Set
    <String> set = new HashSet<String>();
    set.add(
    "w");
    set.add(
    "b");
    r.sadd(
    "set",set);
    Set
    <String> t = r.smembers("set");
    logger.debug(t);
    }

    //测试 map
    @Test
    public void mapTest() throws Exception {
    RedisUtil r
    = new RedisUtil();
    Map
    <String,String> map = new HashMap<>();
    map.put(
    "Red Alert 3","Long live Soviet");
    map.put(
    "Starcraft","No one can undie");
    map.put(
    "PUBG","Keep breath");
    r.hmset(
    "Game",map);
    r.hdel(
    "Game","Starcraft");
    Map
    <String,String> m = r.hgetAll("Game");
    logger.debug(m);
    }
    }