日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

redis连接池操作

發布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis连接池操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**
* @類描述 redis 工具
* @功能名 POJO
* @author zxf
* @date 2014年11月25日
*/
public final class RedisUtil {

private static JedisPool jedisPool = null;
/**
* 初始化Redis連接池
*/
static {
try {
//加載redis配置文件
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException("[redis.properties] is not found!");
}
int maxActivity = Integer.valueOf(bundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.valueOf(bundle.getString("redis.pool.maxIdle"));
long maxWait = Long.valueOf(bundle.getString("redis.pool.maxWait"));
boolean testOnBorrow = Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"));
boolean onreturn = Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"));

//創建jedis池配置實例
JedisPoolConfig config = new JedisPoolConfig();
//設置池配置項值
config.setMaxActive(maxActivity);
config.setMaxIdle(maxIdle);
config.setMaxWait(maxWait);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(onreturn);
jedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 獲取Jedis實例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 釋放jedis資源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}

/**
* 查詢數據
*/
public String find(String key,String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}

/**
* 查詢特定字符串
*/
public String findSubStr(String key,Integer startOffset,Integer endOffset){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 向緩存中設置字符串內容 新增數據|修改
* @param key key
* @param value value
* @return
* @throws Exception
*/
public static int add(String key,String value) throws Exception{
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}finally{
jedisPool.returnResource(jedis);
}
}

/**
* 刪除緩存中得對象,根據key
* @param key
* @return
*/
public static int del(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}finally{
jedisPool.returnResource(jedis);
}
}

}

轉載于:https://www.cnblogs.com/austinspark-jessylu/p/6088627.html

總結

以上是生活随笔為你收集整理的redis连接池操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。