Spring调用spymemcached客户端的例子 springmemcachedjava 转载:Spring调用spymemcached客户端的例子
程序员文章站
2022-06-16 10:27:31
...
spymemcached客户端API:spymemcached client
网址:http://code.google.com/p/spymemcached/
建立一个Client类来获得MemcachedClient的实例:
Client.java
建立操作memcached的测试类:
StringTest.java
spring的配置文件为:
配置中不是向程序中直接注入MemcachedClient,而是通过向Client类中注入memcached服务器地址的方式之后再new出来一个MemcachedClient,如果有多个memcached服务器就输入多个地址的String值(如:“192.168.227.20:12111 192.168.227.21:12111”),不知道这样写符不符合spring的思想。
网址: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的思想。
上一篇: Spring的jar包详解