使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
使用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的缓存的更多相关文章
- (转)Spring使用AspectJ进行AOP的开发:注解方式
http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...
- AspectJ对AOP的实现
一:你应该明白的知识 1.对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且 ...
- Spring框架(6)---AspectJ实现AOP
AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...
- 开涛spring3(6.4) - AOP 之 6.4 基于@AspectJ的AOP
Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 6.4.1 启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明, ...
- Spring框架学习09——基于AspectJ的AOP开发
1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...
- Spring -- aop, 用Aspectj进行AOP开发
1. 概要 添加类库:aspectjrt.jar和aspectjweaver.jar 添加aop schema. 定义xml元素:<aop:aspectj-autoproxy> 编写jav ...
- Spring整合AspectJ的AOP
学而时习之,不亦说乎! --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
随机推荐
- [Linux]Linux下动态安装PHP扩展的一般方法(图)
---------------------------------------------------------------------------------------------------- ...
- windows下配置mysql环境变量 - 使用cmd访问mysql(图)
window7为例,右击“计算机” - 单击“属性” - 单击“高级系统设置” - 单击“环境变量”,剩下看图: <图1> 右下角"环境变量". <图2>选 ...
- SQL Server 中BIT类型字段增删查改那点事
话说BIT类型字段之前,先看“诡异”的一幕,执行Update成功,但是查询出来的结果依然是1,而不是Update的2 当别人问起我来的时候,本人当时也是处于懵逼状态的,后面联想具体的业务突然想起来这个 ...
- Hibernate 再接触 多对多单向双向关联
情景:一个老师可能有多个学生,一个学生也可能有多个老师 多对一单向: 例如老师知道自己教哪些学生,学生却不知道自己被哪些老师教 方法:使用第三张表 分别存两张表的id annotation Stude ...
- swift中UIImageView的创建
let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...
- K-means聚类算法(转)
K-means聚类算法 想想常见的分类算法有决策树.Logistic回归.SVM.贝叶斯等.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是 ...
- kotlin集合操作
1.1 总数操作 方法作用: any--判断集合中是否有满足条件 的元素: all--判断集合中的元素是否都满足条件: count--查询集合中满足条件的元素个数: fold--在给定初始值的基础上, ...
- 简单的实现微信获取openid
微信公众平台获取openid在公众号的开发中有很多用途,前段时间为实现用户使用公众号在登录一次以后可以免密登陆而使用了openid.开发过程中遇到了一些问题,在这里向需要且还没有获取到openid的米 ...
- whatweb工具
WhatWeb WhatWeb可以用来确定服务器使用的CMS.博客平台.统计分析软件包.JavaScript库等.这个工具有超过900个插件用来扫描目标. 使用方法: root@root:/pente ...
- video 播放本地视屏
var file = document.getElementById('file_video').files[0]; var url = URL.createObjectURL(file); docu ...