a simple memcached client Demo

在當前的一個語音系統中,需要頻繁進行I/O操作, 爲了應付未來可能出現的高併發訪問, 計劃引入緩存機制。
在諸多緩存中, ehcache口碑不錯, 我之前也寫過一篇文章介紹ehcache的使用:
http://lcllcl987.iteye.com/blog/222693

 但ehcache不是一個分佈式緩存解決方案。
所以, 就初選memcached了:
http://www.danga.com/memcached/
 
memcached的安裝交給google,略過不表。
 

下面是兩個memcached client的java實現:
http://www.whalin.com/memcached/
http://code.google.com/p/spymemcached/


http://www.whalin.com/memcached/
爲例, 它的文檔不錯, 就不多說了。 test如下:

package co.cache;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MyMemCachedClient
{
    // create a static client as most installs only need
    // a single instance
    private static MemCachedClient memCachedClient = new MemCachedClient();

    // set up connection pool once at class load
    static
    {
        // server list and weights
        String[] servers = {"192.168.0.55:11211"};

        Integer[] weights = {3};

        // grab an instance of our connection pool
        SockIOPool pool = SockIOPool.getInstance();

        // set the servers and the weights
        pool.setServers(servers);
        pool.setWeights(weights);
        // set some basic pool settings
        // 5 initial, 5 min, and 250 max conns
        // and set the max idle time for a conn
        // to 6 hours
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 60 * 60 * 6);

        // set the sleep for the maint thread
        // it will wake up every x seconds and
        // maintain the pool size
        pool.setMaintSleep(30);

        // set some TCP settings
        // disable nagle
        // set the read timeout to 3 secs
        // and don't set a connect timeout
        pool.setNagle(false);
        pool.setSocketTO(3000);
        pool.setSocketConnectTO(0);

        // initialize the connection pool
        pool.initialize();

        // lets set some compression on for the client
        // compress anything larger than 64k
        memCachedClient.setCompressEnable(true);
        memCachedClient.setCompressThreshold(64 * 1024);
    }
   
    public static MemCachedClient getMemCachedClient()
    {
        return memCachedClient;
    }

    // from here on down, you can call any of the client calls
    public static void examples()
    {
        memCachedClient.set("foo", "This is a test String");
        String bar = (String) memCachedClient.get("foo");
    }
   
    public static void main(String[] args)
    {
        MyMemCachedClient.getMemCachedClient().set("foo", "This is a test String.");
        String bar = (String)MyMemCachedClient.getMemCachedClient().get("foo");
        System.out.println(bar);
    }
}
 

 

ehcache有溢出策略, 就是分配的內存滿了, 可以持久化到硬盤。

找了一下memcached在這方面的處理, 居然沒發現這方面的東東, 哪位兄臺能否提供一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章