使用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. 循环取到json中的字段数据,加到html中

    $.ajax({ type:'post', data:{specialName:specialName,count:count}, url:"admin/pcAdminGetArticleL ...

  2. 在Virtualenv中使用Idle

    原创 在Virtualenv中使用Idle 本文适用于Linux系统,在DebianTesting上测试通过. 关于Virtualenv 先看一段Virtualenv这货的官方介绍: virtuale ...

  3. servlet 验证码生成

    servlet package com.htpo.net; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D; ...

  4. DOS批量拷贝本地目录到远程主机(定时执行)

     echo !USER!  net use \\!REMOTE_ADDR!\IPC$ /u:!USER!  copy D:\batch\ip.bat \\!REMOTE_ADDR!\Admin$\ / ...

  5. MVC模式浅谈

    MVC模式浅谈 一.MVC模式概述 模型-视图-控制器(MVC模式)是一种非常经典的软件架构模式,在UI框架和UI设计思路中扮演着非常重要的角色.从设计模式的角度来看,MVC模式是 一种复合模式,它将 ...

  6. python列表中,多次追加元素

    在列表中追加元素,可以使用append(),列表相加也可以用extend()函数,多次追加元素可以用“+”实现 l=[1,2,3,4,5] x=6 y=7 z=8 l=l+[x]+[y]+[z] pr ...

  7. CSS 规范 命名规则

    http://nec.netease.com/standard/css-practice.html

  8. echart生成饼状图

    //绘制图表. echarts.init(document.getElementById('main')).setOption({ tooltip : { trigger: 'item', //触发类 ...

  9. linux 内核启动流程

    Linux内核启动流程详细分析: http://www.linuxidc.com/Linux/2014-10/108034.htm ARM Linux内核启动过程: http://blog.csdn. ...

  10. Python 第一个程序_1

    第一个Python程序(P108) 目标 第一个HelloPython 程序 Python2.x与3.x版本简介 执行Python程序的三种方式 解释器--python/python3  (终端中.使 ...