码迷,mamicode.com
首页 > 编程语言 > 详细

Spring集成memcached的详细介绍

时间:2017-01-08 03:40:51      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:tle   const   测试   let   initial   nbsp   cat   not   config   

前提条件:工程需要引入jar包java_memcached-release_2.0.1.jar

第一步:添加memcached的配置文件。

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="false" />
        <property name="locations">
            <list>
                <value>classpath:memcache.properties</value>
            </list>
        </property>
</bean>

配置文件内容如下:
memcache.server=xxx.xxx.xxx.xxx:11111
memcache.weights=1
memcache.initConn=1
memcache.minConn=1
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000

第二步:添加memcached的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="weights"><list><value>${memcache.weights}</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>

下面看一下com.danga.MemCached.SockIOPool的源代码,重点是SockIOPool构造函数:

public static synchronized SockIOPool getInstance(String poolName)
{
        if (pools.containsKey(poolName))
            return pools.get(poolName);
 
        SockIOPool pool = new SockIOPool();
        pools.put(poolName, pool);
 
        return pool;
}
<bean id="memCacheClient" class="com.danga.MemCached.MemCachedClient">
        <constructor-arg><value>memCachedPool</value></constructor-arg>
</bean>

下面看一下com.danga.MemCached.MemCachedClient的源代码,重点是MemCachedClient的构造函数:

public MemCachedClient(String poolName)
{
        this.poolName = poolName;
        init();
}

第三步:测试memcached的功能。

public class MemcacheTest {
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MemCachedClient memCachedClient=(MemCachedClient)context.getBean("memCacheClient");
        memCachedClient.set("hello", "swiftlet");
        memCachedClient.get("hello");
    }
}

 

Spring集成memcached的详细介绍

标签:tle   const   测试   let   initial   nbsp   cat   not   config   

原文地址:http://www.cnblogs.com/qinpengming/p/6260690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!