simple-spring-memcached緩存搭建

項目中使用的緩存常常是知道使用,沒有試過搭建起它。恰好此次本身的畢業能夠用來搭建緩存。其餘很少說了,直接看操做吧。首先在pom.xml中依賴simple-spring-memcached的架包。spring

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>

最後在在代碼中,增長app

 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。項目運行起來,第一次查找訂單的時候會打印緩存沒有命中的消息,第二次就看不到了。緩存搭建成功。ide

相關文章
相關標籤/搜索