1. 程式人生 > >redis學習之Jedis使用執行緒池封裝redis的基本操作及spring的簡單封裝

redis學習之Jedis使用執行緒池封裝redis的基本操作及spring的簡單封裝

今天是521,作為單身狗屌絲一枚,還是像往常一樣沒任何感覺,好悲哀。不多說,進入今天的redis學習之Jedis使用執行緒池封裝redis的基本操作及spring的簡單封裝。例子都是整理好的,供工作學習只需

一、Jedis使用執行緒池封裝redis的基本操作

redis客戶端jedis常用的操作:key  value,hash,list,set,zset的基本操作;

package util;

import java.util.List;

import java.util.Map;

import java.util.Set;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.BinaryClient.LIST_POSITION;

/**

* <p>redis通用工具類</p>

*

*/

publicclass RedisUtil {

private JedisPoolpool =null;

/**

* <p>傳入ip和埠號構建redis連線池</p>

* @param

ipip

* @param prot

*/

public RedisUtil(String ip,int prot) {

if (pool ==null) {

JedisPoolConfig config = new JedisPoolConfig();

//控制一個pool可分配多少個jedis例項,通過pool.getResource()來獲取;

//如果賦值為-1,則表示不限制;如果pool已經分配了maxActivejedis例項,則此時pool的狀態為exhausted(耗盡)

config.setMaxActive(500);

//控制一個pool最多有多少個狀態為

idle(空閒的)jedis例項。

config.setMaxIdle(5);

//表示當borrow(引入)一個jedis例項時,最大的等待時間,如果超過等待時間,則直接丟擲JedisConnectionException

config.setMaxWait(1000 * 100);

//borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的;

config.setTestOnBorrow(true);

// pool =new JedisPool(config, "192.168.161.130", 6379, 100000);

pool =new JedisPool(config, ip, prot, 100000);

}

}

/**

* <p>通過配置物件 ip 構建連線池</p>

* @param config配置物件

* @param ipip

* @param prot

*/

public RedisUtil(JedisPoolConfig config ,String ip,int prot){

if (pool ==null) {

pool =new JedisPool(config,ip,prot,10000);

}

}

/**

* <p>通過配置物件 ip 超時時間構建連線池</p>

* @param config配置物件

* @param ipip

* @param prot

* @param timeout超時時間

*/

public RedisUtil(JedisPoolConfig config ,String ip,int prot ,int timeout){

if (pool ==null) {

pool =new JedisPool(config,ip,prot,timeout);

}

}

/**

* <p>通過連線池物件構建一個連線池</p>

* @param pool連線池物件

*/

public RedisUtil(JedisPool pool){

if (this.pool == null) {

this.pool = pool;

}

}

/**

* <p>通過key獲取儲存在redis中的value</p>

* <p>並釋放連線</p>

* @param key

* @return成功返回value失敗返回null

*/

public String get(String key){

Jedis jedis = null;

String value = null;

try {

jedis = pool.getResource();

value = jedis.get(key);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return value;

}

/**

* <p>redis存入keyvalue,並釋放連線資源</p>

* <p>如果key已經存在則覆蓋</p>

* @param key

* @param value

* @return成功返回OK失敗返回 0

*/

public String set(String key,String value){

Jedis jedis = null;

try {

jedis = pool.getResource();

return jedis.set(key, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

return"0";

} finally {

returnResource(pool, jedis);

}

}

/**

* <p>刪除指定的key,也可以傳入一個包含key的陣列</p>

* @param keys一個key 也可以使 string陣列

* @return返回刪除成功的個數

*/

public Long del(String...keys){

Jedis jedis = null;

try {

jedis = pool.getResource();

return jedis.del(keys);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

return 0L;

} finally {

returnResource(pool, jedis);

}

}

/**

* <p>通過key向指定的value值追加值</p>

* @param key

* @param str

* @return成功返回新增後value的長度失敗返回新增的 value 的長度異常返回0L

*/

public Long append(String key ,String str){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.append(key, str);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

return 0L;

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>判斷key是否存在</p>

* @param key

* @return true OR false

*/

public Boolean exists(String key){

Jedis jedis = null;

try {

jedis = pool.getResource();

return jedis.exists(key);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

returnfalse;

} finally {

returnResource(pool, jedis);

}

}

/**

* <p>設定key value,如果key已經存在則返回0,nx==> not exist</p>

* @param key

* @param value

* @return成功返回1如果存在發生異常返回 0

*/

public Long setnx(String key ,String value){

Jedis jedis = null;

try {

jedis = pool.getResource();

return jedis.setnx(key, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

return 0L;

} finally {

returnResource(pool, jedis);

}

}

/**

* <p>設定key value並制定這個鍵值的有效期</p>

* @param key

* @param value

* @param seconds單位:

* @return成功返回OK失敗和異常返回null

*/

public String setex(String key,String value,int seconds){

Jedis jedis = null;

String res = null;

try {

jedis = pool.getResource();

res = jedis.setex(key, seconds, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過key offset 從指定的位置開始將原先value替換</p>

* <p>下標從0開始,offset表示從offset下標開始替換</p>

* <p>如果替換的字串長度過小則會這樣</p>

* <p>example:</p>

* <p>value : [email protected]</p>

* <p>str :abc</p>

* <P>從下標7開始替換則結果為</p>

* <p>RES : bigsea.abc.cn</p>

* @param key

* @param str

* @param offset下標位置

* @return返回替換後value的長度

*/

public Long setrange(String key,String str,int offset){

Jedis jedis = null;

try {

jedis = pool.getResource();

return jedis.setrange(key, offset,str);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

return 0L;

} finally {

returnResource(pool, jedis);

}

}

/**

* <p>通過批量的key獲取批量的value</p>

* @param keys string陣列也可以是一個key

* @return成功返回value的集合,失敗返回null的集合 ,異常返回空

*/

public List<String> mget(String...keys){

Jedis jedis = null;

List<String> values = null;

try {

jedis = pool.getResource();

values = jedis.mget(keys);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return values;

}

/**

* <p>批量的設定key:value,可以一個</p>

* <p>example:</p>

* <p>obj.mset(newString[]{"key2","value1","key2","value2"})</p>

* @param keysvalues

* @return成功返回OK失敗異常返回 null

*

*/

public String mset(String...keysvalues){

Jedis jedis = null;

String res = null;

try {

jedis = pool.getResource();

res = jedis.mset(keysvalues);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>批量的設定key:value,可以一個,如果key已經存在則會失敗,操作會回滾</p>

* <p>example:</p>

* <p>obj.msetnx(newString[]{"key2","value1","key2","value2"})</p>

* @param keysvalues

* @return成功返回1失敗返回0

*/

public Long msetnx(String...keysvalues){

Jedis jedis = null;

Long res = 0L;

try {

jedis = pool.getResource();

res =jedis.msetnx(keysvalues);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>設定key的值,並返回一箇舊值</p>

* @param key

* @param value

* @return舊值如果key不存在則返回null

*/

public String getset(String key,String value){

Jedis jedis = null;

String res = null;

try {

jedis = pool.getResource();

res = jedis.getSet(key, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過下標key獲取指定下標位置的 value</p>

* @param key

* @param startOffset開始位置0開始負數表示從右邊開始擷取

* @param endOffset

* @return如果沒有返回null

*/

public String getrange(String key,int startOffset ,int endOffset){

Jedis jedis = null;

String res = null;

try {

jedis = pool.getResource();

res = jedis.getrange(key, startOffset,endOffset);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過key value進行加值+1操作,value不是int型別時會返回錯誤,key不存在是則value1</p>

* @param key

* @return加值後的結果

*/

public Long incr(String key){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.incr(key);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過key給指定的value加值,如果key不存在,則這是value為該值</p>

* @param key

* @param integer

* @return

*/

public Long incrBy(String key,Long integer){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.incrBy(key, integer);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>key的值做減減操作,如果key不存在,則設定key-1</p>

* @param key

* @return

*/

public Long decr(String key) {

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.decr(key);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>減去指定的值</p>

* @param key

* @param integer

* @return

*/

public Long decrBy(String key,Long integer){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.decrBy(key, integer);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過key獲取value值的長度</p>

* @param key

* @return失敗返回null

*/

public Long serlen(String key){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.strlen(key);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過keyfield設定指定的值,如果key不存在,則先建立</p>

* @param key

* @param field欄位

* @param value

* @return如果存在返回0異常返回null

*/

public Long hset(String key,String field,String value) {

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.hset(key, field, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過keyfield設定指定的值,如果key不存在則先建立,如果field已經存在,返回0</p>

* @param key

* @param field

* @param value

* @return

*/

public Long hsetnx(String key,String field,String value){

Jedis jedis = null;

Long res = null;

try {

jedis = pool.getResource();

res = jedis.hsetnx(key, field, value);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {

returnResource(pool, jedis);

}

return res;

}

/**

* <p>通過key同時設定 hash的多個field</p>

* @param key

* @param hash

* @return返回OK異常返回null

*/

public String hmset(String key,Map<String, String> hash){

Jedis jedis = null;

String res = null;

try {

jedis = pool.getResource();

res = jedis.hmset(key, hash);

} catch (Exception e) {

pool.returnBrokenResource(jedis);

e.printStackTrace();

} finally {