使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCached的客户端开发变得超乎寻常的简单,只要一行代码就行:
@ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")
是不是很神奇?这行代码指定了MemCached的key,过期时间和命名空间。假设你的MemCached服务器IP是:196.168.10.101,端口是:12000,那么在数据调用的配置文件中只要加上下面配置代码就可以了:
1 <import resource="classpath:simplesm-context.xml" />
5 <bean id="memcachedConnectionBean" class="net.nelz.simplesm.config.MemcachedConnectionBean">
7 <property name="consistentHashing" value="true" />
9 <property name="nodeList" value="196.168.10.101:12000" />
5 </bean>
从simplesm-context.xml的内容中,可以看出它所封装的类和方法:
1 <bean id="memcachedClientFactory" class="net.nelz.simplesm.config.MemcachedClientFactory" >
3 property name="bean" ref="memcachedConnectionBean" />
5 </bean>
9 <bean id="memcachedClient" factory-bean="memcachedClientFactory" factory-method="createMemcachedClient" />
13 <bean id="methodStore" class="net.nelz.simplesm.aop.CacheKeyMethodStoreImpl" />
17 <bean id="net.nelz.simplesm.DefaultKeyProvider" class="net.nelz.simplesm.impl.DefaultKeyProvider">
19 <property name="methodStore" ref="methodStore" />
21 </bean>
25 <bean id="readThroughSingleCache" class="net.nelz.simplesm.aop.ReadThroughSingleCacheAdvice">
27 <property name="cache" ref="memcachedClient" />
29 <property name="methodStore" ref="methodStore" />
31 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
33 </bean>
35 <bean id="readThroughMultiCache" class="net.nelz.simplesm.aop.ReadThroughMultiCacheAdvice">
37 <property name="cache" ref="memcachedClient" />
39 <property name="methodStore" ref="methodStore" />
41 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
43 </bean>
45 <bean id="readThroughAssignCache" class="net.nelz.simplesm.aop.ReadThroughAssignCacheAdvice">
47 <property name="cache" ref="memcachedClient" />
49 <property name="methodStore" ref="methodStore" />
51 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
53 </bean>
54
55 <bean id="updateSingleCache" class="net.nelz.simplesm.aop.UpdateSingleCacheAdvice">
57 <property name="cache" ref="memcachedClient" />
59 <property name="methodStore" ref="methodStore" />
61 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
63 </bean>
64
65 <bean id="updateMultiCache" class="net.nelz.simplesm.aop.UpdateMultiCacheAdvice">
67 <property name="cache" ref="memcachedClient" />
69 <property name="methodStore" ref="methodStore" />
71 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
73 </bean>
75 <bean id="updateAssignCache" class="net.nelz.simplesm.aop.UpdateAssignCacheAdvice">
77 <property name="cache" ref="memcachedClient" />
79 <property name="methodStore" ref="methodStore" />
81 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
83 </bean>
85 <bean id="invalidateSingleCache" class="net.nelz.simplesm.aop.InvalidateSingleCacheAdvice">
87 <property name="cache" ref="memcachedClient" />
89 <property name="methodStore" ref="methodStore" />
91 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
93 </bean>
95 <bean id="invalidateMultiCache" class="net.nelz.simplesm.aop.InvalidateMultiCacheAdvice">
97 <property name="cache" ref="memcachedClient" />
99 <property name="methodStore" ref="methodStore" />
101 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
103 </bean>
105 <bean id="invalidateAssignCache" class="net.nelz.simplesm.aop.InvalidateAssignCacheAdvice">
107 <property name="cache" ref="memcachedClient" />
109 <property name="methodStore" ref="methodStore" />
111 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
113 </bean>
Simple-Spring-Memcached还提供了一个例子,在spring的petClinic例子中加入了几行代码,就实现了对MemCached的调用:
1 import net.nelz.simplesm.annotations.ReadThroughAssignCache;
2
3 import net.nelz.simplesm.annotations.ReadThroughSingleCache;
4
5 @ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")
6
7 public Collection<Vet> getVets() {
8
9 System.out.println("\n ! ! !Gonna wait a bit: " + new Date() + "\n");
10
11 try {
12
13 Thread.sleep(4000);
14
15 } catch (Exception ex) {}
16
17 return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
18
19 }
为了加强测试的效果,在第一次读取数据时,故意停顿了一下(sleep)。
转载自:http://www.blogjava.net/hao446tian/archive/2012/04/10/373725.html
转载于:https://my.oschina.net/caryliu/blog/264515