使用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 语言的 ...
随机推荐
- adb INSTALL_FAILED_UPDATE_INCOMPATIBLE
今天用Eclipse运行项目时出错: LOG: [2018-05-09 14:16:19 - Module_Android_Demo] ------------------------------ [ ...
- JAVAWEB 一一 Sturts2+ibatis(框架,Sturts2,用action代替servlet)
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- Java的学习04
今天依旧记录一下,学习的东西. import java.io.File; import java.io.IOException; import java.util.Date; /** * 测试File ...
- JDBC连接各种数据库的方法,连接MySql,Oracle数据库
JDBC连接各种数据库的方法: JDBC编程步骤: 1.导入jar包 2.注册驱动 3.获取数据库连接对象 4.定义SQL语句 5.获得执行SQL语句对象statemnet 6.执行SQL语句 7.处 ...
- 从源码安装Node
[从源码安装Node] Nodejs官网未并提供i686架构的bin,为了在i686架构cpu下使用Nodejs,需要从源码编译. 1../configure 2.make 3.make instal ...
- cdh 安装系列1-- manager 6.01 安装
一.如果是远程centos安装,请参考我得博客,通过xmanager 链接centos桌面 二.Non-production Installation Ideal for trying Clouder ...
- python--第二天总结
一.作用域只要变量在内存中存在,则就可以使用.(栈) 二.三元运算result = 值result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result ...
- MongoDB修改默认数据库
在某些情况下,我们并不想把mongoDB的数据库放在c盘,这时候有两种方法可以切换数据库目录. 1.命令方式 首先创建数据路目录,例如:E:\data\db.然后运行命令 mongod -dbpath ...
- React学习札记一
I’m in a hurry! 我在赶时间! It’s her field. 这是她的本行. It’s up to you. 由你决定. You owe me one.你欠我一个人情. 1.React ...
- windows上安装Anaconda和python
下载并安装 anaconda 先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版 ...