常用資料結構 redis簡單操作
public class Test1 {
@Test
public void test() {
Jedis jedis = new Jedis("192.168.1.96");
String zml = jedis.get("zml");
System.out.println(zml);
if(zml==null){
jedis.set("zml", "zhumeilu");
}
System.out.println(jedis.get("zml"));
}
}
複製程式碼
redis池化
配置檔案
#最大分配的物件數
redis.pool.maxActive=1024
#最大能夠保持idel狀態的物件數
redis.pool.maxIdle=200
#當池內沒有返回物件時,最大等待時間
redis.pool.maxWait=1000
#當呼叫borrow Object方法時,是否進行有效性檢查
redis.pool.testOnBorrow=true
#當呼叫return Object方法時,是否進行有效性檢查
redis.pool.testOnReturn=true
#IP
redis.ip=192.168.1.96
#Port
redis.port=6379
建立一個redis池工具類
public class RedisClient {
private static JedisPool pool;
static{
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if(bundle==null){
throw new RuntimeException("reids.propertis為空");
}
System.out.println(bundle);
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
config.setMaxWait(Integer.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"));
}
public static JedisPool getPool() {
return pool;
}
}
複製程式碼
通過JedisPool獲取jedis例項
public class TestPool {
@Test
public void test() {
JedisPool pool = RedisClient.getPool();
Jedis jedis = pool.getResource();
System.out.println(jedis);
String zml = jedis.get("zml");
System.out.println(zml);
pool.returnResource(jedis);
}
}
複製程式碼