Redis學習(5)-Jedis(Java操作redis資料庫技術)

來源:互聯網
上載者:User

標籤:print   擷取   工具   get   配置   res   final   unit   lin   

 Java串連redis

一,匯入jar包

Redis有什麼命令,Jedis就有什麼方法設定防火牆

在Linux上面運行如下代碼:

單一實例:Jedis執行個體:

package com.jedis.demo;import org.junit.Test;import redis.clients.jedis.Jedis;public class Demo1 {    /*     * 單一實例串連redis資料庫     * */    @Test    public void run()    {        //參數:ip地址,連接埠號碼        Jedis jedis=new Jedis("192.168.239.137",6379);        jedis.set("name","張三");        System.out.println("name的值為:"+jedis.get("name"));    }}

Jedis串連池

/*     * Jedis串連池     * */    @Test    public void run2()    {        //1,設定串連池設定物件        JedisPoolConfig config=new JedisPoolConfig();        //設定池中最大串連數量【可選】        config.setMaxTotal(50);        //設定空閑時池中保有的最大串連數【可選】        config.setMaxIdle(10);        //設定連線物件        JedisPool pool=new JedisPool(config,"192.168.239.137",6379);        //池中擷取連線物件        Jedis jedis=pool.getResource();                System.out.println("name的值為:"+jedis.get("name"));        //串連歸還池中        jedis.close();    }

 

抽取串連池工具

為了方便使用串連池,抽取串連池工具:

package com.jedis.demo;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisUtill {    //定義一個串連池對象()    private final static  JedisPool POOL;        //靜態代碼塊裡面初始化串連池對象    static {        //1,設定串連池設定物件        JedisPoolConfig config=new JedisPoolConfig();        //設定池中最大串連數量【可選】        config.setMaxTotal(50);        //設定空閑時池中保有的最大串連數【可選】        config.setMaxIdle(10);        //設定連線物件        POOL=new JedisPool(config,"192.168.239.137",6379);    }        /*     * 從池中擷取串連     * */    public static Jedis    getJedis()    {        return POOL.getResource();     }}

 

Redis學習(5)-Jedis(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.