少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          學習了memcache,這是個好東西,分享一下自己的小實例,也方便以后查找使用

          一、前期準備

          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
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
              xmlns:cache
          ="http://www.springframework.org/schema/cache"
              xmlns:context
          ="http://www.springframework.org/schema/context"
              xmlns:mvc
          ="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
              xmlns:p
          ="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

              
          <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>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="maintSleep">
                      
          <value>3000</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>






          測試類:

          package com.abin.lee.spring.memcache;

          import org.junit.BeforeClass;
          import org.junit.Test;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import com.danga.MemCached.MemCachedClient;

          public class MemcacheUtilTest {
           static MemCachedClient memcachedClient;
           @BeforeClass
           public static void setUpBeforeClass() throws Exception {
            ApplicationContext context= new ClassPathXmlApplicationContext("com/abin/lee/spring/memcache/spring-memcache.xml");
            memcachedClient= (MemCachedClient)context.getBean("memcachedClient");
           }

           @Test
           public void test() {
            memcachedClient.set("name", "abin");
            System.out.println(memcachedClient.get("name"));
           }
          }

          posted on 2012-11-10 22:33 abin 閱讀(1354) 評論(1)  編輯  收藏 所屬分類: memcache

          Feedback

          # re: spring memcache 2013-03-04 11:53 http://www.easy518.com/
          http://www.easy518.com/  回復  更多評論
            

          主站蜘蛛池模板: 康平县| 屯门区| 杂多县| 静乐县| 嘉义市| 岳阳县| 桓台县| 赣榆县| 上饶市| 惠州市| 南安市| 永寿县| 宾阳县| 淮阳县| 芮城县| 泸州市| 克东县| 高唐县| 兴海县| 凌海市| 兰考县| 屯昌县| 都兰县| 宜君县| 尚志市| 长海县| 邮箱| 太白县| 麻城市| 始兴县| 谷城县| 大洼县| 渭南市| 南康市| 克东县| 沂源县| 怀仁县| 凌云县| 讷河市| 修水县| 名山县|