Fork me on GitHub

simple-spring-memcached缓存搭建

项目中使用的缓存经常是知道使用,没有试过搭建起它。刚好这次自己的毕业可以用来搭建缓存。其他不多说了,直接看操作吧。首先在pom.xml中依赖simple-spring-memcached的架包。

1      <dependency>
2             <groupId>com.google.code.simple-spring-memcached</groupId>
3             <artifactId>xmemcached-provider</artifactId>
4             <version>3.3.0</version>
5         </dependency>

在项目中增加一个applicationContext-ssm.xml文件,里面的内容为:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 8         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 9         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
10 
11     <import resource="classpath:simplesm-context.xml" />
12     <aop:aspectj-autoproxy />
13     <context:annotation-config />
14     
15     <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
16         <property name="cacheClientFactory">
17             <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
18         </property>
19         <property name="addressProvider">
20             <bean class="com.google.code.ssm.config.DefaultAddressProvider">
21                   <property name="address" value="127.0.0.1:11211" />
22             </bean>
23         </property>
24         <property name="configuration">
25             <bean class="com.google.code.ssm.providers.xmemcached.XMemcachedConfiguration">
26                 <property name="consistentHashing"><!--consistentHashing属性定义了缓存节点的查找方法: 是否使用哈希 -->
27                     <value>true</value>
28                 </property>
29                 <property name="connectionPoolSize">
30                     <value>1</value>
31                 </property>
32                 <property name="optimizeGet">
33                     <value>false</value>
34                 </property>
35                 <property name="optimizeMergeBuffer">
36                     <value>false</value>
37                 </property>
38                 <property name="mergeFactor">
39                     <value>50</value>
40                 </property>
41                 <property name="useBinaryProtocol">
42                     <value>true</value>
43                 </property>
44                 <property name="connectionTimeout">
45                     <value>2000</value>
46                 </property>
47                 <property name="operationTimeout">
48                     <value>1000</value>
49                 </property>
50                 <property name="enableHeartBeat">
51                     <value>true</value>
52                 </property>
53                 <property name="failureMode">
54                     <value>false</value>
55                 </property>
56             </bean>
57         </property>
58             <!-- 该Memcached配置的Cache名称 一个应用中存在多个Memcached时,各个配置的cacheName必须不同。如果该值未设,系统默认为default -->
59         <property name="cacheName" value="default" />
60     </bean>
61 
62     <bean class="com.google.code.ssm.Settings"><!-- 这玩意儿在3.2 后,文档可以指定顺序 以及 拦截器 前后执行 - -!暂时没用过,加上不报错 -->
63         <property name="order" value="500" />
64     </bean>    
65 </beans>

最后在在代码中,增加

 1     /**
 2      * 根据订单号获取订单信息
 3      * @param orderNum
 4      * @return
 5      */
 6     @ReadThroughSingleCache(namespace = "test", expiration = 30000)
 7     public Order getOrder(@ParameterValueKeyProvider String orderNum) {
 8         System.out.println("缓存没有命中");
 9         return this.orderDao.getOrder(orderNum);
10     }

其中有许多注解的使用,这里就不一一说了。其中缓存使用的是我本地安装的memcached。项目运行起来,第一次查找订单的时候会打印缓存没有命中的消息,第二次就看不到了。缓存搭建成功。

posted @ 2016-06-03 21:36  鹏&鹏  阅读(2949)  评论(1编辑  收藏  举报