a simple memcached client Demo

本文介绍了在高并发场景下采用memcached作为缓存解决方案的过程。包括memcached客户端的两种Java实现方式,以及通过示例代码展示如何设置连接池参数并进行基本的缓存操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在当前的一个语音系统中,需要频繁进行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在这方面的处理, 居然没发现这方面的东东, 哪位兄台能否提供一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值