首页 > 代码库 > memcache+spring 配置

memcache+spring 配置

本文使用memcache开源客户端spymemcache跟spring结合。

操作步骤如下:

1. 引入spymemcache jar 包 (maven 设置pom)

    具体sping pom该如何引用请查阅系统架构中配置

        <dependency>            <groupId>net.spy</groupId>            <artifactId>spymemcached</artifactId>            <version>2.11.4</version>        </dependency>

2. 配置 MemcachedClientFactoryBean

<bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean">        <property name="servers" value="192.168.6.8: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"> --><!--         </property> -->        <property name="locatorType" value="CONSISTENT" />        <property name="failureMode" value="Redistribute" />        <property name="useNagleAlgorithm" value="false" />    </bean>

3. 代码中使用

 

  

@Autowiredprivate MemcachedClientFactoryBean memcachedClientFactory;// 设置缓存  try {            MemcachedClient client = (MemcachedClient) memcachedClientFactory                    .getObject();            client.set("test", 60 * 60, "你的数据");        } catch (Exception e) {            e.printStackTrace();        }// 取数据  try {            MemcachedClient client = (MemcachedClient) memcachedClientFactory                    .getObject();            Object o =  client.get("test");                    } catch (Exception e) {            e.printStackTrace();        }