常用数据结构 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);
}
}