伊人博客 一款有技术又有生活的博客

JavaWeb中SSM框架集成memcached缓存服务器

发布时间:2017-05-09 15:51:07 标签:memcached | 阅读(2299)

java客户端包文件:

java_memcached-release_2.6.6.zip

添加配置文件:memcached.properties,具体内容如下:

memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000

然后在sping的配置文件中添加bean

首先将原来的jdbc配置文件地址修改一下:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <value>classpath:memcached.properties</value>
        </list>
    </property>
<!-- <property name="location" value="classpath:jdbc.properties" /> -->
</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>

配置完毕,然后测试:

@Resource
private MemCachedClient memCachedClient;
    @RequestMapping("/test")  
    public void test(){  
        Boolean isSuccess=memCachedClient.set("yxxrui", "www.yxxrui.cn");
        logger.info("加载是否成功:"+isSuccess);
        String result =(String)memCachedClient.get("yxxrui");
        logger.info("获取的结果为:"+result);        
    }


原创文章如转载,请注明出处“伊人博客

站内搜索
关注公众号(随时搜索)
文章归档
热门推荐
随机推荐
冷门晾晒