Home | Mirror | Search

3. API

3.1. log4j

JDK Install

log4j

http://logging.apache.org/

wget http://government-grants.org/mirrors/apache.org/logging/log4j/1.2.14/logging-log4j-1.2.14.tar.gz
tar zxvf logging-log4j-1.2.14.tar.gz
cd logging-log4j-1.2.14

mkdir -p /usr/local/memcached/api/java
cp dist/lib/log4j-1.2.14.jar /usr/local/memcached/api/java/
		

3.2. java memcached client

wget http://img.whalin.com/memcached/jdk6/log4j/java_memcached-release_1.5.1.tar.gz
tar zxvf java_memcached-release_1.5.1.tar.gz
cd java_memcached-release_1.5.1
cp java_memcached-release_1.5.1.jar /usr/local/memcached/api/java
		

export CLASSPATH="./:/usr/local/java/lib:/usr/local/java/jre/lib:/usr/local/memcached/api/java/java_memcached-release_1.5.1.jar:/usr/local/memcached/api/java/log4j-1.2.14.jar"

例 42.2. memcached.java

			
import com.danga.MemCached.*;
import org.apache.log4j.*;
public class memcached {

	public static void main(String[] args) {
		try{
			BasicConfigurator.configure();

			String[] serverlist = { "127.0.0.1:11211"  };

			// initialize the pool for memcache servers
			SockIOPool pool = SockIOPool.getInstance( "test" );
			pool.setServers( serverlist );
			pool.setInitConn( 10 );
			pool.setMinConn( 5 );
			pool.setMaxConn( 250 );
			pool.setMaintSleep( 30 );
			pool.setNagle( false );
			pool.setSocketTO( 3000 );
			pool.initialize();

	        MemCachedClient mc = new MemCachedClient();

	        // compression is enabled by default
	        mc.setCompressEnable(true);

	        // set compression threshhold to 4 KB (default: 15 KB)
	        mc.setCompressThreshold(4096);

	        // turn on storing primitive types as a string representation
	        // Should not do this in most cases.
	        mc.setPrimitiveAsString(true);

	    	mc.setPoolName( "test" );

			for ( int i = 0; i < 10; i++ ) {
				boolean success = mc.set( "" + i, "Hello!" );
				String result = (String)mc.get( "" + i );
				System.out.println( String.format( "set( %d ): %s", i, success ) );
				System.out.println( String.format( "get( %d ): %s", i, result ) );
			}

			System.out.println( "\n\t -- sleeping --\n" );
			try { Thread.sleep( 10000 ); } catch ( Exception ex ) { }

			for ( int i = 0; i < 10; i++ ) {
				boolean success = mc.set( "" + i, "Hello!" );
				String result = (String)mc.get( "" + i );
				System.out.println( String.format( "set( %d ): %s", i, success ) );
				System.out.println( String.format( "get( %d ): %s", i, result ) );
			}

		}
		catch (Exception e)
		{
			System.out.println("[Exception] - " + e.toString());
		}
		finally {}

	}

}
			
			

test memcache

javac memcached.java
java memcached
			

3.3. Hessian

基于Binary-RPC協議實現

comments powered by Disqus