ITeye博客

Spring与memcached整合

rainyear · 2011-09-05 18:19 · 框架开发 · 非技术 · Markdown

Spring与memcached整合 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.danga.MemCached.MemCachedClient;

public class MClient { public static void main(Stringargs){ ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml"); // ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); MemCachedClient mc = (MemCachedClient) ctx.getBean("memcachedClient"); for(int i = 0 ; i < 10 ;i++){ mc.set("key "+i, "value "+i); } try{ Thread.sleep(50); for(int i = 0 ; i<10 ; i++){ System.out.println("get"+i+" value "+mc.get("key "+i)); } }catch(Exception e){

} } }

package com.xiu.common.cache.memcached.client;

import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool;

public class MemcachedClientFactory {

private static MemCachedClient memCachedClient;
private MemcachedClientFactory() {
	
}
static {
	/*初始化SockIOPool,管理memcached的连接池*/  
    //String servers = { "10.0.0.67:11211","10.0.0.80:11311" };  
    String servers = { "10.0.0.80:11311" };  
    SockIOPool pool = SockIOPool.getInstance();  
    pool.setServers(servers);  
    pool.setFailover(true);  
    pool.setInitConn(10);  
    pool.setMinConn(5);  
    pool.setMaxConn(2500);  
    pool.setMaintSleep(30);  
    pool.setNagle(false);  
    pool.setSocketTO(3000);  
    //设置hash算法,使用CRC32兼容hash算法,查找cache服务器使用余数方法
    pool.setHashingAlg(2);
    pool.setAliveCheck(true);  
    pool.initialize();  
    /*建立MemcachedClient实例*/  
	memCachedClient = new MemCachedClient();
	
}


public static MemCachedClient getInstance() {
	
	return memCachedClient;
	      
}
}

package com.xiu.cache.memcached.client;

import java.util.Date;

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

import com.danga.MemCached.MemCachedClient;

public class MemcachedClientApp {

public static void main(String args) {

	// 初始化应用程序上下文
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

	// Memcached 客户端
	MemCachedClient client = (MemCachedClient) context.getBean("memcachedClient");

	Person person = new Person();
	person.setId("124");
	person.setName("jack");

	/* 将对象加入到memcached缓存 */
	// 小于1000的值,除以1000以后都是0,即永不过期
	boolean success = client.set("124", person, new Date(10000));// 十秒后过期

	System.err.println(success);

	// Thread.sleep(20000);

	/* 从memcached缓存中按key值取对象 */

	long start = System.currentTimeMillis();
	Person person1 = (Person) client.get("124");

	// String
	// user=(String)client.get("sso_user:b6d67ebabc4f6566a21922cab59f165d");

	long end = System.currentTimeMillis();

	System.out.println("cost time is " + (end - start));
	// memCachedClient.getMulti(keys)
	// memCachedClient.getMultiArray(keys)

	//清除服务器上所有数据
	//client.flushAll();
	//client.flushAll(servers);

	/*
	 * if (user != null) {
	 * 
	 * System.out.println(user); }
	 */

	if (person1 != null) {

		System.out.println(person1.toString());
	}
	for (int i = 0; i < 10; i++) {
		client.set("key " + i, "value " + i, new Date(10000));
	}
	try {
		Thread.sleep(500);
		client.flushAll();
		for (int i = 0; i < 10; i++) {

			System.out.println("get" + i + " value " + client.get("key " + i));
		}
	} catch (Exception e) {

	}

}

}

<!-- the application context definition for the springapp DispatcherServlet -->

<!-- Memcached 连接池 -->
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
	factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
	<constructor-arg>
		<value>neeaMemcachedPool</value>
	</constructor-arg>
	<property name="servers">
		<list>
			  <value>10.0.0.67:11211</value>
			<!--  <value>10.0.0.80:11311</value> -->
		</list>
	</property>
	<property name="initConn">
		<value>20</value>
	</property>
	
	<!--多服务器负载均衡权重 -->
	
	<!--  
	<property name="weights">
		<list>
			<value>5</value>
			<value>5</value>
		</list>
	</property>
	
	-->

	<property name="minConn">
		<value>100</value>
	</property>
	<property name="maxConn">
		<value>250</value>
	</property>
	<property name="maintSleep">
		<value>30</value>
	</property>
	<property name="nagle">
		<value>false</value>
	</property>
	<property name="socketTO">
		<value>3000</value>
	</property>
</bean>

<!-- Memcached 客户端 -->
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
	<constructor-arg>
		<value>neeaMemcachedPool</value>
	</constructor-arg>
	
</bean>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。