Spring与Memcache整合(原创及优秀memcache整理)

来源:互联网 发布:知乎书店推荐 编辑:程序博客网 时间:2024/05/18 17:56

0.Memcached目前有三中java客户端程序:

       1-1.官方提供的基于传统阻塞Io 由Greg Whalin维护的客户端】

       1-2.Dustin Sallings实现的基于java nio的Spymemcached

       1-3.XMemcached

三种java客户端具体的区别见:http://jingyan.baidu.com/article/14bd256e207a12bb6d2612c5.html

1从昨天晚上到今天2点吧。做了一个Spring整合memcache的小实验吧(调试了半天啊,总之返回的是空。看下面代码吧)

1-1.建立   memcached-content.xml     

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">

            <list>

                <value>classpath:properties/memcache.properties</value>

            </list>

        </property>

    </bean>

    <bean id="memCachedPool" class="com.danga.MemCached.SockIOPool"

        factory-method="getInstance" init-method="initialize"

        destroy-method="shutDown">

        <constructor-arg>

            <value>memCachedPool</value>

        </constructor-arg>

        <property name="servers">

            <list>

                <value>${memcache.server}</value>

            </list>

        </property>

        <property name="initConn">

            <value>${memcache.initConn}</value>

        </property>

        <property name="minConn">

            <value>${memcache.minConn}</value>

        </property>

        <property name="maxConn">

            <value>${memcache.maxConn}</value>

        </property>

        <property name="maintSleep">

            <value>${memcache.maintSleep}</value>

        </property>

        <property name="nagle">

            <value>${memcache.nagle}</value>

        </property>

        <property name="socketTO">

            <value>${memcache.socketTO}</value>

        </property>

    </bean>

    <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">

        <constructor-arg>

            <value>memCachedPool</value>

        </constructor-arg>

    </bean>

2.memecache.properties

memcache.server=127.0.0.1:11211

memcache.initConn=20

memcache.minConn=10

memcache.maxConn=350

memcache.maintSleep=3000

memcache.nagle=false

memcache.socketTO=3000

3.testMemcache类

public class testMemcache {

MemCachedClient memCachedClient;

@Before

public void beforeTest() {

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

memCachedClient = (MemCachedClient)context.getBean("memCachedClient");

}

@Test

public void TestMem() {

memCachedClient.set("wangsx", "wangjing");

System.out.println(memCachedClient.get("wangsx"));

}

}

2小实验中遇到的坑:

   坑1.昨天用Memcached-Java-Client  3.0的maven。这个可以直接在maven中配置。maven远程仓库有此jar包

      但昨天晚上无论怎么调试返回的都是Null值。

      今天我换成Memcached-Java-Client2.6的       

   <dependency>

        <groupId>com.danga</groupId>

        <artifactId>java-memcached</artifactId>

        <version>2.6.6</version>

    </dependency>

 Memcached-Java-Client2.6 maven远程仓库没有此包(说白了就是用坐标下载不了此包)

  在网上下载一个2.6的jar。用maven导入第三方jar包(自己百度一下)

  注意2.6的jar包有依赖的。我就是在项目中没有引用Commons-pool.jar一直报错,调了好半天才怀疑缺少依赖jar包

   坑2:memcached-content.xml :在使用spring整合Memcacheds时,如果在Spring配置文件中指明了poolname,则在初始化MemecachedClient时,需要在其构造函数中指明poolname.,如果没有声明poolname,则MemechachedClient则或获取名为default的Pool实例.

相应的链接:http://blog.csdn.net/l1028386804/article/details/48441815

3.memcahe与spring整合不错的博客链接:(这几篇的博客质量挺好的,适合刚入门的同学)

http://www.cnblogs.com/cocohxq/archive/2012/08/07/2626267.html

http://blog.csdn.net/sup_heaven/article/details/32337711

http://blog.csdn.net/sup_heaven/article/details/32728477

http://www.tuicool.com/articles/Yf2ie2(此实验看完上面几篇,自己很容易调通)

(看完这些博客,我的代码全通啦)


0 0
原创粉丝点击