小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

simple

 喂我喜歡伱 2015-03-17

一、序言

       有了緩存,還是喜歡用注解去使用,本想和spring 寫一個(gè)類似ehcache 的東西,后來發(fā)google 已經(jīng)提供了spring 和memcache 的 注解配置,那就先拿來用用了~。~。

 

二、基本配置:

       2.1 先把spring 和 memcached 結(jié)合起來,創(chuàng)建一個(gè)spring-xmemcached.xml 的文件

       

Java代碼  收藏代碼
  1. <!-- 用這個(gè)代替xmemcacheClient去構(gòu)建client  -->  
  2.     <bean id="xmemcacheBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">  
  3.         <!-- 暫時(shí)我們就用兩個(gè)端口 -->  
  4.         <constructor-arg>  
  5.             <list>  
  6.                 <bean class="java.net.InetSocketAddress">  
  7.                     <constructor-arg>  
  8.                         <value>localhost</value>  
  9.                     </constructor-arg>  
  10.                     <constructor-arg>  
  11.                         <value>11211</value>  
  12.                     </constructor-arg>  
  13.                 </bean>  
  14.                 <bean class="java.net.InetSocketAddress">  
  15.                     <constructor-arg>  
  16.                         <value>localhost</value>  
  17.                     </constructor-arg>  
  18.                     <constructor-arg>  
  19.                         <value>11212</value>  
  20.                     </constructor-arg>  
  21.                 </bean>  
  22.             </list>  
  23.         </constructor-arg>  
  24.         <!-- 權(quán)重配置 -->  
  25.         <constructor-arg>  
  26.             <list>  
  27.                 <value>1</value>  
  28.                 <value>2</value>  
  29.             </list>  
  30.         </constructor-arg>  
  31.         <!--</property>-->  
  32.         <!-- nio 連接池 配置,默認(rèn)是 1 -->  
  33.         <property name="connectionPoolSize" value="2"/>  
  34.         <!-- 二進(jìn)制協(xié)議 ,默認(rèn)是 TextCommandFactory-->  
  35.         <property name="commandFactory">  
  36.             <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>  
  37.         </property>  
  38.         <!--分布式策略 一致性hash,默認(rèn)是  ArrayMemcachedSessionLocator -->  
  39.         <property name="sessionLocator">  
  40.             <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>  
  41.         </property>  
  42.         <!--序列化轉(zhuǎn)換器,默認(rèn)就是這個(gè) -->  
  43.         <!--<property name="transcoder" value="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>-->  
  44.         <!--字節(jié)緩沖器,不知道為啥這玩意兒過時(shí)了,源碼默認(rèn)采用的這個(gè)??! -->  
  45.         <!--<property name="bufferAllocator" value="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"/>-->  
  46.     </bean>  
  47.     <!-- 配置一個(gè)客戶端 -->  
  48.     <bean name="xmemcachedClient" factory-bean="xmemcacheBuilder" factory-method="build" destroy-method="shutdown"/>  

 

    測(cè)試一下:可以操作就表示 基本配置成功了

    

Java代碼  收藏代碼
  1. ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-xmemcached.xml");  
  2.       MemcachedClient client = (MemcachedClient) context.getBean("xmemcachedClient");  
  3.       System.out.println(client);  
  4.       client.add("name",100,"張三");  
  5.       System.out.println(client.get("name"));  
  6.       client.delete("name");  
  7.       System.out.println(client.get("name"));  

    還是可以參考 https://code.google.com/p/xmemcached/wiki/User_Guide_zh

 

 

 三、spring+memcached 注解的試用

       3.1 引入依賴:

             

Java代碼  收藏代碼
  1. <dependency>  
  2.           <groupId> com.google.code.simple-spring-memcached </groupId>  
  3.           <artifactId> xmemcached-provider </artifactId>  
  4.           <version>3.5.0</version>  
  5.       </dependency>  

 

       3.2 基本配置:

      

Java代碼  收藏代碼
  1. <!-- 輕輕的掃描一下 -->  
  2.    <context:component-scan base-package="com.raycloud.plugin.memcache"/>  
  3.    <!-- AOP 你懂的 -->  
  4.    <aop:aspectj-autoproxy />  
  5.     <!-- simple-spring-memcached 目錄下的 -->  
  6.    <import resource="classpath:simplesm-context.xml" />  
  7.    <!-- 默認(rèn)一個(gè)client -->  
  8.    <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">  
  9.        <property name="cacheClientFactory">  
  10.            <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />  
  11.        </property>  
  12.        <property name="addressProvider">  
  13.            <bean class="com.google.code.ssm.config.DefaultAddressProvider">  
  14.                <property name="address" value="127.0.0.1:11211,127.0.0.1:11212" />  
  15.            </bean>  
  16.        </property>  
  17.        <!-- 一致性hash ~。~ -->  
  18.        <property name="configuration">  
  19.            <bean class="com.google.code.ssm.providers.CacheConfiguration">  
  20.                <property name="consistentHashing" value="true" />  
  21.            </bean>  
  22.        </property>  
  23.    </bean>  
  24.    <!-- 這玩意兒在3.2 后,文檔可以指定順序 以及 攔截器 前后執(zhí)行 - -!暫時(shí)沒用過,加上不報(bào)錯(cuò) -->  
  25.    <bean class="com.google.code.ssm.Settings">  
  26.        <property name="order" value="500" />  
  27.    </bean>  

 

      3.3 測(cè)試一下:我們建立TestDao 和 Test 類,記得在掃描的package 下哦!

      

Java代碼  收藏代碼
  1. import com.google.code.ssm.api.ParameterValueKeyProvider;  
  2. import com.google.code.ssm.api.ReadThroughSingleCache;  
  3. import org.springframework.stereotype.Component;  
  4.   
  5. /** 
  6.  * Created by qiqiang on 2014/12/22. 
  7.  */  
  8. @Component  
  9. public class TestDao {  
  10.     @ReadThroughSingleCache(namespace = "test", expiration = 30000)  
  11.     public long getUserById(@ParameterValueKeyProvider long id) throws Exception{  
  12.         System.out.println("沒有緩存命中");  
  13.         return id;  
  14.     }  
  15. }  

 

   

Java代碼  收藏代碼
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations="classpath:spring/spring-xmemcached.xml")  
  3. public class Test {  
  4.     @Autowired  
  5.     private TestDao testDao;  
  6.     @org.junit.Test  
  7.     public void x(){  
  8.         try {  
  9.             System.out.println( testDao.getUserById(2l)+"------------------------");  
  10.         }catch (Exception e){  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  14. }  

   

    忘記了,還得加測(cè)試的依賴:

   

Java代碼  收藏代碼
  1.   <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-test</artifactId>  
  4.             <version>3.2.4.RELEASE</version>  
  5.             <scope>test</scope>  
  6.         </dependency>  
  7. <dependency>  
  8.             <groupId>junit</groupId>  
  9.             <artifactId>junit</artifactId>  
  10.             <version>4.11</version>  
  11.             <scope>test</scope>  
  12.         </dependency>  

   

    3.4 OK 就測(cè)試完成了,當(dāng)然你也可以用spring 定義的緩存接口,和 ehcache 類似的啦,看配置:

         為了配套依賴:

       

Java代碼  收藏代碼
  1. <dependency>  
  2.          <groupId> com.google.code.simple-spring-memcached </groupId>  
  3.          <artifactId>spring-cache</artifactId>  
  4.          <version>3.5.0</version>  
  5.      </dependency>  

 

 

    

Java代碼  收藏代碼
  1. <cache:annotation-driven />  
  2.    <!-- 這里的cacheManager 緩存名字是默認(rèn)的,要改,就參考我spring+ehcache 的配置改 -->  
  3.    <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">  
  4.        <property name="caches">  
  5.            <set>  
  6.                <bean class="com.google.code.ssm.spring.SSMCache">  
  7.                    <constructor-arg name="cache" index="0" ref="defaultCache" />  
  8.                    <!-- 默認(rèn) 5 minutes -->  
  9.                    <constructor-arg name="expiration" index="1" value="300" />  
  10.                    <!-- @CacheEvict(..., "allEntries" = true) won't work because allowClear is false,  
  11.                     so we won't flush accidentally all entries from memcached instance -->  
  12.                    <!-- 這里表示我們不會(huì)全部清除所有緩存,使用ehcache 的時(shí)候我們就會(huì)發(fā)現(xiàn), @CacheEvict(..., "allEntries" = true)  
  13.                    它是按 配置的緩存名 就行清除的,而memcached 我們是通過namespace 進(jìn)行清除的,還有指定時(shí)間,這是我最喜歡的了~。~  
  14.                     -->  
  15.                    <constructor-arg name="allowClear" index="2" value="false" />  
  16.                </bean>  
  17.            </set>  
  18.        </property>  
  19.    </bean>  
  20.      
  21.    <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">  
  22.        <property name="cacheName" value="defaultCache"/>  
  23.        <property name="cacheClientFactory">  
  24.            <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/>  
  25.        </property>  
  26.        <property name="addressProvider">  
  27.            <bean class="com.google.code.ssm.config.DefaultAddressProvider">  
  28.                <property name="address" value="127.0.0.1:11211,127.0.0.1:11212"/>  
  29.            </bean>  
  30.        </property>  
  31.        <property name="configuration">  
  32.            <bean class="com.google.code.ssm.providers.CacheConfiguration">  
  33.                <property name="consistentHashing" value="true"/>  
  34.            </bean>  
  35.        </property>  
  36.    </bean>  

 
   3.5 現(xiàn)在來看看 這種方式得測(cè)試,應(yīng)該比較熟悉~。~!

     TestDao2 和上面液氧,就改了下名字

Java代碼  收藏代碼
  1. @Component  
  2. public class TestDao2 {  
  3.     @CachePut(value = "defaultCache",key="#id")  
  4.     public long getUserById(long id) throws Exception{  
  5.         System.out.println("沒有緩存命中");  
  6.         return id;  
  7.     }  
  8.   
  9.     @Cacheable(value = "defaultCache")  
  10.     public String getList(String author) throws Exception {  
  11.         System.out.println("沒有緩存命中");  
  12.         return author;  
  13.     }  
  14. }  

    Test,和上面一樣

    

Java代碼  收藏代碼
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations="classpath:spring/spring-xmemcached.xml")  
  3. public class Test {  
  4.     @Autowired  
  5.     private TestDao2 testDao2;  
  6.     @org.junit.Test  
  7.     public void x(){  
  8.         try {  
  9.             System.out.println( testDao2.getUserById(2l)+"------------------------");  
  10.         }catch (Exception e){  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  14. }  

 

    同樣可以實(shí)現(xiàn)緩存

 

小結(jié):

     1.這里介紹了spring +xmemcached 配置,以及注解的配置,關(guān)于注解的一些詳細(xì)資料下次介紹

     2.由于有namespace 和 時(shí)間的控制,使用起來非常方便,集群部署,容錯(cuò) 都比較舒服的~。~,有錯(cuò)誤請(qǐng)指出!

     3.上面的來源 大部分還是文檔介紹,當(dāng)翻譯下而已,更多的大家可以參考下面資料!

 

源碼

https://github.com/ragnor/simple-spring-memcached

文檔

https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started

https://code.google.com/p/simple-spring-memcached/wiki/UserGuide

https://code.google.com/p/xmemcached/wiki/User_Guide_zh

別人的例子

http:///2013/05/31/simple-spring-memcached-spring-caching-abstraction-and-memcached/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多