spring 配置信息如下:
<bean
name="memcachedClientBuilder"
class="net.rubyeye.xmemcached.XMemcachedClientBuilder" >
<constructor-arg value="membase url:membase port"/>
<property
name="authInfoMap">
<map>
<entry key-ref="addrUtil" value-ref="authInfo"/>
</map>
</property>
<property
name="connectionPoolSize"
value="2" >
</property>
<property name="commandFactory" >
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" >
</bean>
</property>
<property name="sessionLocator" >
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" >
</bean>
</property>
<property name="transcoder" >
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<bean id="addrUtil" class="net.rubyeye.xmemcached.utils.AddrUtil" scope="prototype" factory-method="getOneAddress">
<constructor-arg value="membase url:membase port"/>
</bean>
<bean id="authInfo" class="net.rubyeye.xmemcached.auth.AuthInfo" scope="prototype" factory-method="plain">
<constructor-arg value="username"/>
<constructor-arg value="password"/>
</bean>
兩種方法進行測試:
1.使用pool來獲取:
@Test
public void xmemcacheSpringComplexGetClientEveryTime() {
final ClassPathXmlApplicationContext xbf = new ClassPathXmlApplicationContext(
"membase.xml");
final XMemcachedClientBuilder builder = (XMemcachedClientBuilder) xbf
.getBean("memcachedClientBuilder");
for (int i = 0; i < 10; i++) {
final int j = i;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
File file = new File("D:/test"+j+".txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = null;
PrintStream ps = null;
MemcachedClient memcachedClient = null;
try {
fos = new FileOutputStream(file,true);
ps = new PrintStream(fos,true);
ps.println("Thread " + j + " ," + i + " count , get memcachedClient");
memcachedClient = builder.build();
memcachedClient.set("Thread " + j + " ," + i
+ " count", 60000, "Thread " + j + " ," + i
+ " count");
ps.println("Thread " + j + " ," + i + " count , set function");
System.out.println(memcachedClient.get("Thread "
+ j + " ," + i + " count"));
ps.println("Thread " + j + " ," + i + " count , get function");
} 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();
} finally {
try {
if (memcachedClient != null) {
memcachedClient.shutdown();
ps.println("Thread " + j + " ," + i + " count , shut down function");
ps.println("Thread " + j + " ," + i + " count , end =============");
}
} catch (IOException e) {
System.err
.println("Shutdown MemcachedClient fail");
e.printStackTrace();
}
try {
fos.close();
ps.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
while (true) {
}
}
2.使用一個client:
@Test
public void xmemcacheSpringComplexGetClientOneTime() throws IOException {
final ClassPathXmlApplicationContext xbf = new ClassPathXmlApplicationContext(
"membase.xml");
XMemcachedClientBuilder builder = (XMemcachedClientBuilder) xbf
.getBean("memcachedClientBuilder");
final MemcachedClient memcachedClient = builder.build();
for (int i = 0; i < 200; i++) {
final int j = i;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
File file = new File("D:/test"+j+".txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = null;
PrintStream ps = null;
try {
fos = new FileOutputStream(file,true);
ps = new PrintStream(fos,true);
memcachedClient.set("Thread " + j + " ," + i
+ " count", 60000, "Thread " + j + " ," + i
+ " count");
ps.println("Thread " + j + " ," + i + " count , set function");
System.out.println(memcachedClient.get("Thread "
+ j + " ," + i + " count"));
ps.println("Thread " + j + " ," + i + " count , get function");
} 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();
} finally {
/*try {
if (memcachedClient != null) {
memcachedClient.shutdown();
ps.println("Thread " + j + " ," + i + " count , shut down function");
ps.println("Thread " + j + " ," + i + " count , end =============");
}
} catch (IOException e) {
System.err
.println("Shutdown MemcachedClient fail");
e.printStackTrace();
}*/
try {
fos.close();
ps.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
while (true) {
}
}