使用memcached

1.加載commons-pool-1.5.6.jar、java_memcached-release_2.6.6.jar、slf4j-api-1.6.1.jar、slf4j-simple-1.6.1.jar

2.創建memcached工具類:

  1. public class MemcachedUtil { 
  2.  
  3.     /**
  4.      * memcached客戶端單例
  5.      */ 
  6.     private static MemCachedClient cachedClient = new MemCachedClient(); 
  7.      
  8.     /**
  9.      * 初始化連接池
  10.      */ 
  11.     static
  12.         //獲取連接池的實例 
  13.         SockIOPool pool = SockIOPool.getInstance(); 
  14.          
  15.         //服務器列表及其權重 
  16.         String[] servers = {"127.0.0.1:11211"}; 
  17.         Integer[] weights = {3}; 
  18.          
  19.         //設置服務器信息 
  20.         pool.setServers(servers); 
  21.         pool.setWeights(weights); 
  22.          
  23.         //設置初始連接數、最小連接數、最大連接數、最大處理時間 
  24.         pool.setInitConn(10); 
  25.         pool.setMinConn(10); 
  26.         pool.setMaxConn(1000); 
  27.         pool.setMaxIdle(1000*60*60); 
  28.          
  29.         //設置連接池守護線程的睡眠時間 
  30.         pool.setMaintSleep(60); 
  31.          
  32.         //設置TCP參數,連接超時 
  33.         pool.setNagle(false); 
  34.         pool.setSocketTO(60); 
  35.         pool.setSocketConnectTO(0); 
  36.          
  37.         //初始化並啓動連接池 
  38.         pool.initialize(); 
  39.          
  40.         //壓縮設置,超過指定大小的都壓縮 
  41. //      cachedClient.setCompressEnable(true); 
  42. //      cachedClient.setCompressThreshold(1024*1024); 
  43.     } 
  44.      
  45.     private MemcachedUtil(){ 
  46.     } 
  47.      
  48.     public static boolean add(String key, Object value) { 
  49.         return cachedClient.add(key, value); 
  50.     } 
  51.      
  52.     public static boolean add(String key, Object value, Integer expire) { 
  53.         return cachedClient.add(key, value, expire); 
  54.     } 
  55.      
  56.     public static boolean put(String key, Object value) { 
  57.         return cachedClient.set(key, value); 
  58.     } 
  59.      
  60.     public static boolean put(String key, Object value, Integer expire) { 
  61.         return cachedClient.set(key, value, expire); 
  62.     } 
  63.      
  64.     public static boolean replace(String key, Object value) { 
  65.         return cachedClient.replace(key, value); 
  66.     } 
  67.      
  68.     public static boolean replace(String key, Object value, Integer expire) { 
  69.         return cachedClient.replace(key, value, expire); 
  70.     } 
  71.      
  72.     public static Object get(String key) { 
  73.         return cachedClient.get(key); 
  74.     } 
  75.      
public class MemcachedUtil {

	/**
	 * memcached客戶端單例
	 */
	private static MemCachedClient cachedClient = new MemCachedClient();
	
	/**
	 * 初始化連接池
	 */
	static {
		//獲取連接池的實例
		SockIOPool pool = SockIOPool.getInstance();
		
		//服務器列表及其權重
		String[] servers = {"127.0.0.1:11211"};
		Integer[] weights = {3};
		
		//設置服務器信息
		pool.setServers(servers);
		pool.setWeights(weights);
		
		//設置初始連接數、最小連接數、最大連接數、最大處理時間
		pool.setInitConn(10);
		pool.setMinConn(10);
		pool.setMaxConn(1000);
		pool.setMaxIdle(1000*60*60);
		
		//設置連接池守護線程的睡眠時間
		pool.setMaintSleep(60);
		
		//設置TCP參數,連接超時
		pool.setNagle(false);
		pool.setSocketTO(60);
		pool.setSocketConnectTO(0);
		
		//初始化並啓動連接池
		pool.initialize();
		
		//壓縮設置,超過指定大小的都壓縮
//		cachedClient.setCompressEnable(true);
//		cachedClient.setCompressThreshold(1024*1024);
	}
	
	private MemcachedUtil(){
	}
	
	public static boolean add(String key, Object value) {
		return cachedClient.add(key, value);
	}
	
	public static boolean add(String key, Object value, Integer expire) {
		return cachedClient.add(key, value, expire);
	}
	
	public static boolean put(String key, Object value) {
		return cachedClient.set(key, value);
	}
	
	public static boolean put(String key, Object value, Integer expire) {
		return cachedClient.set(key, value, expire);
	}
	
	public static boolean replace(String key, Object value) {
		return cachedClient.replace(key, value);
	}
	
	public static boolean replace(String key, Object value, Integer expire) {
		return cachedClient.replace(key, value, expire);
	}
	
	public static Object get(String key) {
		return cachedClient.get(key);
	}
	
}
3. 創建需要緩存的對象:

  1. public class UserBean implements Serializable { 
  2.  
  3.     private static final long serialVersionUID = 9174194101246733501L; 
  4.  
  5.     private String username; 
  6.      
  7.     private String password; 
  8.      
  9.     public UserBean(String username, String password) { 
  10.         this.username = username; 
  11.         this.password = password; 
  12.     } 
  13.      
  14.     public String getUsername() { 
  15.         return username; 
  16.     } 
  17.      
  18.     public void setUsername(String username) { 
  19.         this.username = username; 
  20.     } 
  21.      
  22.     public String getPassword() { 
  23.         return password; 
  24.     } 
  25.      
  26.     public void setPassword(String password) { 
  27.         this.password = password; 
  28.     } 
  29.      
  30.     @Override 
  31.     public int hashCode() { 
  32.         final int prime = 31
  33.         int result = 1
  34.         result = prime * result 
  35.                 + ((password == null) ? 0 : password.hashCode()); 
  36.         result = prime * result 
  37.                 + ((username == null) ? 0 : username.hashCode()); 
  38.         return result; 
  39.     } 
  40.  
  41.     @Override 
  42.     public boolean equals(Object obj) { 
  43.         if (this == obj) 
  44.             return true
  45.         if (obj == null
  46.             return false
  47.         if (getClass() != obj.getClass()) 
  48.             return false
  49.         UserBean other = (UserBean) obj; 
  50.         if (password == null) { 
  51.             if (other.password != null
  52.                 return false
  53.         } else if (!password.equals(other.password)) 
  54.             return false
  55.         if (username == null) { 
  56.             if (other.username != null
  57.                 return false
  58.         } else if (!username.equals(other.username)) 
  59.             return false
  60.         return true
  61.     } 
  62.  
  63.     @Override 
  64.     public String toString() { 
  65.         return "username:" + username + ",password:" + password; 
  66.     } 
public class UserBean implements Serializable {

	private static final long serialVersionUID = 9174194101246733501L;

	private String username;
	
	private String password;
	
	public UserBean(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserBean other = (UserBean) obj;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "username:" + username + ",password:" + password;
	}
}
4.創建測試用例:

  1. public class MemcachedUtilTest { 
  2.  
  3.     @Test 
  4.     public void testMemcached() { 
  5.         MemcachedUtil.put("hello", "world", 60); 
  6.         String hello = (String) MemcachedUtil.get("hello"); 
  7.         Assert.assertEquals("world", hello); 
  8.          
  9.         for(int i = 0; i < 10000000; ++i) { 
  10.             UserBean userBean = new UserBean("Jason" + i, "123456-" + i); 
  11.             MemcachedUtil.put("user" + i, userBean, 60); 
  12.             Object obj = MemcachedUtil.get("user" + i); 
  13.             Assert.assertEquals(userBean, obj); 
  14.         } 
  15.     } 
public class MemcachedUtilTest {

	@Test
	public void testMemcached() {
		MemcachedUtil.put("hello", "world", 60);
		String hello = (String) MemcachedUtil.get("hello");
		Assert.assertEquals("world", hello);
		
		for(int i = 0; i < 10000000; ++i) {
			UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
			MemcachedUtil.put("user" + i, userBean, 60);
			Object obj = MemcachedUtil.get("user" + i);
			Assert.assertEquals(userBean, obj);
		}
	}
}

5.通過spring注入memcached:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans.xsd"> 
  6.  
  7.     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"  
  8.         factory-method="getInstance" init-method="initialize"> 
  9.         <constructor-arg> 
  10.             <value>neeaMemcachedPool</value> 
  11.         </constructor-arg> 
  12.         <property name="servers"> 
  13.             <list> 
  14.                 <value>127.0.0.1:11211</value> 
  15.             </list> 
  16.         </property> 
  17.         <property name="initConn"> 
  18.             <value>20</value> 
  19.         </property> 
  20.         <property name="minConn"> 
  21.             <value>10</value> 
  22.         </property> 
  23.         <property name="maxConn"> 
  24.             <value>50</value> 
  25.         </property> 
  26.         <property name="nagle"> 
  27.             <value>false</value> 
  28.         </property> 
  29.         <property name="socketTO"> 
  30.             <value>3000</value> 
  31.         </property> 
  32.     </bean> 
  33.     <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"> 
  34.         <constructor-arg> 
  35.             <value>neeaMemcachedPool</value> 
  36.         </constructor-arg> 
  37.     </bean> 
  38. </beans> 
<?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.xsd">

	<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" 
		factory-method="getInstance" init-method="initialize">
		<constructor-arg>
			<value>neeaMemcachedPool</value>
		</constructor-arg>
		<property name="servers">
			<list>
				<value>127.0.0.1:11211</value>
			</list>
		</property>
		<property name="initConn">
			<value>20</value>
		</property>
		<property name="minConn">
			<value>10</value>
		</property>
		<property name="maxConn">
			<value>50</value>
		</property>
		<property name="nagle">
			<value>false</value>
		</property>
		<property name="socketTO">
			<value>3000</value>
		</property>
	</bean>
	<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
		<constructor-arg>
			<value>neeaMemcachedPool</value>
		</constructor-arg>
	</bean>
</beans>

6.創建測試用例:

  1. public class MemcachedSpringTest { 
  2.  
  3.     private MemCachedClient cachedClient; 
  4.      
  5.     @Before 
  6.     public void init() { 
  7.         ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/beans.xml"); 
  8.         cachedClient = (MemCachedClient)context.getBean("memcachedClient"); 
  9.     } 
  10.      
  11.     @Test 
  12.     public void testMemcachedSpring() { 
  13.         UserBean user = new UserBean("lou", "jason"); 
  14.         cachedClient.set("user", user); 
  15.         UserBean cachedBean = (UserBean)user; 
  16.         Assert.assertEquals(user, cachedBean); 
  17.     } 

 

    原文地址:http://blog.csdn.net/loujinhe/article/details/8491673

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章