Jedis工具类

public class JedisUtil {
    protected static Logger logger = LoggerFactory.getLogger(JedisUtil.class);


    public static JedisPool getConnect(String host, int port, String user, String password) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(8);
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMinIdle(0);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, 10000, user, password);
        return jedisPool;
    }


    public static boolean setString(String key, String value, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            logger.debug("setStringEx() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static String getString(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.get(key);
        } catch (Exception e) {
            logger.debug("getString() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static Long getExpire(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.expireTime(key);
        } catch (Exception e) {
            logger.debug("getString() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static Long getTtl(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.ttl(key);
        } catch (Exception e) {
            logger.debug("getString() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static String getType(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.type(key);
        } catch (Exception e) {
            logger.debug("getString() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static boolean delString(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.del(key);
            return true;
        } catch (Exception e) {
            logger.debug("delString() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean delHash(String key, String mKey, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.hdel(key, mKey);
            return true;
        } catch (Exception e) {
            logger.debug("setHash() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean setHash(String key, String mKey, String mVal, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.hset(key, mKey, mVal);
            return true;
        } catch (Exception e) {
            logger.debug("setHash() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean setHash(String key, HashMap hashMap, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.hset(key, hashMap);
            return true;
        } catch (Exception e) {
            logger.debug("setHash() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }


    public static String getHash(String key, String mKey, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.hget(key, mKey);
        } catch (Exception e) {
            logger.debug("setHash() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
        return null;
    }

    public static boolean setHashMulti(String key, Map map, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.hmset(key, map);
            return true;
        } catch (Exception e) {
            logger.debug("setMHash() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static List getHashMulti(String key, String[] members, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.hmget(key, members);
        } catch (Exception e) {
            logger.debug("getHashMulti() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
        return null;
    }

    public static List getHashValsAll(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.hvals(key);
        } catch (Exception e) {
            logger.debug("getHashValsAll() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
        return null;
    }

    public static Set getHashKeysAll(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            return jedis.hkeys(key);
        } catch (Exception e) {
            logger.debug("getHashValsAll() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
        return null;
    }

    public static boolean addScoreSet(String key, String mKey, Double score, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.zadd(key, score, mKey);
            return true;
        } catch (Exception e) {
            logger.debug("addScoreSet() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean delScoreSet(String key, String mKey, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.zrem(key, mKey);
            return true;
        } catch (Exception e) {
            logger.debug("delScoreSet() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public boolean changeScoreSet(String key, String mKey, int score, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.zincrby(key, score, mKey);
            return true;
        } catch (Exception e) {
            logger.debug("changeScoreSet() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }


    public static boolean setList(String key, Object value, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.lpush(key, value.toString());
            return true;
        } catch (Exception e) {
            logger.debug("setList() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean setList(String key, List list, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            for (String s : list) {
                jedis.lpush(key, s);
            }
            return true;
        } catch (Exception e) {
            logger.debug("setList() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }


    public static boolean setSet(String key, Object value, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            List stringList = (List) value;
            for (String s : stringList) {
                jedis.sadd(key, s);
            }
            return true;
        } catch (Exception e) {
            logger.debug("setSet() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static boolean setSet(String key, String value, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            jedis.sadd(key, value);
            return true;
        } catch (Exception e) {
            logger.debug("setSet() key {} throws:{}", key, e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }


    public static List getList(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            List lrange = jedis.lrange(key, 0, -1);
            return lrange;
        } catch (Exception e) {
            logger.debug("getList() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static Set getSet(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            Set smembers = jedis.smembers(key);
            return smembers;
        } catch (Exception e) {
            logger.debug("getSet() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static Map getHash(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            Map stringStringMap = jedis.hgetAll(key);
            return stringStringMap;
        } catch (Exception e) {
            logger.debug("getHash() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static List getZset(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            List tuples = jedis.zrangeWithScores(key, 0, -1);
            return tuples;
        } catch (Exception e) {
            logger.debug("getHash() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static Boolean isExist(String key, Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            boolean exists = jedis.exists(key);
            return exists;
        } catch (Exception e) {
            logger.debug("getHash() key {} throws:{}", key, e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static void setExpire(String key, Jedis jedis, Long expire, Integer db) {
        try {
            jedis.select(db);
            jedis.expire(key, expire);
        } catch (Exception e) {
            logger.debug("setExpire() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
    }

    public static void setExpireAt(String key, Jedis jedis, Long expire, Integer db) {
        try {
            jedis.select(db);
            jedis.expireAt(key, expire);
        } catch (Exception e) {
            logger.debug("setExpireAt() key {} throws:{}", key, e.getMessage());
        } finally {
            close(jedis);
        }
    }


    public static List getKeys(Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            Set keys = jedis.keys("*");
            List list = Arrays.asList(keys.toArray(new String[0]));
            return list;
        } catch (Exception e) {
            logger.debug("getKeys() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static Boolean updateKeys(Jedis jedis, Integer db, String key, String newKey) {
        try {
            jedis.select(db);
            jedis.rename(key, newKey);
            return true;
        } catch (Exception e) {
            logger.debug("updateKeys() key {} throws:{}", e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }


    public static Boolean lrem(Jedis jedis, Integer db, String key, String value) {
        try {
            jedis.select(db);
            jedis.lrem(key, 1, value);
            return true;
        } catch (Exception e) {
            logger.debug("lrem() key {} throws:{}", e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static Boolean srem(Jedis jedis, Integer db, String key, String value) {
        try {
            jedis.select(db);
            jedis.srem(key, value);
            return true;
        } catch (Exception e) {
            logger.debug("srem() key {} throws:{}", e.getMessage());
            return false;
        } finally {
            close(jedis);
        }
    }

    public static ScanResult scan(Jedis jedis, Integer db, Page page) {
        try {
            jedis.select(db);
            ScanParams params = new ScanParams();
            params.count(page.getPageSize());
            ScanResult scan = jedis.scan(page.getPageNum().toString(), params.match("*"));
            return scan;
        } catch (Exception e) {
            logger.debug("scan() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static List getSlowLogs(Jedis jedis, Integer db) {
        try {
            jedis.select(db);
            List slowlogs = jedis.slowlogGet();
            return slowlogs;
        } catch (Exception e) {
            logger.debug("scan() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }


    public static String getClientInfo(Jedis jedis) {
        try {
            return jedis.clientList();
        } catch (Exception e) {
            logger.debug("scan() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static String getConfigInfo(Jedis jedis) {
        try {
            return jedis.info();
        } catch (Exception e) {
            logger.debug("scan() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }

    public static ScanResult getKeys(Jedis jedis,Integer db,Page page) {
        try {
            String cursor = page.getCursor();
            Integer pageSize = page.getPageSize();
            ScanParams params=new ScanParams();
            params.count(pageSize);
            jedis.select(db);
            ScanResult scan = jedis.scan(cursor, params);
            return scan;
        } catch (Exception e) {
            logger.debug("scan() key {} throws:{}", e.getMessage());
            return null;
        } finally {
            close(jedis);
        }
    }




    public static void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

你可能感兴趣的:(java,数据库)