打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
simple

一、序言

       有了缓存,还是喜欢用注解去使用,本想和spring 写一个类似ehcache 的东西,后来发google 已经提供了spring 和memcache 的 注解配置,那就先拿来用用了~。~。

 

二、基本配置:

       2.1 先把spring 和 memcached 结合起来,创建一个spring-xmemcached.xml 的文件

       

Java代码  
  1. <!-- 用这个代替xmemcacheClient去构建client  -->  
  2.     <bean id="xmemcacheBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">  
  3.         <!-- 暂时我们就用两个端口 -->  
  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.         <!-- 权重配置 -->  
  25.         <constructor-arg>  
  26.             <list>  
  27.                 <value>1</value>  
  28.                 <value>2</value>  
  29.             </list>  
  30.         </constructor-arg>  
  31.         <!--</property>-->  
  32.         <!-- nio 连接池 配置,默认是 1 -->  
  33.         <property name="connectionPoolSize" value="2"/>  
  34.         <!-- 二进制协议 ,默认是 TextCommandFactory-->  
  35.         <property name="commandFactory">  
  36.             <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>  
  37.         </property>  
  38.         <!--分布式策略 一致性hash,默认是  ArrayMemcachedSessionLocator -->  
  39.         <property name="sessionLocator">  
  40.             <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>  
  41.         </property>  
  42.         <!--序列化转换器,默认就是这个 -->  
  43.         <!--<property name="transcoder" value="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>-->  
  44.         <!--字节缓冲器,不知道为啥这玩意儿过时了,源码默认采用的这个!! -->  
  45.         <!--<property name="bufferAllocator" value="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"/>-->  
  46.     </bean>  
  47.     <!-- 配置一个客户端 -->  
  48.     <bean name="xmemcachedClient" factory-bean="xmemcacheBuilder" factory-method="build" destroy-method="shutdown"/>  

 

    测试一下:可以操作就表示 基本配置成功了

    

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.    <!-- 默认一个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 后,文档可以指定顺序 以及 拦截器 前后执行 - -!暂时没用过,加上不报错 -->  
  25.    <bean class="com.google.code.ssm.Settings">  
  26.        <property name="order" value="500" />  
  27.    </bean>  

 

      3.3 测试一下:我们建立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. }  

   

    忘记了,还得加测试的依赖:

   

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 就测试完成了,当然你也可以用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 缓存名字是默认的,要改,就参考我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.                    <!-- 默认 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.                    <!-- 这里表示我们不会全部清除所有缓存,使用ehcache 的时候我们就会发现, @CacheEvict(..., "allEntries" = true)  
  13.                    它是按 配置的缓存名 就行清除的,而memcached 我们是通过namespace 进行清除的,还有指定时间,这是我最喜欢的了~。~  
  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 现在来看看 这种方式得测试,应该比较熟悉~。~!

     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. }  

 

    同样可以实现缓存

 

小结:

     1.这里介绍了spring +xmemcached 配置,以及注解的配置,关于注解的一些详细资料下次介绍

     2.由于有namespace 和 时间的控制,使用起来非常方便,集群部署,容错 都比较舒服的~。~,有错误请指出!

     3.上面的来源 大部分还是文档介绍,当翻译下而已,更多的大家可以参考下面资料!

 

源码

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://weblog4j.com/2013/05/31/simple-spring-memcached-spring-caching-abstraction-and-memcached/

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Simple
Xmemcached使用【二】
xplanner0.7配置使用
在EasyJWeb使用spring容器
解决Spring bean Date转化问题
spring注入null对象
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服