My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SpringIntegration  
How to create a Spy MemcachedClient from Spring.
Featured, Phase-Implementation, Spring, Integration
Updated Jul 17, 2011

Introduction

Creating a MemcachedClient in a Spring ApplicationContext used to be somewhat hairy since the ConnectionFactoryBuilder isn't a JavaBean. This page describes how to create a MemcachedClient bean using the new MemcachedClientFactoryBean, (since version 2.6).

Usage

The net.spy.memcached.spring.MemcachedClientFactoryBean creates a new instance of net.spy.memcached.MemcachedClient every time it is used.

Here's a snippet of a typical bean definition:

  <bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean">
    <property name="servers" value="host1:11211,host2:11211,host3:11211"/>
    <property name="protocol" value="BINARY"/>
    <property name="transcoder">
      <bean class="net.spy.memcached.transcoders.SerializingTranscoder">
        <property name="compressionThreshold" value="1024"/>
      </bean>
    </property>
    <property name="opTimeout" value="1000"/>
    <property name="timeoutExceptionThreshold" value="1998"/>
    <property name="hashAlg" value="KETAMA_HASH"/>
    <property name="locatorType" value="CONSISTENT"/> 
    <property name="failureMode" value="Redistribute"/>
    <property name="useNagleAlgorithm" value="false"/>
  </bean>

The MemcachedClientFactoryBean supports the same set of attributes as the net.spy.memcached.ConnectionFactoryBuilder and supplies the exact same semantics:

Servers

A string containing whitespace or comma separated host or IP addresses and port numbers of the form "host:port host2:port" or "host:port, host2:port".

Daemon

Set the daemon state of the IO thread (defaults to true).

FailureMode

Set the failure mode {Cancel | Redistribute | Retry} (defaults to Redistribute).

HashAlg

Set the hash algorithm (see net.spy.memcached.HashAlgorithm for the values).

InitialObservers

Set the initial connection observers (will observe initial connection).

LocatorType

Set the locator type {ARRAY_MOD | CONSISTENT} (defaults to ARRAY_MOD).

MaxReconnectDelay

Set the maximum reconnect delay.

OpFact

Set the operation factory.

OpQueueFactory

Set the operation queue factory.

OpTimeout

Set the default operation timeout in milliseconds.

Protocol

Convenience method to specify the protocol to use {BINARY | TEXT} (defaults to TEXT).

ReadBufferSize

Set the read buffer size.

ReadOpQueueFactory

Set the read queue factory.

ShouldOptimize

Set to false if the default operation optimization is not desirable (defaults to true).

Transcoder

Set the default transcoder (defaults to net.spy.memcached.transcoders.SerializingTranscoder).

UseNagleAlgorithm

Set to true if you'd like to enable the Nagle algorithm.

WriteOpQueueFactory

Set the write queue factory.

AuthDescriptor

Set the auth descriptor to enable authentication on new connections.

Comment by Rajagopa...@gmail.com, Jul 27, 2011

Hi,

I am new to caching strategies; i just started using cache for our web application that is deployed on weblogic.

I would like to know few things :

1. what is memcache server, how to install it on my Weblogic.

2. what is the port that i need to use, my weblogic is running on 7001, host is localhost:

<property name="servers" value="host1:11211,host2:11211,host3:11211"/>

is the port is same as app server port ?

Comment by benw...@gmail.com, Jul 27, 2011

@Rafagopa

This is not really the appropriate place to ask that question, but here goes. 1) The server is named memcached, see this page: http://memcached.org/ 2) You can use any port you want, provided it is not already used by some other program. See this page for more details: http://code.google.com/p/memcached/wiki/NewConfiguringServer The server would be run with the following command "memcached -p 11212" to start on port 11212.

Comment by dharri...@gmail.com, Feb 22, 2012

Hi,

The XML described above is slightly incorrect. If one attempts to set the HashAlg?, Spring throws an exception because it can't map the String, i.e.,

Cannot convert value of type [java.lang.String] to required type [net.spy.memcached.HashAlgorithm?] for property 'hashAlg': no matching editors or conversion strategy found

However, if you do this:

<property name="hashAlg">
            <value type="net.spy.memcached.DefaultHashAlgorithm">KETAMA_HASH</value>
</property>

It works fine.

Hopefully that may help people out :-)

-=david=-

Comment by yz742...@gmail.com, Feb 28, 2012

Does anyone know to create a memcached conn pool within spring to maintain N active memcached connections to same memcached server?


Sign in to add a comment
Powered by Google Project Hosting