天天看點

spring內建memcached示例二

轉載自:http://blog.csdn.net/truong/article/details/17362371

一、前期準備

1)  下載下傳memcached服務端memcached-1.2.6-win32-bin.zip,位址:http:

//code.jellycan.com/memcached/

2)  下載下傳java版用戶端 java_memcached-release_2.6.1.zip

3)  解壓縮memcached-1.2.6-win32-bin.zip到指定目錄,例如:D:\memcached-1.2.6-win32 ,

在終端(即cmd指令行界面)  

D:\memcached-1.2.6-win32\memcached.exe -d install

D:\memcached\memcached.exe -d start

這樣memcache就會作為windows系統服務在每次開機時啟動memcache服務。

  常用指令   -p 監聽的端口 

-l 連接配接的IP位址, 預設是本機 

-d start 啟動memcached服務 

-d restart 重起memcached服務 

-d stop|shutdown 關閉正在運作的memcached服務 

-d install 安裝memcached服務 

-d uninstall 解除安裝memcached服務 

-u 以的身份運作 (僅在以root運作的時候有效) 

-m 最大記憶體使用,機關MB。預設64MB 

-M 記憶體耗盡時傳回錯誤,而不是删除項 

-c 最大同時連接配接數,預設是1024 

-f 塊大小增長因子,預設是1.25 

-n 最小配置設定空間,key+value+flags預設是48 

-h 顯示幫助        二、執行個體代碼 spring-memcache.xml  配置pool和memcache用戶端

spring內建memcached示例二
1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context-2.5.xsd
12     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
13     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
14 
15 
16     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
17         <property name="locations">
18             <list>
19                 <value>classpath:properties/memcache.properties</value>
20             </list>
21         </property>
22     </bean>
23 
24     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
25         factory-method="getInstance" init-method="initialize"
26         destroy-method="shutDown">
27 
28         <constructor-arg>
29             <value>memCachedPool</value>
30         </constructor-arg>
31         
32         <property name="servers">
33             <list>
34                 <value>${memcache.server}</value>
35             </list>
36         </property>
37         
38         <property name="initConn">
39             <value>${memcache.initConn}</value>
40         </property>
41         
42         <property name="minConn">
43             <value>${memcache.minConn}</value>
44         </property>
45 
46         <property name="maxConn">
47             <value>${memcache.maxConn}</value>
48         </property>
49 
50         <property name="maintSleep">
51             <value>${memcache.maintSleep}</value>
52         </property>
53 
54         <property name="nagle">
55             <value>${memcache.nagle}</value>
56         </property>
57 
58         <property name="socketTO">
59             <value>${memcache.socketTO}</value>
60         </property>
61     </bean>
62 
63     <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
64         <constructor-arg>
65             <value>memCachedPool</value>
66         </constructor-arg>
67     </bean>
68 
69 </beans>      
spring內建memcached示例二

memcache.properties memcache的連接配接屬性 這是本機做伺服器的,如果是其它機器,換ip 端口即可

spring內建memcached示例二
memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000      
spring內建memcached示例二

TestMemcache.java測試類   用的是junit4

spring內建memcached示例二
1 package com.pis.memcache;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import com.danga.MemCached.MemCachedClient;
 9 
10 public class TestMemcache {
11     MemCachedClient memCachedClient;
12     @Before
13     public void beforeTest(){
14         
15         ApplicationContext atx = new ClassPathXmlApplicationContext("/spring/spring-memcache.xml");
16         memCachedClient = (MemCachedClient)atx.getBean("memCachedClient");
17     }
18     
19     
20     @Test
21     public void TestMem(){
22         memCachedClient.set("name", "han");
23         
24         System.out.println(memCachedClient.get("name"));
25     }
26     
27     
28     
29 }