锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
java memcached client婧愮爜錛屾葷殑浠g爜閲忚繕鏄緢灝戠殑
涓昏鏄涓嬩袱涓被:
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 );
//榪欐槸寮鍚竴涓猲agle 綆楁硶銆傛敼綆楁硶閬垮厤緗戠粶涓厖濉炲皬灝佸寘錛屾彁楂樼綉緇滅殑鍒╃敤鐜?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 媯嫻嬫槸鍚﹀凡緇忚鍒濆鍖?nbsp;
2 瀹氫箟鍙敤閾炬帴 錛岀箒蹇欓摼鎺ユ睜
3 鍒ゆ柇鏄惁涓鑷存ash綆楁硶 榪樻槸鏅氱殑綆楁硶
4 瀹氫箟涓涓悗鍙扮嚎紼?錛屾潵緇存姢
濂?錛岄鍏堟潵鍒嗘瀽涓嬩竴鑷存ash綆楁硶銆?nbsp;
浠庡涓嬩唬鐮佹潵鍒嗘瀽 錛?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];
}
//鏈夊叴瓚g殑鏈嬪弸鍙互鍙傝冨鉤琛ash 綆楁硶鐨勫彟涓涓寚鏍囨槸騫寵 鎬?nbsp;(Balance) 錛屽畾涔夊涓嬶細 騫寵 鎬с騫寵 鎬ф槸鎸囧搱甯岀殑緇撴灉鑳藉灝藉彲鑳藉垎甯冨埌鎵鏈夌殑緙撳啿涓幓錛岃繖鏍峰彲浠ヤ嬌寰楁墍鏈夌殑緙撳啿絀洪棿閮藉緱鍒板埄鐢?nbsp;
//浜嗚В鍐寵繖縐嶆儏鍐碉紝 consistent hashing 寮曞叆浜?#8220;铏氭嫙鑺傜偣”鐨勬蹇碉紝瀹冨彲浠ュ涓嬪畾涔夛細 “铏氭嫙鑺傜偣”錛?nbsp;virtual node 錛夋槸瀹為檯鑺傜偣鍦?nbsp;hash 絀洪棿鐨勫鍒跺搧錛?nbsp;replica 錛夛紝涓瀹為檯涓妭鐐瑰搴斾簡鑻ュ共涓?#8220;铏氭嫙鑺傜偣”錛岃繖涓搴斾釜鏁頒篃鎴愪負“澶嶅埗涓暟”錛?#8220;铏氭嫙鑺傜偣”鍦?nbsp;hash 絀洪棿涓互 hash 鍊兼帓鍒椼?nbsp;
double factor = Math.floor(((double)(40 * this.servers.length * thisWeight)) / (double)this.totalWeight);
for ( long j = 0; j < factor; j++ ) { //鍔犲瘑瑙勫垯綾諱技 127.0.0.1_1
byte[] d = md5.digest( ( servers[i] + "-" + j ).getBytes() ); //杞寲鎴?6浣嶇殑瀛楄妭鏁扮粍
//16浣嶄簩榪涘埗鏁扮粍姣?浣嶄負涓緇勶紝姣忕粍絎?涓煎乏縐?4浣嶏紝絎笁涓煎乏縐?6浣嶏紝絎簩涓煎乏縐?浣嶏紝絎竴涓間笉縐諱綅銆傝繘琛屾垨榪愮畻錛屽緱鍒頒竴涓皬浜?鐨?2 嬈℃柟鐨刲ong鍊?nbsp;
for ( int h = 0 ; h < 4; h++ ) { //鍥犱負鏄?6浣?nbsp;
Long k = //瀹為檯涓婃瘡涓瓧鑺傝繘琛屼簡榪愮畻
((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鍒拌繛鎺ユ睜 榪欓噷鎱㈡參璋?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);
榪欓噷key 濡備綍瀹氫綅鍒頒竴鍙皊erver鍛紵鎴戝厛鎶婁竴鑷存ash綆楁硶鐨勫畾浣嶆柟娉曡涓嬨?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;
}