memcached使用詳解

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   for   ar   

    最近為瞭解決系統效能問題,研究了一下memcache。由於其可以獨立部署不佔用JVM資源,其優越性可得而知,更由於memcached支援分布式部署這也使得可以實現超大記憶體伺服器。具體memcache是哪個高手之作,再次不在贅述。

    memcached的分布式有個奇怪的特點是其分布式的方式有用戶端決定,所以用戶端演算法顯得格外重要(hash演算法 主流consist),筆者在此不在多說,有興趣的可以下去研究。下面開始講如何使用(後續會把原始碼上傳)。

1. 建立串連池設定相應參數

  

public class MemcacheClientBean {    static MemCachedClient mClient;    static {        // 擷取socket串連池的執行個體對象        SockIOPool ioPool = SockIOPool.getInstance();        // 伺服器列表和其權重        String[] servers = { "192.168.6.40:11211", "192.168.6.40:11212" };        Integer[] weights = { 3 };        // 設定伺服器資訊        ioPool.setServers(servers);        ioPool.setWeights(weights);        // 設定初始串連數、最小和最大串連數以及最大處理時間        ioPool.setInitConn(5);        ioPool.setMinConn(5);        ioPool.setMaxConn(250);        ioPool.setMaxIdle((1000 * 60 * 60L));        // 設定主線程的睡眠時間,單位為秒        ioPool.setMaintSleep(30);        ioPool.setNagle(false);        // 串連建立後的逾時時間        ioPool.setSocketTO(3000);        // 串連建立時的逾時時間        ioPool.setSocketConnectTO(0);        // 初始化串連池        ioPool.initialize();    }    public static MemCachedClient getClient() {        mClient = new MemCachedClient();        mClient.setPrimitiveAsString(true);        return mClient;    }}

不管是DB還是緩衝,只要涉及到串連數及資源的限制設定都會涉及到串連池,所有請求由串連池統一管理。

2. 使用memcache常用方法 set get add replace等等

    

public class MClientTest {    MemCachedClient mClient;    boolean isOK = false;    @Before    public void init() {        mClient = MemcacheClientBean.getClient();    }    @Test    public void testSetCache() {        isOK = mClient.set("test", "不錯");        System.out.println(isOK ? ">>>>>>>>>>>>>>set ok" : ">>>>>>>>>>>>>>set fail");    }    @Test    public void testGetCache() {        System.out.println(mClient.get("test"));    }    @Test    public void testAddCache() {//        isOK = mClient.add("test", "你好");        isOK = mClient.add("test2", "你好");        System.out.println(isOK ? ">>>>>>>>>>>>>>add ok" : ">>>>>>>>>>>>>>add fail");    }    @Test    public void testReplaceCache() {        isOK = mClient.replace("test2", "我很好");        System.out.println(isOK ? ">>>>>>>>>>>>>>replace ok" : ">>>>>>>>>>>>>>replace fail");    }    @Test    public void testFlushCache() {        String[] caches = new String[10];        caches[0] = new String("test");        isOK = mClient.flushAll();        System.out.println(isOK ? ">>>>>>>>>>>>>>flush ok" : ">>>>>>>>>>>>>>flush fail");    }    @Test    public void testFlushAllCache() {        isOK = mClient.flushAll();        System.out.println(isOK ? ">>>>>>>>>>>>>>flush all ok" : ">>>>>>>>>>>>>>flush all fail");    }}

基本使用就是這樣,是不是很easy!後續會講解memcache如何跟spring結合使用!

代碼地址: http://files.cnblogs.com/dmc-tec/memcache-demo.rar

    

聯繫我們

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