詳談Jedis連接池的使用
更新時間:2017年05月11日 08:09:30 投稿:jingxian
下面小編就為大家?guī)硪黄斦凧edis連接池的使用。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
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最多有多少個狀態(tài)為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的實現方式,優(yōu)化如下
優(yōu)化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連接池的使用就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
一文詳解SpringBoot中CommandLineRunner接口
Spring Boot的CommandLineRunner接口是一個函數式接口,用于在Spring Boot應用程序啟動后執(zhí)行一些初始化操作,它提供了一個run方法,該方法在應用程序啟動后被調用,本文給大家詳細介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下2023-10-10
SpringCloud 2020-Ribbon負載均衡服務調用的實現
這篇文章主要介紹了SpringCloud 2020-Ribbon負載均衡服務調用的實現,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
springboot整合springsecurity與mybatis-plus的簡單實現
Spring Security基于Spring開發(fā),項目中如果使用Spring作為基礎,配合Spring Security做權限更加方便,而Shiro需要和Spring進行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領域很常用2021-10-10

