Memcached Java使用

來源:互聯網
上載者:User

標籤:memcached

一、Memcached安裝     下載Memcached的zip包,cmd切換到解壓後的目錄下     管理員身份運行:安裝:memcached.exe -d install                               啟動:memcached.exe -d start                                    關閉:memcached.exe -d stop                                    卸載:memcached.exe -d uninstall
三、Memcached使用
    1、 匯入java_memcached-release_x.x.x.zip包下的幾個jar包(具體包先自行下載,稍後給出地址)    2、代碼
package com.liupeng.controller;import java.util.Date;import com.danga.MemCached.MemCachedClient;import com.danga.MemCached.SockIOPool;import com.liupeng.domain.Person;public class TestMemcached {    public static MemCachedClient mcc = new MemCachedClient();    public static TestMemcached tc = new TestMemcached();       //設定與快取服務器的串連池    static{        //伺服器列表和其權重         String[] servers={ "127.0.0.1:11211" };         Integer[] weights={3};         SockIOPool pool=SockIOPool. getInstance();         pool.setServers( servers );     //設定伺服器數組資訊         pool.setWeights( weights );     //設定伺服器權重數組         pool.setInitConn(5);            //初始串連數         pool.setMinConn(5);             //最小串連數         pool.setMaxConn(250);           //最大串連數         pool.setMaxIdle(1000*60*60*6);  //最大處理時間         pool.setMaintSleep(30);         //設定主線程的睡眠時間         //設定TCP的參數,連線逾時等         pool.setNagle( false);         pool.setSocketTO(3000);         pool.setSocketConnectTO(0);        //初始化串連池         pool.initialize();        //壓縮設定,超過指定大小(單位為K)的資料都會被壓縮         //mcc.setCompressEnable(true);         //mcc.setCompressThreshold(64*1024);     }       /**     * 保護型構造方法,不允許被執行個體化     */    protected TestMemcached(){}       /**     * 擷取唯一執行個體     * @return     */    public static TestMemcached getInstance(){        return tc;    }             /*插入*/    /**     * 添加資料到緩衝中     * @param key     * @param value     * @return     */    public boolean add(String key,Object value){        return mcc.add(key, value);    }    /**     * 添加資料到緩衝中,並設定逾時時間     * @param key     * @param value     * @param expiry     * @return     */    public boolean add(String key, Object value, Date expiry)    {       return mcc.add(key, value, expiry);    }             /*設定*/    /**     * 在緩衝中設定一個鍵的值     * @param key     * @param value     * @return     */    public boolean set(String key,Object value){        return mcc.set(key, value);    }    /**     * 在緩衝中設定一個鍵的值,並設定逾時時間     * @param key     * @param value     * @param expiry     * @return     */    public boolean set(String key ,Object value ,Date expiry){        return mcc.set(key, value, expiry);    }             /*替換*/    /**     * 將某個鍵的值替換成新的值     * @param key     * @param value     * @return     */    public boolean replace(String key, Object value)    {       return mcc.replace(key, value);    }    /**     * 將某個鍵的值替換成新的值,並設定逾時時間     * @param key     * @param value     * @param expiry     * @return     */    public boolean replace(String key, Object value, Date expiry)    {       return mcc.replace(key, value, expiry);    }          /*擷取*/    /**     * 從緩衝中擷取某個鍵的值     * @param key     * @return     */    public Object get(String key)    {       return mcc.get(key);    }       public static void main(String[] args) {        /**********簡單測試***********/        TestMemcached tm = TestMemcached. getInstance();        tm.add( "hello", "1234", new Date( new Date().getTime() + 10000));//10秒到期        while( true){            try {                Thread. sleep(1000);            } catch (Exception e) {                e.printStackTrace();            }            System. out.println(tm.get( "hello"));        }               /*******java bean測試********/        //實體類必須要實現 Serializable介面        Person person = new Person( "liupeng");        TestMemcached tm = TestMemcached.getInstance ();        tm.add( "bean", person);               Person p = (Person)tm.get( "bean");        System. out.println(p.getName());               //修改名稱不影響緩衝中值        p.setName( "xiaoliu");        p = (Person)tm.get( "bean");        System. out.println(p.getName());               p.setName( "xiaoliu");        tm.replace( "bean", p);        System. out.println(p.getName());    }   }









著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Memcached Java使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.