??xml version="1.0" encoding="utf-8" standalone="yes"?>
java memcached client源码Qȝ代码量还是很的
主要是如下两个类:
MemcachedClient.java
SockIOPool.java
?先看推荐的测试代?
* Copyright (c) 2008 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author greg whalin <greg@meetup.com>
*/
package com.meetup.memcached.test;
import com.meetup.memcached.*;
import org.apache.log4j.*;
public class TestMemcached {
public static void main(String[] args) {
// memcached should be running on port 11211 but NOT on 11212
BasicConfigurator.configure();
String[] servers = { "localhost:11211"};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers( servers );
pool.setFailover( true );
pool.setInitConn( 10 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaintSleep( 30 );
//q是开启一个nagle 法。改法避免|络中充塞小包Q提高网l的利用?nbsp;
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setAliveCheck( true );
pool.initialize();
MemcachedClient mcc = new MemcachedClient();
// turn off most memcached client logging:
com.meetup.memcached.Logger.getLogger( MemcachedClient.class.getName() ).setLevel( com.meetup.memcached.Logger.LEVEL_WARN );
for ( int i = 0; i < 10; i++ ) {
boolean success = mcc.set( "" + i, "Hello!" );
String result = (String)mcc.get( "" + i );
System.out.println( String.format( "set( %d ): %s", i, success ) );
System.out.println( String.format( "get( %d ): %s", i, result ) );
}
}
* Initializes the pool.
*/
public void initialize() {
synchronized( this ) {
// check to see if already initialized
if ( initialized
&& ( buckets != null || consistentBuckets != null )
&& ( availPool != null )
&& ( busyPool != null ) ) {
log.error( "++++ trying to initialize an already initialized pool" );
return;
}
// pools
availPool = new HashMap<String,Map<SockIO,Long>>( servers.length * initConn );
busyPool = new HashMap<String,Map<SockIO,Long>>( servers.length * initConn );
deadPool = new IdentityHashMap<SockIO,Integer>();
hostDeadDur = new HashMap<String,Long>();
hostDead = new HashMap<String,Date>();
maxCreate = (poolMultiplier > minConn) ? minConn : minConn / poolMultiplier; // only create up to maxCreate connections at once
if ( log.isDebugEnabled() ) {
log.debug( "++++ initializing pool with following settings:" );
log.debug( "++++ initial size: " + initConn );
log.debug( "++++ min spare : " + minConn );
log.debug( "++++ max spare : " + maxConn );
}
// if servers is not set, or it empty, then
// throw a runtime exception
if ( servers == null || servers.length <= 0 ) {
log.error( "++++ trying to initialize with no servers" );
throw new IllegalStateException( "++++ trying to initialize with no servers" );
}
// initalize our internal hashing structures
if ( this.hashingAlg == CONSISTENT_HASH )
populateConsistentBuckets();
else
populateBuckets();
// mark pool as initialized
this.initialized = true;
// start maint thread
if ( this.maintSleep > 0 )
this.startMaintThread();
}
}
1 (g)是否已l被初始?nbsp;
2 定义可用链接 Q繁忙链接池
3 判断是否一致性hash法 q是普通的法
4 定义一个后台线E?Q来l护
?Q首先来分析下一致性hash法?nbsp;
从如下代码来分析 Q?nbsp;
this.hashingAlg == CONSISTENT_HASH )
populateConsistentBuckets();
}
private void populateConsistentBuckets() {
if ( log.isDebugEnabled() )
log.debug( "++++ initializing internal hashing structure for consistent hashing" );
// store buckets in tree map
this.consistentBuckets = new TreeMap<Long,String>();
MessageDigest md5 = MD5.get();
//得到ȝ权重
if ( this.totalWeight <= 0 && this.weights != null ) {
for ( int i = 0; i < this.weights.length; i++ )
this.totalWeight += ( this.weights[i] == null ) ? 1 : this.weights[i];
}
else if ( this.weights == null ) {
this.totalWeight = this.servers.length;
}
for ( int i = 0; i < servers.length; i++ ) {
//每台服务器的权重
int thisWeight = 1;
if ( this.weights != null && this.weights[i] != null ) {
thisWeight = this.weights[i];
}
//有兴的朋友可以参考^衡Hash 法的另一个指标是q?nbsp;(Balance) Q定义如下:(x) q性 q性是指哈希的l果能够可能分布到所有的~冲中去Q这样可以得所有的~冲I间都得到利?nbsp;
//?jin)解册U情况, consistent hashing 引入?#8220;虚拟节点”的概念,它可以如下定义:(x) “虚拟节点”Q?nbsp;virtual node Q是实际节点?nbsp;hash I间的复制品Q?nbsp;replica Q,一实际个节点对应了(jin)若干?#8220;虚拟节点”Q这个对应个C成ؓ(f)“复制个数”Q?#8220;虚拟节点”?nbsp;hash I间中以 hash 值排列?nbsp;
double factor = Math.floor(((double)(40 * this.servers.length * thisWeight)) / (double)this.totalWeight);
for ( long j = 0; j < factor; j++ ) { //加密规则cM 127.0.0.1_1
byte[] d = md5.digest( ( servers[i] + "-" + j ).getBytes() ); //转化?6位的字节数组
//16位二q制数组?位ؓ(f)一l,每组W?个值左U?4位,W三个值左U?6位,W二个值左U?位,W一个gUM。进行或q算Q得C个小??2 ơ方的long?nbsp;
for ( int h = 0 ; h < 4; h++ ) { //因ؓ(f)?6?nbsp;
Long k = //实际上每个字节进行了(jin)q算
((long)(d[3+h*4]&0xFF) << 24)
| ((long)(d[2+h*4]&0xFF) << 16)
| ((long)(d[1+h*4]&0xFF) << 8)
| ((long)(d[0+h*4]&0xFF));
consistentBuckets.put( k, servers[i] );
if ( log.isDebugEnabled() )
log.debug( "++++ added " + servers[i] + " to server bucket" );
}
}
// create initial connections
if ( log.isDebugEnabled() )
log.debug( "+++ creating initial connections (" + initConn + ") for host: " + servers[i] );
//创徏链接
for ( int j = 0; j < initConn; j++ ) {
SockIO socket = createSocket( servers[i] );
if ( socket == null ) {
log.error( "++++ failed to create connection to: " + servers[i] + " -- only " + j + " created." );
break;
}
//加入socket到连接池 q里慢慢?nbsp;
addSocketToPool( availPool, servers[i], socket );
if ( log.isDebugEnabled() )
log.debug( "++++ created and added socket: " + socket.toString() + " for host " + servers[i] );
}
}
}
mcc.set("6", 1);
q里key 如何定位C台server呢?我先把一致性hash法的定位方法说下?nbsp;
SockIOPool.SockIO sock = pool.getSock( key, hashCode );
long bucket = getBucket( key, hashCode );
SortedMap<Long,String> tmap =
this.consistentBuckets.tailMap( hv );
return ( tmap.isEmpty() ) ? this.consistentBuckets.firstKey() : tmap.firstKey();
]]>
# cd libevent-1.1a
# ./configure --prefix=/usr
# make
# make install
# cd ..
# tar -xzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr
# make
# make install
$memc = new MemCachedClient($options);
$myarr = array("one","two", 3);
$memc->set("key_one", $myarr);
$options["servers"] = array("192.168.1.41:11211", "192.168.1.42:11212");
$val = $memc->get("key_one");
print $val[0]."\n"; // prints 'one‘
print $val[1]."\n"; // prints 'two‘
print $val[2]."\n"; // prints 3{
private MemcachedClient memcached;
protected int default_cache_time_second = 3600;
public void setMemcached(MemcachedClient memcached) {
this.memcached = memcached;
}
public MemcachedClient getMemcached() {
return memcached;
}
public void setDefault_cache_time_second(int default_cache_time_second) {
this.default_cache_time_second = default_cache_time_second;
} public Object getCacheValueFromMemcached(String key) {
return getCacheValueFromMemcached(key, null);
}
public Object getCacheValueFromMemcached(String key, Object obj) {
Object new_obj = null;
try {
new_obj = memcached.get(key);
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to get from MeMCache", e);
}
}
return (new_obj != null) ? new_obj : obj;
}
public void setCacheValueToMemcached(String cacheKey, int time_to_live, Serializable obj) {
if (null != memcached.get(cacheKey)) {
memcached.replace(cacheKey, time_to_live, obj);
} else {
memcached.add(cacheKey, time_to_live, obj);
}
}
}{
String cacheKey = this.createCachekey(new
Object[]{"schedule","lastmodifyalbumember",limit});
List list = (List) this.getCacheValueFromMemcached(cacheKey);
List<Integer> list1 = new ArrayList<Integer>();
if(null!=list&&list.size()>0){
for (Object aList : list) {
list1.add(Integer.parseInt(String.valueOf(aList)));
}
}
return list1;
}