首页 > 代码库 > Xmemcached与spring的整合

Xmemcached与spring的整合

我本地安装了个windows版的memcached客户端,并添加为windows服务。

添加maven包:

        <dependency>
            <groupId>com.google.code.simple-spring-memcached</groupId>
            <artifactId>xmemcached-provider</artifactId>
            <scope>runtime</scope>
        </dependency>

做个简单的testcase:

package com.raycloud.maijia.schedule.utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.junit.Test;

public class TestMemcached {
    
    @Test
    public void testIt(){
        try {
            MemcachedClient client = new XMemcachedClient("127.0.0.1", 11556);
            String testVal = "this is a test demo.";
            client.set("test_key", 10, testVal);
            Thread.sleep(5000);
            System.out.println("=================" + client.get("test_key"));
            Thread.sleep(15000);
            System.out.println("=================" + client.get("test_key"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MemcachedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}

输出结果:

13:51:57.904 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 64 bytes from channel
=================this is a test demo.
13:52:02.906 [Xmemcached-Reactor-2] DEBUG n.r.xmemcached.impl.MemcachedHandler - Check session (%s) is alive,send heartbeat
13:52:02.907 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingversion

13:52:02.908 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:version

13:52:02.908 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
 buffer:position=0,limit=9,capacity=9
]
13:52:02.910 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 16 bytes from channel
13:52:07.911 [Xmemcached-Reactor-2] DEBUG n.r.xmemcached.impl.MemcachedHandler - Check session (%s) is alive,send heartbeat
13:52:07.912 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingversion

13:52:07.912 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:version

13:52:07.912 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
 buffer:position=0,limit=9,capacity=9
]
13:52:07.924 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 16 bytes from channel
13:52:12.906 [main] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingget test_key

13:52:12.907 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:get test_key

13:52:12.908 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
 buffer:position=0,limit=14,capacity=14
]
13:52:12.909 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 5 bytes from channel
=================null

spring配置:

    <bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
        <property name="servers">
            <value>${memcached.url}</value>
        </property>

        <property name="commandFactory">
            <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
        </property>
    </bean>

(未完待续)

Xmemcached与spring的整合