Java操作Redis

來源:互聯網
上載者:User

標籤:null   redis   ash   port   ret   ati   串連   stack   ack   

使用串連池
 1 public class Test { 2  3     /** 4      * Redis地址 5      */ 6     private static final String ADDR = "10.124.133.184"; 7  8     /** 9      * Redis連接埠10      */11     private static final Integer PORT = 6379;12 13     /**14      * Redis訪問密碼15      */16     private static final String AUTH = "icloud20180514160728";17 18     /**19      * 可用串連執行個體的最大數目,預設值為820      * 如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis執行個體,則此時pool的狀態為exhausted(耗盡)21      */22     private static final Integer MAX_ACTIVE = 1024;23 24     /**25      * 控制一個pool最多有多少個狀態為idle(閒置)的jedis執行個體,預設值也是8。26      */27     private static final Integer MAX_IDLE = 200;28 29     /**30      * 等待可用串連的最大時間,單位毫秒,預設值為-1,表示永不逾時。如果超過等待時間,則直接拋出JedisConnectionException;31      */32     private static int MAX_WAIT = 10000;33 34     private static final Integer TIMEOUT = 10000;35 36     /**37      * 在borrow一個jedis執行個體時,是否提前進行validate操作;如果為true,則得到的jedis執行個體均是可用的38      */39     private static final Boolean TEST_ON_BORROW = true;40 41     private static JedisPool jedisPool = null;42 43 44     /**45      * 初始化Redis串連池46      */47     static {48         try {49             JedisPoolConfig config = new JedisPoolConfig();50             config.setMaxIdle(MAX_IDLE);51             config.setTestOnBorrow(TEST_ON_BORROW);52             jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);53         } catch (Exception e) {54             e.printStackTrace();55         }56     }57 58     /**59      * 擷取Jedis執行個體60      * @return61      */62     private static synchronized Jedis getJedis() {63         try {64             if (jedisPool != null) {65                 return jedisPool.getResource();66             } else {67                 return null;68             }69         } catch (Exception e) {70             e.printStackTrace();71             return null;72         }73     }74     public static void main(String[] args) {75 76         Jedis jedis = null;77         jedis = getJedis();78         if(null != jedis){79             List<String> configList = jedis.configGet("*");80             Map<String, String> confMap = new HashMap<String, String>();81             Integer step = 2;82             for(int i = 0; i < configList.size(); i = i + step){83                 String paramName = configList.get(i);84                 String paramValue = configList.get(i + 1);85                 confMap.put(paramName, paramValue);86             }87             jedis.close();88         }else{89             System.out.println("jedis is null");90         }91     }92 }
不用串連池
    public static void main(String[] args) {        Jedis jedis = null;        jedis = new Jedis(ADDR, PORT);        jedis.auth(AUTH);        Map<String, String> confMap = new HashMap<String, String>();        List<String> configList = jedis.configGet("*");        Integer step = 2;        for(int i = 0; i < configList.size(); i = i + step){            String paramName = configList.get(i);            String paramValue = configList.get(i + 1);            confMap.put(paramName, paramValue);        }        for(Map.Entry entry : confMap.entrySet()) {            System.out.println(entry.getKey() + ": " + entry.getValue());        }        //        jedis.configSet("hash-max-ziplist-entries","256");        System.out.println("hash-max-ziplist-entries: " + jedis.configGet("hash-max-ziplist-entries"));        jedis.close();    }

 

Java操作Redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.