今天就跟大家聊聊有關如何正確的使用Jedis連接池,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1、構建redis連接池,返還到連接池
private static JedisPool jedisPool = null;
private static Jedis jedis;
static {
jedis = getJedisPool().getResource();
}
/**
* 構建redis連接池
*/
public static JedisPool getJedisPool() {
if (jedisPool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(1024); // 可用連接實例的最大數目,如果賦值為-1,表示不限制.
config.setMaxIdle(5); // 控制一個Pool最多有多少個狀態為idle(空閑的)jedis實例,默認值也是8
config.setMaxWaitMillis(1000 * 100); // 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時/如果超過等待時間,則直接拋出異常
config.setTestOnBorrow(true); // 在borrow一個jedis實例時,是否提前進行validate操作,如果為true,則得到的jedis實例均是可用的
jedisPool = new JedisPool(config, "127.0.0.1", 6379);
}
return jedisPool;
}
/**
* 釋放jedis資源
*/
public static void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}2、 jedis使用
典型的jedis使用方法
public static String get(String key) {
String value = null;
Jedis jedis = null;
try {
JedisPool pool = getJedisPool();
jedis = pool.getResource();
value = jedis.get(key);
}
catch (Exception e) {
returnResource(jedis);
e.printStackTrace();
}
finally {
returnResource(jedis);
}
return value;
}這種寫法會經常忘記返回jedis到pool.參考Spting JdbcTemplate的實現方式,優化如下
優化jedis使用方法
public static String getByTemplate(final String key) {
RedisTemplate redisTemplate = new RedisTemplate(getJedisPool());
String value = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String handle(Jedis jedis) {
return jedis.get(key);
}
});
return value;
}RedisTemplate封裝了從JedisPool中取jedis以及返回池中
public class RedisTemplate {
private JedisPool jedisPool;
public RedisTemplate(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
public <T> T execute(RedisCallback<T> callback) {
Jedis jedis = jedisPool.getResource();
try {
return callback.handle(jedis);
}
catch (Exception e) {
// throw your exception
throw e;
}
finally {
returnResource(jedis);
}
}
private void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
public interface RedisCallback<T> {
public T handle(Jedis jedis);
}常用的jedis方法
字符串
@Test
public void testString() {
jedis.set("name", "webb"); // 添加數據
System.out.println("name -> " + jedis.get("name"));
jedis.append("name", " , javaer"); // 拼接
System.out.println("name -> " + jedis.get("name"));
jedis.del("name"); // 刪除數據
System.out.println("name -> " + jedis.get("name"));
jedis.mset("name", "webb", "age", "24"); // 設置多個鍵值對
jedis.incr("age"); // 進行加1操作
System.out.println("name -> " + jedis.get("name") + ", age -> " + jedis.get("age"));
}列表
@Test
public void testList() {
String key = "java framework";
jedis.lpush(key, "spring");
jedis.lpush(key, "spring mvc");
jedis.lpush(key, "mybatis");
System.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有
jedis.del(key);
jedis.rpush(key, "spring");
jedis.rpush(key, "spring mvc");
jedis.rpush(key, "mybatis");
System.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有
System.out.println(jedis.llen(key)); // 列表長度
System.out.println(jedis.lrange(key, 0, 3));
jedis.lset(key, 0 , "redis"); // 修改列表中單個值
System.out.println(jedis.lindex(key, 1)); // 獲取列表指定下標的值
System.out.println(jedis.lpop(key)); // 列表出棧
System.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有
}散列
@Test
public void testMap() {
String key = "user";
Map<String, String> map = new HashMap<>();
map.put("name", "webb");
map.put("age", "24");
map.put("city", "hangzhou");
jedis.hmset(key, map); // 添加數據
List<String> rsmap = jedis.hmget(key, "name", "age", "city"); // 第一個參數存入的是redis中map對象的key,后面跟的是放入map中的對象的key
System.out.println(rsmap);
jedis.hdel(key, "age"); // 刪除map中的某個鍵值
System.out.println(jedis.hmget(key, "age"));
System.out.println(jedis.hlen(key)); // 返回key為user的鍵中存放的值的個數
System.out.println(jedis.exists(key)); // 是否存在key為user的記錄
System.out.println(jedis.hkeys(key)); // 返回map對象中的所有key
System.out.println(jedis.hvals(key)); // 返回map對象中所有的value
Iterator<String> iterator = jedis.hkeys("user").iterator();
while (iterator.hasNext()) {
String key2 = iterator.next();
System.out.print(key2 + " : " + jedis.hmget("user", key2) + "\n");
}
}集合
@Test
public void testSet() {
String key = "userSet";
String key2 = "userSet2";
jedis.sadd(key, "webb");
jedis.sadd(key, "webb");
jedis.sadd(key, "lebo");
jedis.sadd(key, "lebo0425");
jedis.sadd(key, "who");
jedis.srem(key, "who"); // 刪除
System.out.println(jedis.smembers(key)); // 獲取所有加入的value
System.out.println(jedis.sismember(key, "who")); // 判斷value是否在集合中
System.out.println(jedis.srandmember(key)); // 隨機返回一個value
System.out.println(jedis.scard(key)); // 返回集合的元素個數
jedis.sadd(key2, "webb");
jedis.sadd(key2, "ssq");
System.out.println(jedis.sinter(key, key2)); // 交集
System.out.println(jedis.sunion(key, key2)); // 并集
System.out.println(jedis.sdiff(key, key2)); // 差集
}有序集合
@Test
public void testSortedSet() {
String key = "sortedSet";
jedis.zadd(key, 1999, "one");
jedis.zadd(key, 1994, "two");
jedis.zadd(key, 1998, "three");
jedis.zadd(key, 2000, "four");
jedis.zadd(key, 2017, "five");
Set<String> setValues = jedis.zrange(key, 0 , -1); // score從小到大
System.out.println(setValues);
Set<String> setValues2 = jedis.zrevrange(key, 0, -1); // score從大到小
System.out.println(setValues2);
System.out.println(jedis.zcard(key)); // 元素個數
System.out.println(jedis.zscore(key, "three")); // 元素下標
System.out.println(jedis.zrange(key, 0, -1)); // 集合子集
System.out.println(jedis.zrem(key, "five")); // 刪除元素
System.out.println(jedis.zcount(key, 1000, 2000)); // score在1000-2000內的元素個數
}看完上述內容,你們對如何正確的使用Jedis連接池有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。