memcache java 安装_memcache安装与使用(二)

使用java调用memcache,获取数据

1,下载客户端

下载java客户端API,实际上是一个jar包。

http://www.danga.com/memcached/apis.bml

memcache支持很多种客户端调用如:Perl、php、python、Ruby、java、C#、C等等

在java中目前支持两种客户端“Java API for memcached”和“Improved Java API for memcached”

我用的是前者,因为它有详细的说明文档

下载http://img.whalin.com/memcached/jdk6/standard/java_memcached-release_2.0.1.jar

2,开始用java程序调用memcached

在eclipse中新建一个工程,将上面下载的java_memcached-release_2.0.1.jar引入工程。

新建java类memcachedTest

import com.danga.MemCached.MemCachedClient;

import com.danga.MemCached.SockIOPool;

public class memcachedTest {

// create a static client as most installs only need

// a single instance

protected static MemCachedClient mcc = new MemCachedClient();

//连接memcache

static{

// set up connection pool once at class load

// server list and weights

String[] servers =

{

"192.168.0.20:11211",

"192.168.0.20:11212"

};

Integer[] weights = { 3,2 };

// 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

mcc.setCompressEnable( true );

mcc.setCompressThreshold( 64 * 1024 );

}

public static void main(String[] a){

for(int i=0;i<1000;i++){

mcc.set( "Test_"+i, "hello world_"+i);

}

//第一次运行讲一下三行注释,用以上面三行进行数据插入,以后运行就将上面三行注释,运行下面三行语句来看执行效果。

System.out.println("dd1="+(String)mcc.get( "Test_967"));

System.out.println("dd2="+(String)mcc.get( "Test_984"));

System.out.println("dd3="+(String)mcc.get( "Test_981"));

System.out.println("End");

}

}

网上参考资料:

http://bbs.linuxpk.com/thread-13497-1-1.html

http://www.ccvita.com/257.html

http://www.danga.com/memcached/apis.bml

http://www.whalin.com/memcached/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用好Cache,优化应用 服务集成平台5.6的性能测试进入尾声,这期的优化也算告一段落。这次主要的优化工作还是在三个方面:应用服务器(Apache,JBoss)配置,业务流程,Cache Client包(http://code.google.com/p/memcache-client-forjava/ )。这里把过去和这次优化对于Cache的使用作一个经验分享,希望大家能够用好Cache,提速你的应用。 这里还是通过一些点滴的启示来介绍优化的一些心得,很多时候还是要根据具体情况来判断如何去具体实施,因此这里所说的仅仅是在一些场景下适用,并非放之四海皆准的教条。同时也希望看此文的各位同学,如果有更好的思路可以给我反馈,技术在交流中才会有发展。 积少成多,集腋成裘 性能提不上去,多半是在一些容易成为瓶颈的“暗点”(IO,带宽,连接数,资源竞争等等)。Memcached Cache现在已经被大家广泛使用,但是千万不要认为对Cache的操作是低损耗的,要知道这类集中式Cache对Socket连接数(会牵涉到linux操作系统文件句柄可用数),带宽,网络IO都是有要求的,有要求就意味着会有损失,因此积少成多,集腋成裘。服务集成平台是一个高速的服务路由器,其大部分的业务数据,访问控制策略,安全策略以及对应的一些控制阀值被缓存在Cache服务端,因此对于Cache的依赖性很强。每一次对于客户端的性能提升,总会给服务集成平台性能带来不小的影响,但是每一次优化速度后,客户端可以优化的空间越来越小,这时候需要一些策略来配合,提升应用整体性能。当前主要采用了以下几点策略:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值