锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
姣斿錛屽湪涓氬姟鎿嶄綔涓殑緋葷粺鏃ュ織鍜屾潈闄愭棩蹇楋紝灝卞緢濂界殑浣撶幇浜嗕簨鍔″師瀛愭у拰浜嬪姟闅旂鐨勫簲鐢ㄣ?/span>
榪欓噷錛屼富瑕佽涓涓嬩簨鍔¢殧紱?/span>
EJB
鐨勪嬌鐢ㄣ?/span>
聽涓轟簡鑳藉湪鍏鋒湁浜嬪姟鐨?/span>
action
涓皟鐢ㄤ笉鍏佽浜嬪姟鏀寔鐨勫閮ㄧ郴緇?/span>
EJB
錛屽
UMSAO
錛屽鍔犱簡浜嬪姟闅旂
EJB
銆?br />
闇瑕佸湪鐩稿叧鏂囦歡涓厤緗細(xì)
biz-context.xml
"ejb/test/Facade4IsolateService"鍦╳eblogic-ejb-jar.xml涓厤緗紝蹇呴』鍚屽悕銆?br />
weblogic-ejb-jar.xml
Facade4IsolateServiceEJB鍦╡jb-jar.xml鏂囦歡涓緗?br />
ejb-jar.xml
1銆佺啛緇僡nt榪涜java宸ョ▼緙栬瘧錛堝弬鑰冩湰浜衡渁nt緙栬瘧java宸ョ▼鈥濅竴鏂囷級
2銆乯unit鐭ヨ瘑錛岀湅鐪媤ww.junit.org
鍑嗗錛?/p>
1銆亀ww.junit.org涓嬭澆junit.jar
2銆亁ml.apache.org涓嬭澆xalan.jar錛坅nt 鐨?juntreport闇瑕佺敤鍒幫紝xalan鏈濂界増鏈ぇ浜?.0錛屼笉鐒朵綘榪樿涓嬭澆鍏朵粬鐨刯ava搴擄級
鎶妀unit.jar銆亁alan.jar鎷瘋礉鍒?ANT_HOME/lib/涓嬮潰錛屽茍涓旀渶濂藉姞鍒扮郴緇烠LASSPATH錛坖unit.jar鏄偗瀹氳鐨勶紝xalan.jar灝變笉娓呮浜嗭級銆?br />
瀵硅嚜宸辯殑紼嬪簭娣誨姞junit嫻嬭瘯紼嬪簭錛屾澶勮鍙傝僿ww.junit.org鐨勬枃妗o紝寰堢畝鍗曠殑銆?br />
鐒跺悗鍦╞uild.xml涓坊鍔犲涓嬫寚浠?寰堝涓滆タ鐣ヨ繃錛屾墍浠ュ己鐑堣姹傝鑰呭厛瀹屾垚鏈漢鈥渁nt緙栬瘧java宸ョ▼鈥濅竴鏂囷級
<!-- 鍗曞厓嫻嬭瘯錛岄渶瑕佸畬鎴恈ompile浠誨姟 -->
<target name="test" depends="compile">
<junit printsummary="yes">
<!-- 闇瑕佺殑classpath -->
<classpath refid="classpath"/>
<batchtest>
<!-- 鍗曞厓嫻嬭瘯鏂囦歡涓烘墍鏈塻rc鐩綍涓嬬殑*Test.java鏂囦歡 -->
<fileset dir="${srcDir}"><include name="**/*Test.java"/></fileset>
<!-- 鐢熸垚鏍煎紡涓簒ml錛屼篃鍙互鐢╬lain鎴栬卋rief -->
<!-- 涓轟粈涔堢敓鎴恱ml錛屾槸涓轟簡涓嬩竴姝ュ仛report鐢?-->
<formatter type="xml"/>
</batchtest>
</junit>
<!-- 瀵箈ml鏂囦歡鐢熸垚鐩稿簲鐨刪tml鏂囦歡鍦╮eports鐩綍涓?-->
<!-- 濡傛灉鎸囧畾浜巜eb鍙闂殑鐩綍錛屽氨鍙互浣挎暣涓」鐩粍鐪嬪埌鍗曞厓嫻嬭瘯鎯呭喌 -->
<junitreport todir="reports">
<fileset dir=".">
<include name="TEST-*.xml"/>
</fileset>
<!-- 甯︽湁妗嗘灦錛屽彲浠ョ敤noframes閫変笉甯︽鏋?-->
<report format="frames" todir="reports/html"/>
</junitreport>
</target>
鐜板湪榪愯ant test錛岀湅鐪媟eports/涓嬮潰鏄笉鏄湁浜嗙敓鎴愮殑鍗曞厓嫻嬭瘯緇撴灉鐨勬枃浠朵簡錛?/p>
濡傛灉鏈夐棶棰樺彲浠ョ敤ant -debug test鐪嬬湅闂鍑哄湪鍝噷錛?/p>
鍙﹀鏇存敼婧愮▼搴忥紝璁╃▼搴忛氳繃嫻嬭瘯鍜岄氫笉榪囨祴璇曠湅鐪嬬敓鎴愮殑緇撴灉濡備綍錛?/p>
浠g爜: |
public class SomeBusinessClass extends OtherBusinessClass { 聽 聽 // 鏍稿績鏁版嵁鎴愬憳 聽 聽 // 鍏跺畠鏁版嵁鎴愬憳錛氭棩蹇楁祦錛屼繚璇佹暟鎹畬鏁存х殑鏍囧織浣嶇瓑 聽 聽 // 閲嶈澆鍩虹被鐨勬柟娉? 聽 聽 public void performSomeOperation(OperationInformation info) { 聽 聽 聽 聽 // 瀹夊叏鎬ч獙璇? 聽 聽 聽 聽 // 媯鏌ヤ紶鍏ユ暟鎹槸鍚︽弧瓚沖崗璁? 聽 聽 聽 聽 // 閿佸畾瀵硅薄浠ヤ繚璇佸綋鍏朵粬綰跨▼璁塊棶鏃剁殑鏁版嵁瀹屾暣鎬? 聽 聽 聽 聽 // 媯鏌ョ紦瀛樹腑鏄惁涓烘渶鏂頒俊鎭? 聽 聽 聽 聽 // 綰綍鎿嶄綔寮濮嬫墽琛屾椂闂? 聽 聽 聽 聽 // 鎵ц鏍稿績鎿嶄綔 聽 聽 聽 聽 // 綰綍鎿嶄綔瀹屾垚鏃墮棿 聽 聽 聽 聽 // 緇欏璞¤В閿? 聽 聽 } 聽 聽 // 涓浜涚被浼兼搷浣? 聽 聽 public void save(PersitanceStorage ps) { 聽 聽 } 聽 聽 public void load(PersitanceStorage ps) { 聽 聽 } } |
浠g爜: |
public class CreditCardProcessor { 聽 聽 public void debit(CreditCard card, Currency amount) 聽 聽 聽 聽throws InvalidCardException, NotEnoughAmountException, 聽 聽 聽 聽 聽 聽 聽 CardExpiredException { 聽 聽 聽 聽 // 鍙栧嚭閫昏緫 聽 聽 } 聽 聽 聽 聽 public void credit(CreditCard card, Currency amount) 聽 聽 聽 聽 throws InvalidCardException { 聽 聽 聽 聽 // 瀛樺叆閫昏緫 聽 聽 } } |
浠g爜: |
public interface Logger { 聽 聽 public void log(String message); } |
浠g爜: |
public class CreditCardProcessorWithLogging { 聽 聽 Logger _logger; 聽 聽 public void debit(CreditCard card, Money amount) 聽 聽 聽 聽 throws InvalidCardException, NotEnoughAmountException, 聽 聽 聽 聽 聽 聽 聽 聽CardExpiredException { 聽 聽 聽 聽 _logger.log("Starting CreditCardProcessor.credit(CreditCard, Money) " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 + "Card: " + card + " Amount: " + amount); 聽 聽 聽 聽 // 鍙栧嚭閫昏緫 聽 聽 聽 聽 _logger.log("Completing CreditCardProcessor.credit(CreditCard, Money) " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 + "Card: " + card + " Amount: " + amount); 聽 聽 } 聽 聽 聽 聽 public void credit(CreditCard card, Money amount) 聽 聽 聽 聽 throws InvalidCardException { 聽 聽 聽 聽 System.out.println("Debiting"); 聽 聽 聽 聽 _logger.log("Starting CreditCardProcessor.debit(CreditCard, Money) " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 + "Card: " + card + " Amount: " + amount); 聽 聽 聽 聽 // 瀛樺叆閫昏緫 聽 聽 聽 聽 _logger.log("Completing CreditCardProcessor.credit(CreditCard, Money) " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 + "Card: " + card + " Amount: " + amount); 聽 聽 } } |
浠g爜: |
public aspect LogCreditCardProcessorOperations { 聽 聽 Logger logger = new StdoutLogger(); 聽 聽 pointcut publicOperation(): 聽 聽 聽 聽 execution(public * CreditCardProcessor.*(..)); 聽 聽 pointcut publicOperationCardAmountArgs(CreditCard card, 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽Money amount): 聽 聽 聽 聽 publicOperation() && args(card, amount); 聽 聽 before(CreditCard card, Money amount): 聽 聽 聽 聽 publicOperationCardAmountArgs(card, amount) { 聽 聽 聽 聽 logOperation("Starting", 聽 聽 聽 聽 聽 聽 聽thisjoin point.getSignature().toString(), card, amount); 聽 聽 } 聽 聽 after(CreditCard card, Money amount) returning: 聽 聽 聽 聽 publicOperationCardAmountArgs(card, amount) { 聽 聽 聽 聽 logOperation("Completing", 聽 聽 聽 聽 聽 聽 thisjoin point.getSignature().toString(), card, amount); 聽 聽 } 聽 聽 after (CreditCard card, Money amount) throwing (Exception e): 聽 聽 聽 聽 publicOperationCardAmountArgs(card, amount) { 聽 聽 聽 聽 logOperation("Exception " + e, 聽 聽 聽 聽 聽 聽 thisjoin point.getSignature().toString(), card, amount); 聽 聽 } 聽 聽 private void logOperation(String status, String operation, 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 CreditCard card, Money amount) { 聽 聽 聽 聽 logger.log(status + " " + operation + 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽" Card: " + card + " Amount: " + amount); 聽 聽 } } |
浠g爜: |
package com.company.springaop.test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class TestBeforeAdvice implements MethodBeforeAdvice { 聽 public void before(Method m, Object[] args, Object target) 聽 throws Throwable { 聽 聽 System.out.println("Hello world! (by " 聽 聽 聽 聽 + this.getClass().getName() 聽 聽 聽 聽 + ")"); 聽 } } 聽 |
浠g爜: |
package com.company.springaop.test; public class BeanImpl implements Bean { 聽 public void theMethod() { 聽 聽 System.out.println(this.getClass().getName() 聽 聽 聽 聽 + "." + new Exception().getStackTrace()[0].getMethodName() 聽 聽 聽 聽 + "()" 聽 聽 聽 聽 + " says HELLO!"); 聽 } } |
浠g爜: |
package com.company.springaop.test; public interface Bean { 聽 public void theMethod(); } |
浠g爜: |
package com.company.springaop.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Main { 聽 public static void main(String[] args) { 聽 聽 //Read the configuration file 聽 聽 ApplicationContext ctx 聽 聽 聽 聽 = new FileSystemXmlApplicationContext("springconfig.xml"); 聽 聽 //Instantiate an object 聽 聽 Bean x = (Bean) ctx.getBean("bean"); 聽 聽 //Execute the public method of the bean (the test) 聽 聽 x.theMethod(); 聽 } } |
浠g爜: |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC聽 "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> 聽 <!--CONFIG--> 聽 <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean"> 聽 聽 <property name="proxyInterfaces"> 聽 聽 聽 <value>com.company.springaop.test.Bean</value> 聽 聽 </property> 聽 聽 <property name="target"> 聽 聽 聽 <ref local="beanTarget"/> 聽 聽 </property> 聽 聽 <property name="interceptorNames"> 聽 聽 聽 <list> 聽 聽 聽 聽 <value>theAdvisor</value> 聽 聽 聽 </list> 聽 聽 </property> 聽 </bean> 聽 <!--CLASS--> 聽 <bean id="beanTarget" class="com.company.springaop.test.BeanImpl"/> 聽 <!--ADVISOR--> 聽 <!--Note: An advisor assembles pointcut and advice--> 聽 <bean id="theAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 聽 聽 <property name="advice"> 聽 聽 聽 <ref local="theBeforeAdvice"/> 聽 聽 </property> 聽 聽 <property name="pattern"> 聽 聽 聽 <value>com\.company\.springaop\.test\.Bean\.theMethod</value> 聽 聽 </property> 聽 </bean> 聽 <!--ADVICE--> 聽 <bean id="theBeforeAdvice" class="com.company.springaop.test.TestBeforeAdvice"/> </beans> 聽 |
by:http://opensource.atlassian.com/confluence/spring/display/DISC/AOP+Cache
I've written an AOP interceptor which allows you to specify which methods to cache for Spring beans.
Different cache providers are available: Memory HashMap, EHCache, OSCache (which is clusterable) and SwarmCache.
We start by defining a bookManager object which has one method
聽
which returns a List of Books that are related to the Book that is specified. This method is ideally suited for caching, since the related books will not change that often.<bean聽id="bookManager"聽class="com.example.BookManager"/>
Next we define the AOP interceptor which will cache the results. This example will return a cached result (the List of Books) instead of a call to bookManager.getRelated(Book), if the method is called with the same Book argument.
Available implementations of CacheInterceptor are:
The cache in the example is expired after 15 minutes.聽聽
聽聽聽聽聽
聽聽聽聽聽聽
refreshPeriods and defaultRefreshPeriod are properties that are specific for the OSCacheInterceptor. The best way to know how to configure a specific CacheInterceptor implementation is by having a look in the Javadoc (see the project.zip file).
refreshPeriods indicates how long the results will be cached in seconds. So a call to com.example.BookManager.getRelated() will be cached for 900 seconds (15 minutes).
When a method is intercepted that is not defined under refreshPeriods, the value of defaultRefreshPeriod will be used.
In order to be able to identify a call to the method with the same parameter, we use the identifiers property. Here you can list the function that needs to be called in order to get a unique identifier for this class. For each class that is used as an argument for the cached methods, specify the method name (which may not have any parameters). This is a better aproach than using the toString() method, since this method can produce long lines while most of the time a simple identifier is available. So in this example, book.getId() will be used to identify separate Book arguments. There is no need to specify arguments that are primitives (float, int), Strings or Numbers (Float, Integer, ...).
Now we wire the cacheInterceptor to the bookManager bean and we're done! Calls to bookManager.getRelated() will from now on be cached for 15 minutes.
Of course you can add as many beans to the cache as you want.
聽
The first version was written quite some time ago, but since 2006/03/21 there is a new version
聽(2.0.18).
I've used this code already in three projects, but not all implementations of the cache (SwarmCache) are well tested. The code is written in the org.springframework.aop.interceptor.cache package and may be copied/used for free.
This code is written by Pieter Coucke聽for Onthoo.com
, but may be used for free. Of course, if the Spring core developers are interested in integrating this, they are free to do so. If you find this code useful or have any remarks, please let me know!
See this XML file聽for a sample configuration.
Eclipse project and source code. More info is available in the javadocs
.
Caching the result of methods using Spring and EHCache
My AOP cache blog
聽and a post about version聽2.0.18.
Attribute based caching
浠庤В鍘嬬緝鐩綍鍙栧緱oscache.jar聽 鏂囦歡鏀懼埌聽 /WEB-INF/lib聽 鎴栫浉搴旂被搴撶洰褰暵?鐩綍涓紝
jar鏂囦歡鍚嶅彲鑳藉惈鏈夌増鏈彿鍜岃鐗堟湰鐨勫彂甯冩棩鏈熶俊鎭瓑錛屽oscache-2.0.2-22Jan04.jar
濡傛灉浣犵殑jdk鐗堟湰涓?.3.x,寤鴻鍦╨ib涓姞鍏pache聽 Common聽 Lib聽 鐨刢ommons-collections.jar鍖呫?br />濡俲dk鏄?.4浠ヤ笂鍒欎笉蹇?/p>
浠巗rc鎴杄tc鐩綍鍙栧緱oscache.properties聽 鏂囦歡錛屾斁鍏rc鏍圭洰褰曟垨鍙戝竷鐜鐨?WEB-INF/classes聽 鐩綍
濡備綘闇瑕佸緩绔嬬鐩樼紦瀛橈紝欏諱慨鏀筼scache.properties聽 涓殑cache.path淇℃伅聽 (鍘繪帀鍓嶉潰鐨?娉ㄩ噴)銆?br />win綾昏礬寰勭被浼間負(fù)c:\app\cache
unix綾昏礬寰勭被浼間負(fù)/opt/myapp/cache
鎷瘋礉OSCache鏍囩搴撴枃浠秓scache.tld鍒?WEB-INF/classes鐩綍銆?/p>
鐜板湪浣犵殑搴旂敤鐩綍綾諱技濡備笅錛?br />$WEB_APPLICATIONWEB-INFliboscache.jar
$WEB_APPLICATIONWEB-INFclassesoscache.properties
$WEB_APPLICATIONWEB-INFclassesoscache.tld
灝嗕笅鍒椾唬鐮佸姞鍏eb.xml鏂囦歡涓?br />紼嬪簭浠g爜錛?br /><taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>
涓轟簡渚夸簬璋冭瘯鏃ュ織杈撳嚭錛岄』鍔犲叆commons-logging.jar鍜宭og4j-1.2.8.jar鍒板綋鍓嶇被搴撹礬寰勪腑
鍦╯rc鐩綍鍔犲叆涓嬮潰涓や釜鏃ュ織杈撳嚭閰嶇疆鏂囦歡錛?/p>
log4j.properties聽 鏂囦歡鍐呭涓猴細(xì)
紼嬪簭浠g爜錛?br />log4j.rootLogger=DEBUG,stdout,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=oscache.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
log4j.logger.org.apache.commons=ERROR
log4j.logger.com.opensymphony.oscache.base=INFO
commons-logging.properties 鏂囦歡鍐呭涓?br />org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog
2.oscache.properties聽 鏂囦歡閰嶇疆鍚戝聽
cache.memory
鍊間負(fù)true聽 鎴柭?false聽 錛岄粯璁や負(fù)鍦ㄥ唴瀛樹腑浣滅紦瀛橈紝
濡傝緗負(fù)false錛岄偅cache鍙兘緙撳瓨鍒版暟鎹簱鎴栫‖鐩樹腑錛岄偅cache榪樻湁浠涔堟剰涔夛細(xì)錛?/p>
cache.capacity
緙撳瓨鍏冪礌涓暟
cache.persistence.class
鎸佷箙鍖栫紦瀛樼被錛屽姝ょ被鎵撳紑錛屽垯蹇呴』璁劇疆cache.path淇℃伅
cache.cluster聽 鐩稿叧
涓洪泦緹よ緗俊鎭?br />濡?br />cache.cluster.multicast.ip涓哄箍鎾璉P鍦板潃
cache.cluster.properties涓洪泦緹ゅ睘鎬?/p>
3.OSCache鐨勫熀鏈敤娉?/p>
cache1.jsp聽 鍐呭濡備笅
紼嬪簭浠g爜錛?br /><%@ page import="java.util.*" %>
<%@ taglib uri="oscache" prefix="cache" %>
<html>
<body>
娌℃湁緙撳瓨鐨勬棩鏈? <%= new Date() %><p>
<!--鑷姩鍒鋒柊-->
<cache:cache time="30">
姣?0縐掑埛鏂扮紦瀛樹竴嬈$殑鏃ユ湡: <%= new Date() %>
</cache:cache>
<!--鎵嬪姩鍒鋒柊-->
<cache:cache key="testcache">
鎵嬪姩鍒鋒柊緙撳瓨鐨勬棩鏈? <%= new Date() %> <p>
</cache:cache>
<a href="cache2.jsp">鎵嬪姩鍒鋒柊</a>
</body>
</html>
cache2.jsp聽 鎵ц鎵嬪姩鍒鋒柊欏甸潰濡備笅
<聽 %@聽 taglib聽 uri="oscache"聽 prefix="cache"聽 %聽 >
<html>
<body>
緙撳瓨宸插埛鏂?..<p>
<cache:flush聽 key="testcache"聽 scope="application"/>
<a聽 href="cache1.jsp">榪斿洖</a>
</body>
</html>
浣犱篃鍙互閫氳繃涓嬮潰璇彞瀹氫箟Cache鐨勬湁鏁堣寖鍥?濡備笉瀹氫箟scope,scope榛樿涓篈pplcation
紼嬪簭浠g爜錛?br /><cache:cache time="30" scope="session">
...
</cache:cache>
4.聽 緙撳瓨榪囨護(hù)鍣?CacheFilter聽
浣犲彲浠ュ湪web.xml涓畾涔夌紦瀛樿繃婊ゅ櫒錛屽畾涔夌壒瀹氳祫婧愮殑緙撳瓨銆?br />紼嬪簭浠g爜錛?br /><filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>60</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
涓婇潰瀹氫箟灝嗙紦瀛樻墍鏈?jsp欏甸潰錛岀紦瀛樺埛鏂版椂闂翠負(fù)60縐掞紝緙撳瓨浣滅敤鍩熶負(fù)Session
娉ㄦ剰錛孋acheFilter鍙崟鑾稨ttp澶翠負(fù)200鐨勯〉闈㈣姹傦紝鍗沖彧瀵規(guī)棤閿欒璇鋒眰浣滅紦瀛橈紝
鑰屼笉瀵瑰叾浠栬姹傦紙濡?00,404,400錛変綔緙撳瓨澶勭悊
OSCache綆浠?
OSCache鏄疧penSymphony緇勭粐鎻愪緵鐨勪竴涓狫2EE鏋舵瀯涓璚eb搴旂敤灞傜殑緙撳瓨鎶鏈疄鐜扮粍浠訛紝瀹冪殑鍑虹幇瑙e喅浜嗘垜浠潰涓寸殑闂銆?OSCache鐩墠鏈鏂扮殑紼沖畾鐗堟湰鏄?.0錛屾湰鏂囦腑鐨勪緥瀛愰兘鏄熀浜庤繖涓増鏈殑錛屽鏋滃ぇ瀹惰繍琛屼緥瀛愮殑榪囩▼涓彂鐢熼棶棰橈紝璇烽鍏堢‘璁ゆ槸鍚﹂噰鐢ㄤ簡姝g‘鐨勮蔣浠剁増鏈?
2.1 涓昏鐗瑰緛
1. 鍏煎澶氱鏀寔JSP鐨剋eb鏈嶅姟鍣?
宸茬粡閫氳繃鍏煎嫻嬭瘯鐨剋eb鏈嶅姟鍣ㄥ寘鎷琌rionServer (1.4.0鎴栬呬互涓婄増鏈? 銆丮acromedia JRun (3.0鎴栬呬互涓婄増鏈? 銆丅EA Weblogic (7.x鎴栬呬互涓婄増鏈? 銆両BM Websphere (5.0鐗堟湰)銆丼ilverstream (3.7.4鐗堟湰)銆丆aucho Resin (1.2.3鎴栬呬互涓婄増鏈?銆乀omcat (4.0鎴栬呬互涓婄増鏈? 錛屽叾浠栨敮鎸乻ervlet2.3銆乯sp1.2鐨剋eb鏈嶅姟鍣ㄥ簲璇ラ兘鏄畬鍏ㄥ吋瀹?/font>OSCache鐨勩?
2. 鍙夌殑緙撳瓨鍖?
浣犲彲浠ヤ嬌鐢ㄥ唴瀛樸佺‖鐩樼┖闂淬佸悓鏃朵嬌鐢ㄥ唴瀛樺拰紜洏鎴栬呮彁渚涜嚜宸辯殑鍏朵粬璧勬簮錛堥渶瑕佽嚜宸辨彁渚涢傞厤鍣級浣滀負(fù)緙撳瓨鍖恒?
浣跨敤鍐呭瓨浣滀負(fù)緙撳瓨鍖哄皢鍙互鎻愪緵鏇村ソ鐨勬ц兘
浣跨敤紜洏浣滀負(fù)緙撳瓨鍖哄彲浠ュ湪鏈嶅姟鍣ㄩ噸璧峰悗榪呴熸仮澶嶇紦瀛樺唴瀹?
鍚屾椂浣跨敤鍐呭瓨鍜岀‖鐩樹綔涓虹紦瀛樺尯鍒欏彲浠ュ噺灝戝鍐呭瓨鐨勫崰鐢?
3. 鐏墊椿鐨勭紦瀛樼郴緇?
OSCache鏀寔瀵歸儴鍒嗛〉闈㈠唴瀹規(guī)垨鑰呭欏甸潰綰х殑鍝嶅簲鍐呭榪涜緙撳瓨錛岀紪紼嬭呭彲浠ユ牴鎹笉鍚岀殑闇姹傘佷笉鍚岀殑鐜閫夋嫨涓嶅悓鐨勭紦瀛樼駭鍒?
4. 瀹歸敊
鍦ㄤ竴鑸殑web搴旂敤涓紝濡傛灉鏌愪釜欏甸潰闇瑕佸拰鏁版嵁搴撴墦浜ら亾錛岃屽綋瀹㈡埛璇鋒眰鍒拌揪鏃訛紝web搴旂敤鍜屾暟鎹簱涔嬮棿鏃犳硶榪涜浜や簰錛岄偅涔堝皢榪斿洖緇欑敤鎴?緋葷粺鍑洪敊"鎴栬呯被浼肩殑鎻愮ず淇℃伅錛屽鏋滀嬌鐢ㄤ簡OSCache鐨勮瘽錛屼綘鍙互浣跨敤緙撳瓨鎻愪緵緇欑敤鎴鳳紝緇欒嚜宸辮耽寰楃淮鎶ょ郴緇熸垨鑰呴噰鍙栧叾浠栬ˉ鏁戠殑鏃墮棿銆?
鍏跺畠鐗規(guī)ц繕鍖呮嫭瀵歸泦緹ょ殑鏀寔銆佺紦瀛樹富鍔ㄥ埛鏂扮瓑鐗規(guī)э紝澶у鍙互鍙傝僌penSymphony緗戠珯涓婄殑鍏朵粬璧勬簮鑾峰彇鏇村鐨勪俊鎭?
3 OSCache緇勪歡鐨勫畨瑁?
OSCache鏄竴涓熀浜巜eb搴旂敤鐨勭粍浠訛紝浠栫殑瀹夎宸ヤ綔涓昏鏄web搴旂敤榪涜閰嶇疆錛屽ぇ姒傜殑姝ラ濡備笅錛?
1. 涓嬭澆銆佽В鍘嬬緝OSCache
璇峰埌OSCache鐨勪富欏礹ttp://www.opensymphony.com/oscache/download.html涓嬭澆Oscache鐨勬渶鏂扮増鏈紝浣滆呬笅杞界殑鏄?/font>OSCache鐨勬渶鏂扮ǔ瀹氱増鏈?.0銆?
灝嗕笅杞藉悗鐨勩俍ip鏂囦歡瑙e帇緙╁埌c:\oscache錛堝悗闈㈢殑绔犺妭涓皢浣跨敤%OSCache_Home%鏉ヨ〃紺鴻繖涓洰褰曪級鐩綍涓?
2. 鏂板緩绔嬩竴涓獁eb搴旂敤
3. 灝嗕富瑕佺粍浠?OSCache_Home%\oscache.jar鏀懼叆WEB-INF\lib鐩綍
4. commons-logging.jar銆乧ommons-collections.jar鐨勫鐞?
OSCache緇勪歡鐢↗akarta Commons Logging鏉ュ鐞嗘棩蹇椾俊鎭紝鎵浠ラ渶瑕乧ommons-logging.jar鐨勬敮鎸侊紝璇峰皢%OSCache_Home%\lib\core\commons-logging.jar鏀懼叆classpath錛堥氬父鎰忓懗鐫灝嗚繖涓枃浠舵斁鍏EB-INF\lib鐩綍錛?
濡傛灉浣跨敤JDK1.3,璇峰皢%OSCache_Home%\lib\core\commons-collections.jar鏀懼叆classpath錛屽鏋滀嬌鐢↗DK1.4鎴栬呬互涓婄増鏈紝鍒欎笉闇瑕佷簡
5. 灝?/font>oscache.properties銆?/font>oscache.tld鏀懼叆WEB-INF\class鐩綍
%OSCache_Home%\oscache.properties鍖呭惈浜嗗OSCache榪愯鐗瑰緛鍊肩殑璁劇疆淇℃伅
%OSCache_Home%\oscache.tld鍖呭惈浜?/font>OSCache鎻愪緵鐨勬爣絳懼簱鐨勫畾涔夊唴瀹?
6. 淇敼web.xml鏂囦歡
鍦╳eb.xml鏂囦歡涓鍔犱笅闈㈢殑鍐呭錛屽鍔犲OSCache鎻愪緵鐨則aglib鐨勬敮鎸侊細(xì)
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/ oscache.tld</taglib-location>
</taglib>
4 寮濮嬩嬌鐢?/font>OSCache涓殑緙撳瓨緇勪歡
OSCache涓寜鐓х紦瀛樿寖鍥寸殑涓嶅悓鍒嗕負(fù)涓ょ涓嶅悓鐨勬柟寮忥細(xì)涓縐嶆槸緙撳瓨JSP欏甸潰涓儴鍒嗘垨鑰呭叏閮ㄥ唴瀹癸紝涓縐嶆槸鍩轟簬鏁翠釜欏甸潰鏂囦歡鐨勭紦瀛樸?
4.1 JSP閮ㄥ垎鍐呭緙撳瓨
4.1.1 Cache-OSCache鎻愪緵鐨勭紦瀛樻爣絳?
榪欐槸OSCache鎻愪緵鐨勬爣絳懼簱涓渶閲嶈鐨勪竴涓爣絳撅紝鍖呮嫭鍦ㄦ爣絳句腑鐨勫唴瀹瑰皢搴旂敤緙撳瓨鏈哄埗榪涜澶勭悊錛屽鐞嗙殑鏂瑰紡灝嗗彇鍐充簬緙栫▼鑰呭cache鏍囩灞炴х殑璁劇疆銆?
絎竴嬈¤姹傚埌杈炬椂錛屾爣絳句腑鐨勫唴瀹硅澶勭悊騫朵笖緙撳瓨璧鋒潵錛屽綋涓嬩竴涓姹傚埌杈炬椂錛岀紦瀛樼郴緇熶細(xì)媯鏌ヨ繖閮ㄥ垎鍐呭鐨勭紦瀛樻槸鍚﹀凡緇忓け鏁堬紝涓昏鏄互涓嬪嚑欏癸細(xì)
1. 緙撳瓨鏃墮棿瓚呰繃浜哻ache鏍囩璁劇疆鐨則ime鎴栬卍uration灞炴ц瀹氱殑瓚呮椂鏃墮棿
2. cron灞炴ц瀹氱殑鏃墮棿姣旂紦瀛樹俊鎭殑寮濮嬫椂闂存洿鏅?
3. 鏍囩涓紦瀛樼殑鍐呭鍦ㄧ紦瀛樺悗鍙堣閲嶆柊鍒鋒柊榪?
4. 鍏朵粬緙撳瓨瓚呮湡璁懼畾
濡傛灉絎﹀悎涓婇潰鍥涢」涓殑浠諱綍涓欏癸紝琚紦瀛樼殑鍐呭瑙嗕負(fù)宸茬粡澶辨晥錛岃繖鏃惰緙撳瓨鐨勫唴瀹瑰皢琚噸鏂板鐞嗗茍涓旇繑鍥炲鐞嗚繃鍚庣殑淇℃伅錛屽鏋滆緙撳瓨鐨勫唴瀹規(guī)病鏈夊け鏁堬紝閭d箞榪斿洖緇欑敤鎴風(fēng)殑灝嗘槸緙撳瓨涓殑淇℃伅銆?
cache鏍囩鐨勫睘鎬ц鏄?
key - 鏍囪瘑緙撳瓨鍐呭鐨勫叧閿瘝銆傚湪鎸囧畾鐨勪綔鐢ㄨ寖鍥村唴蹇呴』鏄敮涓鐨勩傞粯璁ょ殑key鏄璁塊棶欏甸潰鐨刄RI鍜屽悗闈㈢殑璇鋒眰瀛楃涓層?
浣犲彲浠ュ湪鍚屼竴涓〉闈腑浣跨敤寰堝cache鏍囩鑰屼笉鎸囧畾浠栫殑key灞炴э紝榪欑鎯呭喌涓嬬郴緇熶嬌鐢ㄨ欏甸潰鐨刄RI鍜屽悗闈㈢殑璇鋒眰瀛楃涓詫紝鍙﹀鍐嶈嚜鍔ㄧ粰榪欎簺key澧炲姞涓涓儲寮曞兼潵鍖哄垎榪欎簺緙撳瓨鍐呭銆備絾鏄笉鎺ㄨ崘閲囩敤榪欐牱鐨勬柟寮忋?
scope - 緙撳瓨鍙戠敓浣滅敤鐨勮寖鍥達(dá)紝鍙互鏄痑pplication鎴栬卻ession
time - 緙撳瓨鍐呭鐨勬椂闂存錛屽崟浣嶆槸縐掞紝榛樿鏄?600縐掞紝涔熷氨鏄竴涓皬鏃訛紝濡傛灉璁懼畾涓涓礋鍊鹼紝閭d箞榪欓儴鍒嗚緙撳瓨鐨勫唴瀹瑰皢姘歌繙涓嶈繃鏈熴?
duration - 鎸囧畾緙撳瓨鍐呭澶辨晥鐨勬椂闂達(dá)紝鏄浉瀵箃ime鐨勫彟涓涓夋嫨錛屽彲浠ヤ嬌鐢ㄧ畝鍗曟棩鏈熸牸寮忔垨鑰呯鍚圲SO-8601鐨勬棩鏈熸牸寮忋傚錛歞uration='PT5M' duration='5s'絳?
refresh - false 鎴栬卼rue銆?
濡傛灉refresh灞炴ц緗負(fù)true錛屼笉綆″叾浠栫殑灞炴ф槸鍚︾鍚堟潯浠訛紝榪欓儴鍒嗚緙撳瓨鐨勫唴瀹歸兘灝嗚鏇存柊錛岃繖緇欑紪紼嬭呬竴縐嶉夋嫨錛屽喅瀹氫粈涔堟椂鍊欏繀欏誨埛鏂般?
mode - 濡傛灉緙栫▼鑰呬笉甯屾湜琚紦瀛樼殑鍐呭澧炲姞鍒扮粰鐢ㄦ埛鐨勫搷搴斾腑錛屽彲浠ヨ緗甿ode灞炴т負(fù)"silent"
鍏跺畠鍙敤鐨勫睘鎬ц繕鍖呮嫭錛歝ron 銆乬roups銆乴anguage銆乺efreshpolicyclass銆乺efreshpolicyparam銆?
涓婇潰鐨勮繖浜涘睘鎬у彲浠ュ崟鐙嬌鐢紝涔熷彲浠ユ牴鎹渶瑕佺粍鍚堜嬌鐢紝涓嬮潰鐨勪緥瀛愬皢璁茶В榪欎簺甯哥敤灞炴х殑浣跨敤鏂瑰紡銆?
4.1.2 Cache鏍囩瀹炰緥鍒嗘瀽:
1. 鏈綆鍗曠殑cache鏍囩鐢ㄦ硶
浣跨敤榛樿鐨勫叧閿瓧鏉ユ爣璇哻ache鍐呭錛岃秴鏃舵椂闂存槸榛樿鐨?600縐?
<cache:cache>
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
</cache:cache>
2. 鐢ㄨ嚜宸辨寚瀹氱殑瀛楃涓叉爣璇嗙紦瀛樺唴瀹癸紝騫朵笖璁懼畾浣滅敤鑼冨洿涓簊ession銆?
<cache:cache key="foobar" scope="session">
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
</cache:cache>
3.鍔ㄦ佽瀹歬ey鍊鹼紝浣跨敤鑷繁鎸囧畾鐨則ime灞炴ц瀹氱紦瀛樺唴瀹圭殑瓚呮椂鏃墮棿錛屼嬌鐢ㄥ姩鎬乺efresh鍊煎喅瀹氭槸鍚﹀己鍒跺唴瀹瑰埛鏂般?
鍥犱負(fù)OSCache浣跨敤key鍊兼潵鏍囪瘑緙撳瓨鍐呭錛屼嬌鐢ㄧ浉鍚岀殑key鍊煎皢浼?xì)琚湄?fù)浣跨敤鐩稿悓鐨勭殑緙撳瓨鍐呭錛屾墍浠ヤ嬌鐢ㄥ姩鎬佺殑key鍊煎彲浠ヨ嚜鐢辯殑鏍規(guī)嵁涓嶅悓鐨勮鑹層佷笉鍚岀殑瑕佹眰鍐沖畾浣跨敤涓嶅悓鐨勭紦瀛樺唴瀹廣?
<cache:cache key="<%= product.getId() %>" time="1800" refresh="<%= needRefresh %>">
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
</cache:cache>
4. 璁劇疆time灞炴т負(fù)璐熸暟浣跨紦瀛樺唴瀹規(guī)案涓嶈繃鏈?
<cache:cache time="-1">
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
5. 浣跨敤duration灞炴ц緗秴鏈熸椂闂?
<cache:cache duration='PT5M'>
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
6. 浣跨敤mode灞炴т嬌琚紦瀛樼殑鍐呭涓嶅姞鍏ョ粰瀹㈡埛鐨勫搷搴斾腑
<cache:cache mode='silent'>
<%
//鑷繁鐨凧SP浠g爜鍐呭
%>
4.2 鐢–ashFilter瀹炵幇欏甸潰綰х紦瀛?
鍦?/font>OSCache緇勪歡涓彁渚涗簡涓涓狢acheFilter鐢ㄤ簬瀹炵幇欏甸潰綰х殑緙撳瓨錛屼富瑕佺敤浜庡web搴旂敤涓殑鏌愪簺鍔ㄦ侀〉闈㈣繘琛岀紦瀛橈紝灝ゅ叾鏄偅浜涢渶瑕佺敓鎴恜df鏍煎紡鏂囦歡/鎶ヨ〃銆佸浘鐗囨枃浠剁瓑鐨勯〉闈紝涓嶄粎鍑忓皯浜嗘暟鎹簱鐨勪氦浜掋佸噺灝戞暟鎹簱鏈嶅姟鍣ㄧ殑鍘嬪姏錛岃屼笖瀵逛簬鍑忓皯web鏈嶅姟鍣ㄧ殑鎬ц兘娑堣楁湁寰堟樉钁楃殑鏁堟灉銆?
榪欑鍔熻兘鐨勫疄鐜版槸閫氳繃鍦╳eb.xml涓繘琛岄厤緗潵鍐沖畾緙撳瓨鍝竴涓垨鑰呬竴緇勯〉闈紝鑰屼笖榪樺彲浠ヨ緗紦瀛樼殑鐩稿叧灞炴э紝榪欑鍩轟簬閰嶇疆鏂囦歡鐨勫疄鐜版柟寮忓浜嶫2EE鏉ヨ搴旇鏄竴縐嶆爣鍑嗙殑瀹炵幇鏂瑰紡浜嗐?
[娉╙ 鍙湁瀹㈡埛璁塊棶鏃惰繑鍥瀐ttp澶翠俊鎭腑浠g爜涓?00錛堜篃灝辨槸璁塊棶宸茬粡鎴愬姛錛夌殑欏甸潰淇℃伅鎵嶈兘澶熻緙撳瓨
1. 緙撳瓨鍗曚釜鏂囦歡
淇敼web.xml錛屽鍔犲涓嬪唴瀹癸紝紜畾瀵?testContent.jsp欏甸潰榪涜緙撳瓨銆?
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-瀵?testContent.jsp欏甸潰鍐呭榪涜緙撳瓨-->
<url-pattern>/testContent.jsp</url-pattern>
</filter-mapping>
2. 緙撳瓨URL pattern
淇敼web.xml錛屽鍔犲涓嬪唴瀹癸紝紜畾瀵?.jsp欏甸潰榪涜緙撳瓨銆?
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-瀵規(guī)墍鏈塲sp欏甸潰鍐呭榪涜緙撳瓨-->
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
3. 鑷繁璁懼畾緙撳瓨灞炴?
鍦ㄩ〉闈㈢駭緙撳瓨鐨勬儏鍐典笅錛屽彲浠ラ氳繃璁劇疆CacheFilter鐨勫垵濮嬪睘鎬ф潵鍐沖畾緙撳瓨鐨勪竴浜涚壒鎬э細(xì)time灞炴ц緗紦瀛樼殑鏃墮棿孌碉紝榛樿涓?600縐掞紝鍙互鏍規(guī)嵁鑷繁鐨勯渶瑕佸彧鏈夌殑璁劇疆錛岃宻cope灞炴ц緗紝榛樿涓篴pplication錛屽彲閫夐」鍖呮嫭application銆乻ession
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-瀵規(guī)墍鏈塲sp欏甸潰鍐呭榪涜緙撳瓨-->
<url-pattern>*.jsp</url-pattern>
</filter-mapping>