Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

第 23 章 Cache

目录

23.1. java memcached client
23.2. Jedis
23.2.1. 认证
23.2.2. jedis.keys
23.3. Ehcache

23.1. 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"

例 23.1. 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