使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存

今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCached的客户端开发变得超乎寻常的简单,只要一行代码就行:

@ReadThroughAssignCache(assignedKey = "VETS", expiration = , namespace = "NELZ")

是不是很神奇?这行代码指定了MemCached的key,过期时间和命名空间。假设你的MemCached服务器IP是:196.168.10.101,端口是:,那么在数据调用的配置文件中只要加上下面配置代码就可以了:

  <import resource="classpath:simplesm-context.xml" />
<bean id="memcachedConnectionBean" class="net.nelz.simplesm.config.MemcachedConnectionBean">
<property name="consistentHashing" value="true" />
<property name="nodeList" value="196.168.10.101:12000" />
</bean> 从simplesm-context.xml的内容中,可以看出它所封装的类和方法: <bean id="memcachedClientFactory" class="net.nelz.simplesm.config.MemcachedClientFactory" >
property name="bean" ref="memcachedConnectionBean" />
</bean>
<bean id="memcachedClient" factory-bean="memcachedClientFactory" factory-method="createMemcachedClient" />
<bean id="methodStore" class="net.nelz.simplesm.aop.CacheKeyMethodStoreImpl" />
<bean id="net.nelz.simplesm.DefaultKeyProvider" class="net.nelz.simplesm.impl.DefaultKeyProvider">
<property name="methodStore" ref="methodStore" />
</bean>
<bean id="readThroughSingleCache" class="net.nelz.simplesm.aop.ReadThroughSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="readThroughMultiCache" class="net.nelz.simplesm.aop.ReadThroughMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="readThroughAssignCache" class="net.nelz.simplesm.aop.ReadThroughAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean> <bean id="updateSingleCache" class="net.nelz.simplesm.aop.UpdateSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean> <bean id="updateMultiCache" class="net.nelz.simplesm.aop.UpdateMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="updateAssignCache" class="net.nelz.simplesm.aop.UpdateAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="invalidateSingleCache" class="net.nelz.simplesm.aop.InvalidateSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="invalidateMultiCache" class="net.nelz.simplesm.aop.InvalidateMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean> <bean id="invalidateAssignCache" class="net.nelz.simplesm.aop.InvalidateAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean> Simple-Spring-Memcached还提供了一个例子,在spring的petClinic例子中加入了几行代码,就实现了对MemCached的调用: import net.nelz.simplesm.annotations.ReadThroughAssignCache; import net.nelz.simplesm.annotations.ReadThroughSingleCache; @ReadThroughAssignCache(assignedKey = "VETS", expiration = , namespace = "NELZ") public Collection<Vet> getVets() { System.out.println("\n ! ! !Gonna wait a bit: " + new Date() + "\n"); try { Thread.sleep(); } catch (Exception ex) {} return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list(); } 为了加强测试的效果,在第一次读取数据时,故意停顿了一下(sleep)。
转载自:http://www.blogjava.net/hao446tian/archive/2012/04/10/373725.html

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存的更多相关文章

  1. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  2. AspectJ对AOP的实现

    一:你应该明白的知识 1.对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且 ...

  3. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

  4. 开涛spring3(6.4) - AOP 之 6.4 基于@AspectJ的AOP

    Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 6.4.1  启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明, ...

  5. Spring框架学习09——基于AspectJ的AOP开发

    1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...

  6. Spring -- aop, 用Aspectj进行AOP开发

    1. 概要 添加类库:aspectjrt.jar和aspectjweaver.jar 添加aop schema. 定义xml元素:<aop:aspectj-autoproxy> 编写jav ...

  7. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  8. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...

  9. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

随机推荐

  1. split()方法解析

    split()方法用于将字符串分割为字符串数组. 废话不多说,直接贴代码: var str="How are you doing today?" console.log(str.s ...

  2. Android事件拦截机制 - 两句话

    模拟情形:ViewGroupA ->ViewGroupB->View False往下走,True就停下.(适用于事件传递和事件处理)

  3. work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章

    <?phpuse Workerman\Worker;use Workerman\Lib\Timer; require_once '../../web/Workerman/Autoloader.p ...

  4. Recycleview 横竖屏

    看到了一篇贴子:https://blog.csdn.net/yaosongqwe/article/details/48710375 //竖屏线性展示 mLlayoutmanager = new Lin ...

  5. Physical (Raw) Versus Logical Backups

    [Physical (Raw) Versus Logical Backups] Physical backups consist of raw copies of the directories an ...

  6. Activty左出右进动画

    [Activty左出右进动画] public void overridePendingTransition (int enterAnim, int exitAnim) 其中: enterAnim 定义 ...

  7. Photoshop Keynote

    [Photoshop Keynote] 1.Tab:隐藏.显示所有面板. 2.Sihft+Tab:隐藏.显示右侧面板. 3.F:全屏切换. 4.选择并遮住: 参考:http://www.51shipi ...

  8. ncat的使用

    由于netcat的缺陷,所以有了升级版ncat,弥补了netcat的一些不足. ncat是nmap工具包的一个工具. 服务器端 ncat -c bash --allow 192.168.1.119 - ...

  9. day12 装饰器的模版

    1.什么是装饰器 装饰器指的是为被装饰对象(别人)添加新功能的工具 装饰器本身可以是任意可调用对象 被装饰器对象也可以是任意可调用对象 2.为何要用装饰器 开放封闭原则:指的是对修改封闭,对扩展开放 ...

  10. python内置函数getattr用法

    class Tests(object):    #定义类     aaa = '10'          #定义变量       def test(self):     #定义类的方法test     ...