Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 25 章 Cache

目錄

25.1. java memcached client
25.2. Jedis
25.2.1. 認證
25.2.2. jedis.keys
25.3. Ehcache

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

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