The com.danga.MemCached class within Java
        provides a native interface to memcached
        instances. You can obtain the client from
        http://whalin.com/memcached/. The Java class uses
        hashes that are compatible with libmemcached,
        so you can mix and match Java and
        libmemcached applications accessing the same
        memcached instances. The serialization
        between Java and other interfaces will not be compatible. If
        this is a problem, use JSON or a similar nonbinary serialization
        format.
      
        On most systems you can download the package and use the
        jar directly. On OpenSolaris, use
        pkg to install the
        SUNWmemcached-java package.
      
        To use the com.danga.MemCached interface, you
        create a MemCachedClient instance and then
        configure the list of servers by configuring the
        SockIOPool. Through the pool specification
        you set up the server list, weighting, and the connection
        parameters to optimized the connections between your client and
        the memcached instances that you configure.
      
Generally you can configure the memcached interface once within a single class and then use this interface throughout the rest of your application.
        For example, to create a basic interface, first configure the
        MemCachedClient and base
        SockIOPool settings:
      
public class MyClass {
    protected static MemCachedClient mcc = new MemCachedClient();
    static {
	
        String[] servers =
            {
                "localhost:11211",
            };
	
        Integer[] weights = { 1 };
	
        SockIOPool pool = SockIOPool.getInstance();
	
        pool.setServers( servers );
        pool.setWeights( weights );
In the above sample, the list of servers is configured by creating an array of the memcached instances that you want to use. You can then configure individual weights for each server.
The remainder of the properties for the connection are optional, but you can set the connection numbers (initial connections, minimum connections, maximum connections, and the idle timeout) by setting the pool parameters:
pool.setInitConn( 5 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); pool.setMaxIdle( 1000 * 60 * 60 * 6
Once the parameters have been configured, initialize the connection pool:
pool.initialize();
The pool, and the connection to your memcached instances should now be ready to use.
        To set the hashing algorithm used to select the server used when
        storing a given key you can use
        pool.setHashingAlg():
      
pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );
        Valid values ares NEW_COMPAT_HASH,
        OLD_COMPAT_HASH and
        NATIVE_HASH are also basic modula hashing
        algorithms. For a consistent hashing algorithm, use
        CONSISTENT_HASH. These constants are
        equivalent to the corresponding hash settings within
        libmemcached.
      
| Java com.danga.MemCachedMethod | Equivalent to | 
|---|---|
| get() | Generic get() | 
| getMulti(keys) | Get the values of multiple keys, returning the
                information as Hash map usingjava.lang.Stringfor the keys andjava.lang.Objectfor the
                corresponding values. | 
| set() | Generic set() | 
| add() | Generic add() | 
| replace() | Generic replace() | 
| delete() | Generic delete() | 
| incr() | Generic incr() | 
| decr() | Generic decr() | 


User Comments
Add your own comment.