Git Product home page Git Product logo

aspect-cache-plug's Introduction

aspect-cache-plug

先介绍缓存使用下场景
对同一请求(针对不同请求参数)进行缓存,可以减少服务器压力、加快响应速度

查询接口:根据请求条件对返回结果进行缓存
更新接口:根据条件对指定缓存进行清除

@Aspect(key = "query")
public Object query(String q) {
  // do something
  return null;
}
@Aspect(key = "update")
public Object update(String q) {
  // do something  
  return null;
}

缓存query方法返回值

<bean class="com.aspect.config.CacheConfig">
	<property name="key" value="query"/>
	<property name="space" value="space2"/>
	<property name="keyExpression">
		<value>this[0]+new java.util.Date()</value>
	</property>					
	<property name="timeout" value="60"/>
</bean>

执行update时清除query的返回结果

<bean class="com.aspect.config.ClearConfig">
    <property name="key" value="update"/>    
    <property name="cachedConfigs">
	    <list>
	        <value>query</value>
	    </list>
	</property>
</bean>

加入spring支持

<aop:config> 
	    <aop:pointcut id="adviceAspectPoint" expression="execution(* com.sample..*.*(..)) and @annotation(Aspect)" /> 
		<aop:aspect ref="adviceAspect"> 
			<aop:around method="execute" pointcut-ref="adviceAspectPoint"  arg-names="Aspect" />
		</aop:aspect> 
	</aop:config>
    
    <bean id="adviceAspect" class="com.aspect.AdviceAspect">
		<property name="rootKey" value="aspect.cache."/>
		<property name="cache" ref="cache"/>
		<property name="caches">
		    <list>
		        <bean class="com.aspect.config.CacheConfig">
		            <property name="key" value="queryMethod1"/>
		            <property name="space" value="space1"/>
					<property name="keyExpression">
						<value>this[1]+"."+this[2]</value>
					</property>
					<property name="domainExpression">
						<value>this[0]</value>
					</property>
					<property name="timeout" value="60"/>
		        </bean>
		        <bean class="com.aspect.config.CacheConfig">
		            <property name="key" value="queryMethod2"/>
		            <property name="space" value="space1"/>
					<property name="keyExpression">
						<value>this[0]</value>
					</property>					
					<property name="timeout" value="60"/>
		        </bean>
		        <bean class="com.aspect.config.CacheConfig">
		            <property name="key" value="space2QueryMethod1"/>
		            <property name="space" value="space2"/>
					<property name="keyExpression">
						<value>this[0]+new java.util.Date()</value>
					</property>					
					<property name="timeout" value="60"/>
		        </bean>
		    </list>
		</property>
		<property name="clears">
		    <list>
		        <bean class="com.aspect.config.ClearConfig">
		            <property name="key" value="updateMethod1"/>
		            <property name="domainExpression">
						<value>this[0]</value>
					</property>
					<property name="cachedConfigs">
					    <list>
					        <value>queryMethod1</value>
					        <value>space2QueryMethod1</value>
					    </list>
					</property>
		        </bean>
		    </list>
		</property>
    </bean>

aspect-cache-plug's People

Contributors

yuxiangping avatar

Watchers

gspripple avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.