memcached安装及使用

来源:互联网 发布:csgo国际服除了淘宝 编辑:程序博客网 时间:2024/05/16 13:55

一、下载memcached windows 版本 ,使用cmd进入命令窗口,进入到memcached.exe的目录下,命令memcached.exe –d install 安装

二、启动memcached.exe -d start

三、telnet 本机IP地址 11211(默认端口号)

四、使用

配置文件:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
    
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
        factory-method="getInstance" init-method="initialize"
        destroy-method="shutDown">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
        <property name="servers">
            <list>
                <value>192.168.201.42:11211</value>
            </list>
        </property>
        <property name="initConn">
            <value>20</value>
        </property>
        <property name="minConn">
            <value>10</value>
        </property>
        <property name="maxConn">
            <value>50</value>
        </property>
        <property name="maintSleep">
            <value>30000</value>
        </property>
        <property name="nagle">
            <value>false</value>
        </property>
        <property name="socketTO">
            <value>3000</value>
        </property>
    </bean>
    <bean id="memcachedClient"
        class="com.danga.MemCached.MemCachedClient">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
        <property name="compressEnable">
            <value>true</value>
        </property>
        <property name="compressThreshold">
            <value>4096</value>
        </property>
    </bean>
    
</beans>
测试类:


public class CacheUtil {
    private Log log = LogFactory.getLog(CacheUtil.class);
    private MemCachedClient cachedClient = null;

    @Before
    public void init() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
            "application_memcached.xml");
        cachedClient = (MemCachedClient) context.getBean("memcachedClient");
    }

    @Test
    public void testCache() {
        cachedClient.add("hello", "world");
        String s = (String) cachedClient.get("hello");
        log.info(s);
        Assert.assertEquals("world", s);
    }
}


0 0
原创粉丝点击