Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 高性能Java数据库缓存  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
作者 memcached是什么?
liuaike



版主


发贴: 20
于 2008-07-24 13:42 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Memcached是什么?
Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
Memcached由Danga Interactive开发,用于提升LiveJournal.com访问速度的。LJ每秒动态页面访问量几千次,用户700万。Memcached将数据库负载大幅度降低,更好的分配资源,更快速访问。

如何使用memcached-Server端?
在服务端运行:
# ./memcached -d -m 2048 -l 10.0.0.40 -p 11211
这将会启动一个占用2G内存的进程,并打开11211端口用于接收请求。由于32位系统只能处理4G内存的寻址,所以在大于4G内存使用PAE的32位服务器上可以运行2-3个进程,并在不同端口进行监听。

如何使用memcached-Client端?(Java版是这样的,参看http://www.whalin.com/memcached/#download)
String serverStr = "218.241.154.12:12321";
String[] serverlist = {serverStr};
//String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
//Integer[] weights = { new Integer(5), new Integer(2) };
int initialConnections = 100;
int minSpareConnections = 50;
int maxSpareConnections = 500;
long maxIdleTime = 1000 * 60 * 30; // 30 minutes
long maxBusyTime = 1000 * 60 * 5; // 5 minutes
long maintThreadSleep = 1000 * 5; // 5 seconds
int socketTimeOut = 1000 * 3; // 3 seconds to block on reads
//int socketConnectTO = 1000 * 3; // 3 seconds to block on initial connections. If 0, then will use blocking connect (default)
//boolean failover = false; // turn off auto-failover in event of server down
boolean nagleAlg = false; // turn off Nagle's algorithm on all sockets in pool
//boolean aliveCheck = false; // disable health check of socket on checkout

pool = SockIOPool.getInstance("mymemcache");
pool.setServers( serverlist );
//pool.setWeights( weights );
pool.setInitConn( initialConnections );
pool.setMinConn( minSpareConnections );
pool.setMaxConn( maxSpareConnections );
pool.setMaxIdle( maxIdleTime );
pool.setMaxBusyTime( maxBusyTime );
pool.setMaintSleep( maintThreadSleep );
pool.setSocketTO( socketTimeOut );
pool.setNagle( nagleAlg );
pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );
pool.setAliveCheck( true );
pool.initialize();

mCachedClient = new MemCachedClient( "mymemcache" );
mCachedClient.setCompressEnable( false );
mCachedClient.setCompressThreshold(4096);
然后用mCachedClient的set/get/delete方法就可以了。memcached的吞吐量每秒大概能get两万次左右,这比mysql的select提高了好几倍,所以很多网站都用这个来做缓存,如豆瓣。

我在系统种也用了memcached,我的建议是在有分布式的时候才考虑用memcached,如果只有一台应用服务器就没有必要用memcached,毕竟Memcached的吞吐量还是有限,本地HashMap每秒可以get一百万次,用本地HashMap做缓存才是最快的。用了分布式后,缓存同步和分布式session都是比较难处理的问题,所以建议1000万pv/天以下的应用不要用分布式。



作者 Re:memcached是什么? [Re:liuaike]
lcwling

Java咋就这么难呢?!

CJSDN高级会员


发贴: 367
于 2008-07-28 23:15 user profilesend a private message to usersend email to lcwlingreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liuaike wrote:
用了分布式后,缓存同步和分布式session都是比较难处理的问题,所以建议1000万pv/天以下的应用不要用分布式。


一般来说读多写少,即数据库更新不太频繁的数据,是很适合做memcached;另外,附带说一下memcached的key & value的限制:
key:250个字符;value:1M的对象

最近一直在思考,如何才能解决数据库“写”的瓶颈,读可以采取很多种手段来cache,如果写的并发很大的时候,如订单的集中提交,改如何优化呢?



认真对待每一天吧,因为生活每天都是新的!

reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923