Jedis的简单使用
定义:Jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。
前提先导如Jedis的jar包
1.方式一
普通版的使用:
Jedis jedis =new Jedis("127.0.0.1",6379);//默认情况(使用自己的主机/6379)下不用主机与加端口号
若在本地主机上进行测试,则可以不写主机IP地址,与端口号。
Jedis.close();//关闭连接池
2.方式二
采用连接池的方式编写:用到两个类
JedisPoolConfig与JedisPool
JedisPoolConfig config = new JedisPoolConfig();
config.setXXX();//设置相关的
JedisPool pools = new JedisPool();//实例化一个JedisPool的连接池
Jedis jedis = pools.getResource(config,"127.0.0.1",6379);
jedis.close();//将连接归还连接池
//若想更简便可以将相关的配置到一个properties文件中区去。
3.本人的小结示例,有需要的可以取走哦
public class JedisUntils {
public static Jedis jedis;
public static Jedis getJdedis(){
if(jedis == null) {
InputStream in = JedisUntils.class.getResourceAsStream("/redis.properties");
System.out.println(in);
Properties p =new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
String host = p.getProperty("host");
int port = Integer.parseInt(p.getProperty("port"));
JedisPoolConfig config = new JedisPoolConfig();
JedisPool pool = new JedisPool(config, host, port);
return pool.getResource();
}
return jedis;
}