Jedis工具類


概述

Jedis是Redis官方推薦的Java連接開發(fā)工具。要在Java開發(fā)中使用好Redis中間件,必須對Jedis熟悉才能寫成漂亮的代碼。


基本使用

Jedis的基本使用非常簡單,只需要創(chuàng)建Jedis對象的時候指定host,port, password即可。當(dāng)然,Jedis對象又很多構(gòu)造方法,都大同小異,只是對應(yīng)和Redis連接的socket的參數(shù)不一樣而已。簡單使用如下圖所示

基本使用

連接池使用

Jedis連接池是基于apache-commons pool2實現(xiàn)的。在構(gòu)建連接池對象的時候,需要提供池對象的配置對象,及JedisPoolConfig(繼承自GenericObjectPoolConfig)。我們可以通過這個配置對象對連接池進行相關(guān)參數(shù)的配置(如最大連接數(shù),最大空數(shù)等)。

連接池使用

Ps.使用Jedis連接池之后,盡量在用完連接對象后記得把連接歸還給連接池。只需要使用Jedis的close方法就可以了。


工具類



package com.aicai.qa.tools.statics.redis;

import com.aicai.qa.tools.statics.config.SysConfigUtil;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author tengfei
* @version 1.0
* @date 2018/7/13 下午4:15
**/
public class RedisUtil {

private JedisPool pool = null;
private RedisUtil() {
    if (pool == null) {
        String ip = SysConfigUtil.getSysConfigUtil("redis.properties").getString("redis.host");
        int port = SysConfigUtil.getSysConfigUtil("redis.properties").getInt("redis.port");
        String password = SysConfigUtil.getSysConfigUtil("redis.properties").getString("redis.password");
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(SysConfigUtil.getSysConfigUtil("redis.properties").getInt("redis.maxTotal"));
        jedisPoolConfig.setMaxIdle(SysConfigUtil.getSysConfigUtil("redis.properties").getInt("redis.maxIdle"));
        jedisPoolConfig.setMaxWaitMillis(SysConfigUtil.getSysConfigUtil("redis.properties").getLong("redis.maxWaitMillis"));
        jedisPoolConfig.setTestOnBorrow(SysConfigUtil.getSysConfigUtil("redis.properties").getBoolean("redis.testOnBorrow"));
        if (password != null && !"".equals(password)) {
            // redis 設(shè)置了密碼
            pool = new JedisPool(jedisPoolConfig, ip, port, 10000, password);
        }else {
            // redis 未設(shè)置密碼
            pool = new JedisPool(jedisPoolConfig,ip,port,10000);
        }


    }


}


/**
 * 獲取指定key的值,如果key不存在返回null,如果該Key存儲的不是字符串,會拋出一個錯誤
 *
 * @param key
 * @return
 */
public String get(String key) {
    Jedis jedis = getJedis();
    String value = null;
    value = jedis.get(key);
    return value;
}


/**
 * 設(shè)置key的值為value
 *
 * @param key
 * @param value
 * @return
 */
public String set(String key, String value) {
    Jedis jedis = getJedis();
    return jedis.set(key, value);
}


/**
 * 刪除指定的key,也可以傳入一個包含key的數(shù)組
 *
 * @param keys
 * @return
 */
public Long del(String... keys) {
    Jedis jedis = getJedis();
    return jedis.del(keys);
}


/**
 * 通過key向指定的value值追加值
 *
 * @param key
 * @param str
 * @return
 */
public Long append(String key, String str) {
    Jedis jedis = getJedis();
    return jedis.append(key, str);
}


/**
 * 判斷key是否存在
 *
 * @param key
 * @return
 */
public Boolean exists(String key) {
    Jedis jedis = getJedis();
    return jedis.exists(key);
}


/**
 * 設(shè)置key value,如果key已經(jīng)存在則返回0
 *
 * @param key
 * @param value
 * @return
 */
public Long setnx(String key, String value) {
    Jedis jedis = getJedis();
    return jedis.setnx(key, value);
}


/**
 * 設(shè)置key value并指定這個鍵值的有效期
 *
 * @param key
 * @param seconds
 * @param value
 * @return
 */
public String setex(String key, int seconds, String value) {
    Jedis jedis = getJedis();
    return jedis.setex(key, seconds, value);
}


/**
 * 通過key 和offset 從指定的位置開始將原先value替換
 *
 * @param key
 * @param offset
 * @param str
 * @return
 */
public Long setrange(String key, int offset, String str) {
    Jedis jedis = getJedis();
    return jedis.setrange(key, offset, str);
}


/**
 * 通過批量的key獲取批量的value
 *
 * @param keys
 * @return
 */
public List<String> mget(String... keys) {
    Jedis jedis = getJedis();
    return jedis.mget(keys);
}


/**
 * 批量的設(shè)置key:value,也可以一個
 *
 * @param keysValues
 * @return
 */
public String mset(String... keysValues) {
    Jedis jedis = getJedis();
    return jedis.mset(keysValues);
}


/**
 * 批量的設(shè)置key:value,可以一個,如果key已經(jīng)存在則會失敗,操作會回滾
 *
 * @param keysValues
 * @return
 */
public Long msetnx(String... keysValues) {
    Jedis jedis = getJedis();
    return jedis.msetnx(keysValues);
}


/**
 * 設(shè)置key的值,并返回一個舊值
 *
 * @param key
 * @param value
 * @return
 */
public String getSet(String key, String value) {
    Jedis jedis = getJedis();
    return jedis.getSet(key, value);
}


/**
 * 通過下標(biāo) 和key 獲取指定下標(biāo)位置的 value
 *
 * @param key
 * @param startOffset
 * @param endOffset
 * @return
 */
public String getrange(String key, int startOffset, int endOffset) {
    Jedis jedis = getJedis();
    return jedis.getrange(key, startOffset, endOffset);
}


/**
 * 通過key 對value進行加值+1操作,當(dāng)value不是int類型時會返回錯誤,當(dāng)key不存在是則value為1
 *
 * @param key
 * @return
 */
public Long incr(String key) {
    Jedis jedis = getJedis();
    return jedis.incr(key);
}


/**
 * 通過key給指定的value加值,如果key不存在,則這是value為該值
 *
 * @param key
 * @param integer
 * @return
 */
public Long incrBy(String key, long integer) {
    Jedis jedis = getJedis();
    return jedis.incrBy(key, integer);
}


/**
 * 對key的值做減減操作,如果key不存在,則設(shè)置key為-1
 *
 * @param key
 * @return
 */
public Long decr(String key) {
    Jedis jedis = getJedis();
    return jedis.decr(key);
}


/**
 * 減去指定的值
 *
 * @param key
 * @param integer
 * @return
 */
public Long decrBy(String key, long integer) {
    Jedis jedis = getJedis();
    return jedis.decrBy(key, integer);
}


/**
 * 通過key獲取value值的長度
 *
 * @param key
 * @return
 */
public Long strLen(String key) {
    Jedis jedis = getJedis();
    return jedis.strlen(key);
}


/**
 * 通過key給field設(shè)置指定的值,如果key不存在則先創(chuàng)建,如果field已經(jīng)存在,返回0
 *
 * @param key
 * @param field
 * @param value
 * @return
 */
public Long hsetnx(String key, String field, String value) {
    Jedis jedis = getJedis();
    return jedis.hsetnx(key, field, value);
}


/**
 * 通過key給field設(shè)置指定的值,如果key不存在,則先創(chuàng)建
 *
 * @param key
 * @param field
 * @param value
 * @return
 */
public Long hset(String key, String field, String value) {
    Jedis jedis = getJedis();
    return jedis.hset(key, field, value);
}


/**
 * 通過key同時設(shè)置 hash的多個field
 *
 * @param key
 * @param hash
 * @return
 */
public String hmset(String key, Map<String, String> hash) {
    Jedis jedis = getJedis();
    return jedis.hmset(key, hash);
}


/**
 * 通過key 和 field 獲取指定的 value
 *
 * @param key
 * @param failed
 * @return
 */
public String hget(String key, String failed) {
    Jedis jedis = getJedis();
    return jedis.hget(key, failed);
}


/**
 * 設(shè)置key的超時時間為seconds
 *
 * @param key
 * @param seconds
 * @return
 */
public Long expire(String key, int seconds) {
    Jedis jedis = getJedis();
    return jedis.expire(key, seconds);
}


/**
 * 通過key 和 fields 獲取指定的value 如果沒有對應(yīng)的value則返回null
 *
 * @param key
 * @param fields 可以是 一個String 也可以是 String數(shù)組
 * @return
 */
public List<String> hmget(String key, String... fields) {
    Jedis jedis = getJedis();
    return jedis.hmget(key, fields);
}


/**
 * 通過key給指定的field的value加上給定的值
 *
 * @param key
 * @param field
 * @param value
 * @return
 */
public Long hincrby(String key, String field, Long value) {
    Jedis jedis = getJedis();
    return jedis.hincrBy(key, field, value);
}


/**
 * 通過key和field判斷是否有指定的value存在
 *
 * @param key
 * @param field
 * @return
 */
public Boolean hexists(String key, String field) {
    Jedis jedis = getJedis();
    return jedis.hexists(key, field);
}


/**
 * 通過key返回field的數(shù)量
 *
 * @param key
 * @return
 */
public Long hlen(String key) {
    Jedis jedis = getJedis();
    return jedis.hlen(key);
}


/**
 * 通過key 刪除指定的 field
 *
 * @param key
 * @param fields 可以是 一個 field 也可以是 一個數(shù)組
 * @return
 */
public Long hdel(String key, String... fields) {
    Jedis jedis = getJedis();
    return jedis.hdel(key, fields);
}


/**
 * 通過key返回所有的field
 *
 * @param key
 * @return
 */
public Set<String> hkeys(String key) {
    Jedis jedis = getJedis();
    return jedis.hkeys(key);
}


/**
 * 通過key返回所有和key有關(guān)的value
 *
 * @param key
 * @return
 */
public List<String> hvals(String key) {
    Jedis jedis = getJedis();
    return jedis.hvals(key);
}


/**
 * 通過key獲取所有的field和value
 *
 * @param key
 * @return
 */
public Map<String, String> hgetall(String key) {
    Jedis jedis = getJedis();
    return jedis.hgetAll(key);
}


/**
 * 通過key向list頭部添加字符串
 *
 * @param key
 * @param strs 可以是一個string 也可以是string數(shù)組
 * @return 返回list的value個數(shù)
 */
public Long lpush(String key, String... strs) {
    Jedis jedis = getJedis();
    return jedis.lpush(key, strs);
}


/**
 * 通過key向list尾部添加字符串
 *
 * @param key
 * @param strs 可以是一個string 也可以是string數(shù)組
 * @return 返回list的value個數(shù)
 */
public Long rpush(String key, String... strs) {
    Jedis jedis = getJedis();
    return jedis.rpush(key, strs);
}


/**
 * 通過key在list指定的位置之前或者之后 添加字符串元素
 *
 * @param key
 * @param where LIST_POSITION枚舉類型
 * @param pivot list里面的value
 * @param value 添加的value
 * @return
 */
public Long linsert(String key, BinaryClient.LIST_POSITION where,
                    String pivot, String value) {
    Jedis jedis = getJedis();
    return jedis.linsert(key, where, pivot, value);
}


/**
 * 通過key設(shè)置list指定下標(biāo)位置的value
 * 如果下標(biāo)超過list里面value的個數(shù)則報錯
 *
 * @param key
 * @param index 從0開始
 * @param value
 * @return 成功返回OK
 */
public String lset(String key, Long index, String value) {
    Jedis jedis = getJedis();
    return jedis.lset(key, index, value);
}


/**
 * 通過key從對應(yīng)的list中刪除指定的count個 和 value相同的元素
 *
 * @param key
 * @param count 當(dāng)count為0時刪除全部
 * @param value
 * @return 返回被刪除的個數(shù)
 */
public Long lrem(String key, long count, String value) {
    Jedis jedis = getJedis();
    return jedis.lrem(key, count, value);
}


/**
 * 通過key保留list中從strat下標(biāo)開始到end下標(biāo)結(jié)束的value值
 *
 * @param key
 * @param start
 * @param end
 * @return 成功返回OK
 */
public String ltrim(String key, long start, long end) {
    Jedis jedis = getJedis();
    return jedis.ltrim(key, start, end);
}


/**
 * 通過key從list的頭部刪除一個value,并返回該value
 *
 * @param key
 * @return
 */
public synchronized String lpop(String key) {

    Jedis jedis = getJedis();
    return jedis.lpop(key);
}


/**
 * 通過key從list尾部刪除一個value,并返回該元素
 *
 * @param key
 * @return
 */
synchronized public String rpop(String key) {
    Jedis jedis = getJedis();
    return jedis.rpop(key);
}


/**
 * 通過key從一個list的尾部刪除一個value并添加到另一個list的頭部,并返回該value
 * 如果第一個list為空或者不存在則返回null
 *
 * @param srckey
 * @param dstkey
 * @return
 */
public String rpoplpush(String srckey, String dstkey) {
    Jedis jedis = getJedis();
    return jedis.rpoplpush(srckey, dstkey);
}


/**
 * 通過key獲取list中指定下標(biāo)位置的value
 *
 * @param key
 * @param index
 * @return 如果沒有返回null
 */
public String lindex(String key, long index) {
    Jedis jedis = getJedis();
    return jedis.lindex(key, index);
}


/**
 * 通過key返回list的長度
 *
 * @param key
 * @return
 */
public Long llen(String key) {
    Jedis jedis = getJedis();
    return jedis.llen(key);
}


/**
 * 通過key獲取list指定下標(biāo)位置的value
 * 如果start 為 0 end 為 -1 則返回全部的list中的value
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public List<String> lrange(String key, long start, long end) {
    Jedis jedis = getJedis();
    return jedis.lrange(key, start, end);
}


/**
 * 通過key向指定的set中添加value
 *
 * @param key
 * @param members 可以是一個String 也可以是一個String數(shù)組
 * @return 添加成功的個數(shù)
 */
public Long sadd(String key, String... members) {
    Jedis jedis = getJedis();
    return jedis.sadd(key, members);
}


/**
 * 通過key刪除set中對應(yīng)的value值
 *
 * @param key
 * @param members 可以是一個String 也可以是一個String數(shù)組
 * @return 刪除的個數(shù)
 */
public Long srem(String key, String... members) {
    Jedis jedis = getJedis();
    return jedis.srem(key, members);
}


/**
 * 通過key隨機刪除一個set中的value并返回該值
 *
 * @param key
 * @return
 */
public String spop(String key) {
    Jedis jedis = getJedis();
    return jedis.spop(key);
}


/**
 * 通過key獲取set中的差集
 * 以第一個set為標(biāo)準(zhǔn)
 *
 * @param keys 可以 是一個string 則返回set中所有的value 也可以是string數(shù)組
 * @return
 */
public Set<String> sdiff(String... keys) {
    Jedis jedis = getJedis();
    return jedis.sdiff(keys);
}


/**
 * 通過key獲取set中的差集并存入到另一個key中
 * 以第一個set為標(biāo)準(zhǔn)
 *
 * @param dstkey 差集存入的key
 * @param keys   可以 是一個string 則返回set中所有的value 也可以是string數(shù)組
 * @return
 */
public Long sdiffstore(String dstkey, String... keys) {
    Jedis jedis = getJedis();
    return jedis.sdiffstore(dstkey, keys);
}


/**
 * 通過key獲取指定set中的交集
 *
 * @param keys 可以 是一個string 也可以是一個string數(shù)組
 * @return
 */
public Set<String> sinter(String... keys) {
    Jedis jedis = getJedis();
    return jedis.sinter(keys);
}


/**
 * 通過key獲取指定set中的交集 并將結(jié)果存入新的set中
 *
 * @param dstkey
 * @param keys   可以 是一個string 也可以是一個string數(shù)組
 * @return
 */
public Long sinterstore(String dstkey, String... keys) {
    Jedis jedis = getJedis();
    return jedis.sinterstore(dstkey, keys);
}


/**
 * 通過key返回所有set的并集
 *
 * @param keys 可以 是一個string 也可以是一個string數(shù)組
 * @return
 */
public Set<String> sunion(String... keys) {
    Jedis jedis = getJedis();
    return jedis.sunion(keys);
}


/**
 * 通過key返回所有set的并集,并存入到新的set中
 *
 * @param dstkey
 * @param keys   可以 是一個string 也可以是一個string數(shù)組
 * @return
 */
public Long sunionstore(String dstkey, String... keys) {
    Jedis jedis = getJedis();
    return jedis.sunionstore(dstkey, keys);
}


/**
 * 通過key將set中的value移除并添加到第二個set中
 *
 * @param srckey 需要移除的
 * @param dstkey 添加的
 * @param member set中的value
 * @return
 */
public Long smove(String srckey, String dstkey, String member) {
    Jedis jedis = getJedis();
    return jedis.smove(srckey, dstkey, member);
}


/**
 * 通過key獲取set中value的個數(shù)
 *
 * @param key
 * @return
 */
public Long scard(String key) {
    Jedis jedis = getJedis();
    return jedis.scard(key);
}


/**
 * 通過key判斷value是否是set中的元素
 *
 * @param key
 * @param member
 * @return
 */
public Boolean sismember(String key, String member) {
    Jedis jedis = getJedis();
    return jedis.sismember(key, member);
}


/**
 * 通過key獲取set中隨機的value,不刪除元素
 *
 * @param key
 * @return
 */
public String srandmember(String key) {
    Jedis jedis = getJedis();
    return jedis.srandmember(key);
}


/**
 * 通過key獲取set中所有的value
 *
 * @param key
 * @return
 */
public Set<String> smembers(String key) {
    Jedis jedis = getJedis();
    return jedis.smembers(key);
}


/**
 * 通過key向zset中添加value,score,其中score就是用來排序的
 * 如果該value已經(jīng)存在則根據(jù)score更新元素
 *
 * @param key
 * @param score
 * @param member
 * @return
 */
public Long zadd(String key, double score, String member) {
    Jedis jedis = getJedis();
    return jedis.zadd(key, score, member);
}

/**
 * 通過key刪除在zset中指定的value
 *
 * @param key
 * @param members 可以 是一個string 也可以是一個string數(shù)組
 * @return
 */
public Long zrem(String key, String... members) {
    Jedis jedis = getJedis();
    return jedis.zrem(key, members);
}

/**
 * 通過key增加該zset中value的score的值
 *
 * @param key
 * @param score
 * @param member
 * @return
 */
public Double zincrby(String key, double score, String member) {
    Jedis jedis = getJedis();
    return jedis.zincrby(key, score, member);
}

/**
 * 通過key返回zset中value的排名
 * 下標(biāo)從小到大排序
 *
 * @param key
 * @param member
 * @return
 */
public Long zrank(String key, String member) {
    Jedis jedis = getJedis();
    return jedis.zrank(key, member);
}

/**
 * 通過key返回zset中value的排名
 * 下標(biāo)從大到小排序
 *
 * @param key
 * @param member
 * @return
 */
public Long zrevrank(String key, String member) {
    Jedis jedis = getJedis();
    return jedis.zrevrank(key, member);
}

/**
 * 通過key將獲取score從start到end中zset的value
 * socre從大到小排序
 * 當(dāng)start為0 end為-1時返回全部
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public Set<String> zrevrange(String key, long start, long end) {
    Jedis jedis = getJedis();
    return jedis.zrevrange(key, start, end);
}

/**
 * 通過key返回指定score內(nèi)zset中的value
 *
 * @param key
 * @param max
 * @param min
 * @return
 */
public Set<String> zrangebyscore(String key, String max, String min) {
    Jedis jedis = getJedis();
    return jedis.zrevrangeByScore(key, max, min);
}

/**
 * 通過key返回指定score內(nèi)zset中的value
 *
 * @param key
 * @param max
 * @param min
 * @return
 */
public Set<String> zrangeByScore(String key, double max, double min) {
    Jedis jedis = getJedis();
    return jedis.zrevrangeByScore(key, max, min);
}

/**
 * 返回指定區(qū)間內(nèi)zset中value的數(shù)量
 *
 * @param key
 * @param min
 * @param max
 * @return
 */
public Long zcount(String key, String min, String max) {
    Jedis jedis = getJedis();
    return jedis.zcount(key, min, max);
}

/**
 * 通過key返回zset中的value個數(shù)
 *
 * @param key
 * @return
 */
public Long zcard(String key) {
    Jedis jedis = getJedis();
    return jedis.zcard(key);
}

/**
 * 通過key獲取zset中value的score值
 *
 * @param key
 * @param member
 * @return
 */
public Double zscore(String key, String member) {
    Jedis jedis = getJedis();
    return jedis.zscore(key, member);
}

/**
 * 通過key刪除給定區(qū)間內(nèi)的元素
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public Long zremrangeByRank(String key, long start, long end) {
    Jedis jedis = getJedis();
    return jedis.zremrangeByRank(key, start, end);
}

/**
 * 通過key刪除指定score內(nèi)的元素
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public Long zremrangeByScore(String key, double start, double end) {
    Jedis jedis = getJedis();
    return jedis.zremrangeByScore(key, start, end);
}

/**
 * 返回滿足pattern表達式的所有key
 * keys(*)
 * 返回所有的key
 *
 * @param pattern
 * @return
 */
public Set<String> keys(String pattern) {
    Jedis jedis = getJedis();
    return jedis.keys(pattern);
}

/**
 * 通過key判斷值得類型
 *
 * @param key
 * @return
 */
public String type(String key) {
    Jedis jedis = getJedis();
    return jedis.type(key);
}


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

private Jedis getJedis() {
    return pool.getResource();
}

public static RedisUtil getRedisUtil() {
    return new RedisUtil();
}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評論 19 139
  • NOSQL類型簡介鍵值對:會使用到一個哈希表,表中有一個特定的鍵和一個指針指向特定的數(shù)據(jù),如redis,volde...
    MicoCube閱讀 4,156評論 2 27
  • 本周學(xué)習(xí)主題是:第一講 在哈佛學(xué)校畢業(yè)典禮上的演講。本章的主題是如何才能得到幸福?運用反過來想,總是反過來想,來實...
    oseca閱讀 332評論 1 4
  • 作者 王明亮 日期 2017-7-17 閑言碎語不多敘, 說一段《小伙兒買雞記》。 小伙兒今年三十一, 清早出門兒...
    明O亮閱讀 480評論 0 0

友情鏈接更多精彩內(nèi)容