|
一、序言
有了緩存,還是喜歡用注解去使用,本想和spring 寫一個(gè)類似ehcache 的東西,后來發(fā)google 已經(jīng)提供了spring 和memcache 的 注解配置,那就先拿來用用了~。~。
二、基本配置:
2.1 先把spring 和 memcached 結(jié)合起來,創(chuàng)建一個(gè)spring-xmemcached.xml 的文件
- <!-- 用這個(gè)代替xmemcacheClient去構(gòu)建client -->
- <bean id="xmemcacheBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
- <!-- 暫時(shí)我們就用兩個(gè)端口 -->
- <constructor-arg>
- <list>
- <bean class="java.net.InetSocketAddress">
- <constructor-arg>
- <value>localhost</value>
- </constructor-arg>
- <constructor-arg>
- <value>11211</value>
- </constructor-arg>
- </bean>
- <bean class="java.net.InetSocketAddress">
- <constructor-arg>
- <value>localhost</value>
- </constructor-arg>
- <constructor-arg>
- <value>11212</value>
- </constructor-arg>
- </bean>
- </list>
- </constructor-arg>
- <!-- 權(quán)重配置 -->
- <constructor-arg>
- <list>
- <value>1</value>
- <value>2</value>
- </list>
- </constructor-arg>
- <!--</property>-->
- <!-- nio 連接池 配置,默認(rèn)是 1 -->
- <property name="connectionPoolSize" value="2"/>
- <!-- 二進(jìn)制協(xié)議 ,默認(rèn)是 TextCommandFactory-->
- <property name="commandFactory">
- <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>
- </property>
- <!--分布式策略 一致性hash,默認(rèn)是 ArrayMemcachedSessionLocator -->
- <property name="sessionLocator">
- <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
- </property>
- <!--序列化轉(zhuǎn)換器,默認(rèn)就是這個(gè) -->
- <!--<property name="transcoder" value="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>-->
- <!--字節(jié)緩沖器,不知道為啥這玩意兒過時(shí)了,源碼默認(rèn)采用的這個(gè)??! -->
- <!--<property name="bufferAllocator" value="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"/>-->
- </bean>
- <!-- 配置一個(gè)客戶端 -->
- <bean name="xmemcachedClient" factory-bean="xmemcacheBuilder" factory-method="build" destroy-method="shutdown"/>
測(cè)試一下:可以操作就表示 基本配置成功了
- ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-xmemcached.xml");
- MemcachedClient client = (MemcachedClient) context.getBean("xmemcachedClient");
- System.out.println(client);
- client.add("name",100,"張三");
- System.out.println(client.get("name"));
- client.delete("name");
- System.out.println(client.get("name"));
還是可以參考 https://code.google.com/p/xmemcached/wiki/User_Guide_zh
三、spring+memcached 注解的試用
3.1 引入依賴:
- <dependency>
- <groupId> com.google.code.simple-spring-memcached </groupId>
- <artifactId> xmemcached-provider </artifactId>
- <version>3.5.0</version>
- </dependency>
3.2 基本配置:
- <!-- 輕輕的掃描一下 -->
- <context:component-scan base-package="com.raycloud.plugin.memcache"/>
- <!-- AOP 你懂的 -->
- <aop:aspectj-autoproxy />
- <!-- simple-spring-memcached 目錄下的 -->
- <import resource="classpath:simplesm-context.xml" />
- <!-- 默認(rèn)一個(gè)client -->
- <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
- <property name="cacheClientFactory">
- <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
- </property>
- <property name="addressProvider">
- <bean class="com.google.code.ssm.config.DefaultAddressProvider">
- <property name="address" value="127.0.0.1:11211,127.0.0.1:11212" />
- </bean>
- </property>
- <!-- 一致性hash ~。~ -->
- <property name="configuration">
- <bean class="com.google.code.ssm.providers.CacheConfiguration">
- <property name="consistentHashing" value="true" />
- </bean>
- </property>
- </bean>
- <!-- 這玩意兒在3.2 后,文檔可以指定順序 以及 攔截器 前后執(zhí)行 - -!暫時(shí)沒用過,加上不報(bào)錯(cuò) -->
- <bean class="com.google.code.ssm.Settings">
- <property name="order" value="500" />
- </bean>
3.3 測(cè)試一下:我們建立TestDao 和 Test 類,記得在掃描的package 下哦!
- import com.google.code.ssm.api.ParameterValueKeyProvider;
- import com.google.code.ssm.api.ReadThroughSingleCache;
- import org.springframework.stereotype.Component;
-
- /**
- * Created by qiqiang on 2014/12/22.
- */
- @Component
- public class TestDao {
- @ReadThroughSingleCache(namespace = "test", expiration = 30000)
- public long getUserById(@ParameterValueKeyProvider long id) throws Exception{
- System.out.println("沒有緩存命中");
- return id;
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations="classpath:spring/spring-xmemcached.xml")
- public class Test {
- @Autowired
- private TestDao testDao;
- @org.junit.Test
- public void x(){
- try {
- System.out.println( testDao.getUserById(2l)+"------------------------");
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
忘記了,還得加測(cè)試的依賴:
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.2.4.RELEASE</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
3.4 OK 就測(cè)試完成了,當(dāng)然你也可以用spring 定義的緩存接口,和 ehcache 類似的啦,看配置:
為了配套依賴:
- <dependency>
- <groupId> com.google.code.simple-spring-memcached </groupId>
- <artifactId>spring-cache</artifactId>
- <version>3.5.0</version>
- </dependency>
- <cache:annotation-driven />
- <!-- 這里的cacheManager 緩存名字是默認(rèn)的,要改,就參考我spring+ehcache 的配置改 -->
- <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
- <property name="caches">
- <set>
- <bean class="com.google.code.ssm.spring.SSMCache">
- <constructor-arg name="cache" index="0" ref="defaultCache" />
- <!-- 默認(rèn) 5 minutes -->
- <constructor-arg name="expiration" index="1" value="300" />
- <!-- @CacheEvict(..., "allEntries" = true) won't work because allowClear is false,
- so we won't flush accidentally all entries from memcached instance -->
- <!-- 這里表示我們不會(huì)全部清除所有緩存,使用ehcache 的時(shí)候我們就會(huì)發(fā)現(xiàn), @CacheEvict(..., "allEntries" = true)
- 它是按 配置的緩存名 就行清除的,而memcached 我們是通過namespace 進(jìn)行清除的,還有指定時(shí)間,這是我最喜歡的了~。~
- -->
- <constructor-arg name="allowClear" index="2" value="false" />
- </bean>
- </set>
- </property>
- </bean>
-
- <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
- <property name="cacheName" value="defaultCache"/>
- <property name="cacheClientFactory">
- <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/>
- </property>
- <property name="addressProvider">
- <bean class="com.google.code.ssm.config.DefaultAddressProvider">
- <property name="address" value="127.0.0.1:11211,127.0.0.1:11212"/>
- </bean>
- </property>
- <property name="configuration">
- <bean class="com.google.code.ssm.providers.CacheConfiguration">
- <property name="consistentHashing" value="true"/>
- </bean>
- </property>
- </bean>
3.5 現(xiàn)在來看看 這種方式得測(cè)試,應(yīng)該比較熟悉~。~!
TestDao2 和上面液氧,就改了下名字
- @Component
- public class TestDao2 {
- @CachePut(value = "defaultCache",key="#id")
- public long getUserById(long id) throws Exception{
- System.out.println("沒有緩存命中");
- return id;
- }
-
- @Cacheable(value = "defaultCache")
- public String getList(String author) throws Exception {
- System.out.println("沒有緩存命中");
- return author;
- }
- }
Test,和上面一樣
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations="classpath:spring/spring-xmemcached.xml")
- public class Test {
- @Autowired
- private TestDao2 testDao2;
- @org.junit.Test
- public void x(){
- try {
- System.out.println( testDao2.getUserById(2l)+"------------------------");
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
同樣可以實(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/
|