java緩衝(4、memcached)

來源:互聯網
上載者:User

標籤:

概述

       之前學習.net緩衝的時候,已經講過memcached,對於其叢集、並發和記憶體回收等問題,都講過,這裡不再贅述,有興趣的可以看這篇部落格:分布式緩衝Memcache和Redis。本篇主要講解是使用java語言操作memcached。


memcached的demo        添加第三方jar

                java_memcached-release.jar;commons-pool.jar;slf4j-api.jar;slf4j-simple.jar(demo)

        程式

package com.tgb.testmemcached;import java.util.Date;import org.junit.Before;import org.junit.Test;import com.danga.MemCached.MemCachedClient;import com.danga.MemCached.SockIOPool;public class TestMemcachedClient {SockIOPool sockIOPool = SockIOPool.getInstance();MemCachedClient memCacheClient = new MemCachedClient();@Beforepublic void init(){String[] servers = {"192.168.24.131:11211"};Integer[] weights ={3};sockIOPool.setServers(servers);sockIOPool.setWeights(weights);sockIOPool.setInitConn(5);sockIOPool.setMinConn(5);sockIOPool.setMaxConn(20);sockIOPool.setMaxIdle(1000*60*60*6);sockIOPool.setMaintSleep(30);sockIOPool.setNagle(false);sockIOPool.setSocketTO(3000);sockIOPool.setSocketConnectTO(0);sockIOPool.initialize();//memCacheClient.setCompressEnable(true);//memCacheClient.setCompressThreshold(64*1024);}@Testpublic void testAdd(){//將資料放入緩衝memCacheClient.set("test1", "test1");//將資料放入緩衝,並設定失效時間Date date = new Date(2000000);memCacheClient.set("test2", "test2",date);System.out.println("添加成功");}@Testpublic void testGet(){String strValue1 = memCacheClient.get("test1").toString();String strValue2 = memCacheClient.get("test2").toString();System.out.println(strValue1 + "   "  + strValue2);}@Testpublic void testDelete(){boolean flag = memCacheClient.delete("test1");System.out.println(flag);String strValue1 = memCacheClient.get("test1").toString();System.out.println(strValue1);}@Testpublic void testAdd2(){//不對key做編碼memCacheClient.setSanitizeKeys(false);memCacheClient.add("[email protected]", "青山");String strName = memCacheClient.get("[email protected]").toString();System.out.println(strName);}}


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

java緩衝(4、memcached)

聯繫我們

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