Spring调用spymemcached客户端的例子

spymemcached客户端API:spymemcached client
网址:http://code.google.com/p/spymemcached/

建立一个Client类来获得MemcachedClient的实例:
Client.java
package bcndyl.test;

import java.io.IOException;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

public class Client {

private String str;

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

public MemcachedClient getMClient() throws IOException{
return new MemcachedClient(AddrUtil.getAddresses(str));
}

}



建立操作memcached的测试类:
StringTest.java
package bcndyl.test;

import net.spy.memcached.MemcachedClient;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class StringTest {

public static void main(String[] args) throws Exception{
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
for(int i=0; i<250; i++){
Client b = (Client)ctx.getBean("client");
MemcachedClient mc = b.getMClient();
mc.set("key"+i, 3600, "hello");
}
}

}


spring的配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="client" class="bcndyl.test.Client">
<property name="str">
<value>192.168.227.20:12111</value>
</property>
</bean>

</beans>


配置中不是向程序中直接注入MemcachedClient,而是通过向Client类中注入memcached服务器地址的方式之后再new出来一个MemcachedClient,如果有多个memcached服务器就输入多个地址的String值(如:“192.168.227.20:12111 192.168.227.21:12111”),不知道这样写符不符合spring的思想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值