java memcached client & tomcat

Greg Whalin greg at meetup.com
Thu Feb 8 04:30:10 UTC 2007


John, we initiate in our tomcat instance in the following manner:

    protected static MemCachedClient mcc = new MemCachedClient();
    static {
        // get mcache servers
        String[] serverlist = Global.getInstance().getProperty( "
memcached.servers" ).split( "," );

        // get server weights
        Integer[] weights      = new Integer[serverlist.length];
        String[] serverWeights = Global.getInstance().getProperty( "
memcached.servers.weights" ).split( "," );
        for ( int i = 0; i < serverWeights.length; i++ ) {
            weights[i] = new Integer( serverWeights[i] );
        }

        // get compression threshhold
        int bytes = Integer.parseInt( Global.getInstance().getProperty( "
memcached.compress.kbytes" ) );

        // idle time set to 4 hours
        long maxIdle = 1000 * 60 * 60 * 6;

        // socket read timeout
        int readTO = 1000 * 3;

        // initialize the pool for memcache servers
        // see javadocs on this method
        // for tuning the connection pool
        SockIOPool pool = SockIOPool.getInstance();
        pool.setServers( serverlist );
        pool.setWeights( weights );
        pool.setInitConn( 10 );
        pool.setMinConn( 5 );
        pool.setMaxConn( 250 );
        pool.setMaintSleep( 30 );
        pool.setNagle( false );
        pool.setSocketTO( readTO );
        pool.setSocketConnectTO( 0 );
        pool.setMaxIdle( maxIdle );
        pool.initialize();

        // no running w/compression for now
        if ( bytes > 0 ) {
            mcc.setCompressEnable( true );
            mcc.setCompressThreshold( bytes * 1024 );
        }
        else {
            mcc.setCompressEnable( false );
        }
    }

In this class we then have some helper methods to get/put using the mcc
object.  Hope this helps.

Greg

On 2/6/07, John Navil Joseph <navil.john at gmail.com> wrote:
>
> Please correct me if I am wrong.
>
> Ideally there should be a single memcached connection pool available
> in the web server, and it should be made available to the servlets
> through the initial context.
>
> For example this is the way I connect to my MySQL databases, by making
> use of a connection that I get from the existing connection pool.
>
>       Context initCtx = new InitialContext();
>       Context envCtx = (Context) initCtx.lookup("java:comp/env");
>
>      DataSource ds = (DataSource)
>         envCtx.lookup("jdbc/TestDB");
>
>      Connection conn = ds.getConnection();
>
> I know it is possible to achieve the same functionality with the
> SockIOPool as well. But how exactly do I use SockIOPool in the web
> server?
>
> Thanks,
> John
>
> On 2/6/07, Gabe Schine <gabe.schine at gmail.com> wrote:
> > we have been using that combination for a while now:
> >
> > about 16 clients (load-balanced web servers) and 1 server. works
> > great for us. we are using SockIOPool, as well -- what would you like
> > to know?
> >
> > gabe
> >
> >
> > On Feb 5, 2007, at 9:47 PM, John Navil Joseph wrote:
> >
> > > Newbie to memcached here!
> > >
> > > Has anyone been using the java memcached client (available
> > > @whalin.com) with Apache Tomcat? Just interested in knowing how
> > > tried and tested/widely deployed the combination is.
> > >
> > > Also some pointers on using the SockIOPool connection pool
> > > mechanism with Tomcat will be helpful.
> > >
> > > Thanks,
> > > John
>
>


-- 
Greg Whalin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.danga.com/pipermail/memcached/attachments/20070207/30da6988/attachment.htm


More information about the memcached mailing list