liuaike
版主
发贴: 20
|
于 2008-07-24 13:42
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/天以下的应用不要用分布式。
|