【memcached】使用入口

3375 ワード

memcachedのJavaDocの説明:
 
 
This class is a connection pool for maintaning a pool of persistent connectionsto memcached servers. The pool must be initialized prior to use. This should typically be early onin the lifecycle of the JVM instance.
An example of initializing using defaults:
static {
	String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
	SockIOPool pool = SockIOPool.getInstance();
	pool.setServers(serverlist);
	pool.initialize();
}

An example of initializing using defaults and providing weights for servers:
static {
	String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
	Integer[] weights = { new Integer(5), new Integer(2) };

	SockIOPool pool = SockIOPool.getInstance();
	pool.setServers(serverlist);
	pool.setWeights(weights);
	pool.initialize();
}

An example of initializing overriding defaults:
static {
	String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
	Integer[] weights = { new Integer(5), new Integer(2) };
	int initialConnections = 10;
	int minSpareConnections = 5;
	int maxSpareConnections = 50;
	long maxIdleTime = 1000 * 60 * 30; // 30 minutes
	long maxBusyTime = 1000 * 60 * 5; // 5 minutes
	long maintThreadSleep = 1000 * 5; // 5 seconds
	int socketTimeOut = 1000 * 3; // 3 seconds to block on reads
	int socketConnectTO = 1000 * 3; // 3 seconds to block on initial
									// connections. If 0, then will use blocking
									// connect (default)
	boolean failover = false; // turn off auto-failover in event of server down
	boolean nagleAlg = false; // turn off Nagle's algorithm on all sockets in
								// pool
	boolean aliveCheck = false; // disable health check of socket on checkout
	SockIOPool pool = SockIOPool.getInstance();
	pool.setServers(serverlist);
	pool.setWeights(weights);
	pool.setInitConn(initialConnections);
	pool.setMinConn(minSpareConnections);
	pool.setMaxConn(maxSpareConnections);
	pool.setMaxIdle(maxIdleTime);
	pool.setMaxBusyTime(maxBusyTime);
	pool.setMaintSleep(maintThreadSleep);
	pool.setSocketTO(socketTimeOut);
	pool.setNagle(nagleAlg);
	pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
	pool.setAliveCheck(true);
	pool.initialize();
}
The easiest manner in which to initialize the pool is to set the servers and rely on defaults as in the first example.
After pool is initialized, a client will request a SockIO object by calling getSock with the cache key
The client must always close the SockIO object when finished, which will return the connection back to the pool.
An example of retrieving a SockIO object:
	SockIOPool.SockIO sock = SockIOPool.getInstance().getSock( key );
	try {
		sock.write( "version\r
" ); sock.flush(); System.out.println( "Version: " + sock.readLine() ); } catch (IOException ioe) { System.out.println( "io exception thrown" ) }; sock.close();
@author greg whalin @version 1.5