Jedis连接Redis单机版

分手后的思念是犯贱 2022-05-17 01:04 297阅读 0赞

连接单机版

把jedis依赖的jar包
添加到工程中

  1. //单机版测试
  2. @Test
  3. public void testJedisSingle() throws Exception {
  4. //创建一个Jedis对象
  5. Jedis jedis = new Jedis("192.168.25.153", 6379);
  6. jedis.set("test", "hello jedis");
  7. String string = jedis.get("test");
  8. System.out.println(string);
  9. jedis.close();
  10. }

使用连接池

  1. //使用连接池
  2. @Test
  3. public void testJedisPool() throws Exception {
  4. //创建一个连接池对象
  5. //系统中应该是单例的。
  6. JedisPool jedisPool = new JedisPool("192.168.25.153", 6379);
  7. //从连接池中获得一个连接
  8. Jedis jedis = jedisPool.getResource();
  9. String result = jedis.get("test");
  10. System.out.println(result);
  11. //jedis必须关闭
  12. jedis.close();
  13. //系统关闭时关闭连接池
  14. jedisPool.close();
  15. }

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读