1 package com.java56.redis; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 /** 8 * 測試類 9 * @author user 10 * 11 */ 12 public class JedisTest { 13 14 public static void main(String[] args) { 15 JedisPoolConfig config=new JedisPoolConfig(); // 連接池的配置對象 16 config.setMaxTotal(100); // 設置最大連接數 17 config.setMaxIdle(10); // 設置最大空閑連接數 18 19 JedisPool jedisPool=new JedisPool(config,"192.168.1.107",6379); 20 21 Jedis jedis=null; 22 try{ 23 jedis=jedisPool.getResource(); // 獲取連接 24 jedis.auth("123456"); // 設置密碼 25 jedis.set("name", "java56教程網"); // 設置值 26 String value=jedis.get("name"); // 獲取值 27 System.out.println(value); 28 29 }catch(Exception e){ 30 e.printStackTrace(); 31 }finally{ 32 if(jedis!=null){ 33 jedis.close(); 34 } 35 if(jedisPool!=null){ 36 jedisPool.close(); 37 } 38 } 39 } 40 }
運行