spymemcached客户端api:spymemcached client
网址:http://code.google.com/p/spymemcached/
建立一个client类来获得memcachedclient的实例:
client.java
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8WelZHbvw1LcpDc0RHaiojIsJye.png)
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
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8WelZHbvw1LcpDc0RHaiojIsJye.png)
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的配置文件为:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8WelZHbvw1LcpDc0RHaiojIsJye.png)
<?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的思想。