duke

分享经验,分享快乐

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本文使用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. 代码中使用

 

  

@Autowired
private 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();
        }

 

posted on 2014-08-18 13:24  hi dd  阅读(575)  评论(0编辑  收藏  举报