memcache安裝和使用,memcache安裝使用

來源:互聯網
上載者:User

memcache安裝和使用,memcache安裝使用

1 memcache會依賴libevent

brew install libevent

2 下載memcache

#wget http://www.memcached.org/files/memcached-1.4.22.tar.gz

然後configure;make;make install安裝

3 啟動:

`sudo ./memcached -p 11211 -m 64 -u shenyb -d➜  soft  ps -ef |grep memcache  501  4859     1   0  9:40上午 ??         0:00.16 ./memcached -p 11211 -m 64 -u shenyb -d`

可以看到memcache已經啟動。
4使用java用戶端串連測試
Java用戶端:
目前主要有兩種用戶端:
<1>. https://github.com/gwhalin/Memcached-Java-Client/
<2>. http://code.google.com/p/spymemcached/會依賴spy.jar
兩種差別不大,只是set時到期時間設定方式不同,效率沒有測試過。
這裡使用第一種:
測試代碼如下:

import java.util.Date;import com.danga.MemCached.MemCachedClient;import com.danga.MemCached.SockIOPool;public class MemcacheUtil {    public static void testDanga() throws Exception {        /* 初始化SockIOPool,管理memcached的串連池 */        String[] servers = { "127.0.0.1:11211" };        SockIOPool pool = SockIOPool.getInstance();        pool.setServers(servers);        pool.setFailover(true);        pool.setInitConn(10);        pool.setMinConn(5);        pool.setMaxConn(250);        // 設定主線程睡眠時間,每30秒蘇醒一次,維持串連池大小        pool.setMaintSleep(30);        // 關閉通訊端緩衝        pool.setNagle(false);        // 串連建立後的逾時時間        pool.setSocketTO(3000);        pool.setAliveCheck(true);        pool.initialize();        /* 建立MemcachedClient執行個體 */        MemCachedClient memCachedClient = new MemCachedClient();        long start = System.currentTimeMillis();        for (int i = 0; i < 1000; i++) {            memCachedClient.set(i + "", "hello" + i);        }        for (int i = 0; i < 1000; i++) {            System.out.println(memCachedClient.get(i + ""));        }        long end = System.currentTimeMillis();        System.out.println((end - start));        memCachedClient.set("name1", "test22",                new Date(System.currentTimeMillis() + 3000));        Thread.sleep(2000);        System.out.println(memCachedClient.get("name1"));    }    public static void main(String[] args) {        try {            testDanga();        } catch (Exception e) {            e.printStackTrace();        }    }}

關於設定到期時間:

 client.set(key, value, new Date(expireTime));

但這個時間如何設定,還是有一點區別,比如設定10分鐘後到期,是應該設定date為System.currentTimeInMillis()+10*60*1000
還是10*60*1000
服務端是兩種方式都相容的,一個是多少秒後到期,一個是什麼時候到期,
但後者因為設定時間是在用戶端,儲存在服務端,假如兩台伺服器時間差別很大,就會導致資料的到期時間和我要求的時間點不符合。

最後,memcache一般用來儲存不怎麼變化的資料,比如網站欄目資訊,商品的屬性資訊,網站頁尾等。

聯繫我們

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