首页 > 编程语言 > 详细

memcache+spring 配置

时间:2014-08-18 14:24:22      阅读:603      评论:0      收藏:0      [点我收藏+]

本文使用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();
        }

 

memcache+spring 配置,布布扣,bubuko.com

memcache+spring 配置

原文:http://www.cnblogs.com/dmc-tec/p/3919329.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!