<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    捕風之巢

    統計

    留言簿(3)

    java友情鏈接

    閱讀排行榜

    評論排行榜

    spring+AOP+osCache

    Introduction

    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.

    Example

    We start by defining a bookManager object which has one method

    public ?List?getRelated(Book);

    ?

    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:

    • MemoryCacheInterceptor: a simple in-memory cache that's not meant for production
    • EHCacheInterceptor: uses EHCache from Hibernate and should be configured in ehcache.xml as described in the EHCache documentation.
    • SwarmCacheInterceptor: a clusterable cache implementation
    • OSCacheInterceptor: uses OSCache from OpenSymphony and is the one used in this example

    The cache in the example is expired after 15 minutes.??
    ?????

    < bean? id ="cacheInterceptor" ?
    ????class
    ="org.springframework.aop.interceptor.cache.OSCacheInterceptor" >
    ????
    < property? name ="refreshPeriods" >
    ????????
    < props >
    ????????????
    <!-- ?Cache?the?returned?related?books?for?15?minutes? -->
    ????????????
    < prop? key ="com.example.BookManager@getRelated" > 900 </ prop >
    ????????
    </ props >
    ????
    </ property >
    ????
    <!-- ?For?caches?not?defined?under?'refreshPeriods',?use?this?value? -->
    ????
    < property? name ="defaultRefreshPeriod" >
    ????????
    < value > -1 </ value >
    ????
    </ property >
    ????
    <!-- ?Which?method?to?call?for?non-standard?objects?like?String,?Boolean?or?Number.
    ?This?method?should?be?a?simple?method,?like?getId()?or?toString(),?but?should
    ?uniquely?identify?the?object.?
    -->
    ????
    < property? name ="identifiers" >
    ????????
    < props >
    ????????????
    <!-- ?If?a?method?contains?an?argument?com.example.Book,
    ?????????????the?generated?cache?key?contains?the?value?of?Book.getId()?
    -->
    ????????????
    < prop? key ="com.example.Book" > getId </ prop >
    ????????
    </ props >
    ????
    </ property >
    </ bean >

    ??????

    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.

    <!-- ?An?advisor?that?wraps?bookManager.getRelated()?with?the?cacheInterceptor? -->
    < bean? id ="bookManagerAdvisor" ?
    ????class
    ="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
    ????
    < property? name ="advice" >
    ????????
    < ref? bean ="cacheInterceptor" />
    ????
    </ property >
    ????
    < property? name ="patterns" >
    ????????
    < list >
    ????????????
    < value > .*getRelated </ value >
    ????????
    </ list >
    ????
    </ property >
    </ bean >
    < bean? id ="bookManagerCacheProxyCreator" ?
    ????class
    ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
    ????
    < property? name ="beanNames" >
    ????????
    < value > bookManager </ value >
    ????
    </ property >
    ????
    < property? name ="interceptorNames" >
    ????????
    < list >
    ????????????
    < value > bookManagerAdvisor </ value >
    ????????
    </ list >
    ????
    </ property >
    </ bean >

    ?

    Versions

    The first version was written quite some time ago, but since 2006/03/21 there is a new version ?(2.0.18).

    About

    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!

    Files

    See this XML file?for a sample configuration.
    Eclipse project and source code. More info is available in the javadocs.

    ?Related

    Caching the result of methods using Spring and EHCache
    My AOP cache blog ?and a post about version?2.0.18.
    Attribute based caching

    posted on 2006-10-11 09:43 捕風 閱讀(829) 評論(0)  編輯  收藏 所屬分類: java高級

    主站蜘蛛池模板: 亚洲色精品88色婷婷七月丁香| 午夜网站免费版在线观看| 久久亚洲国产成人影院网站| 国产综合成人亚洲区| 国产午夜影视大全免费观看| 国产成人va亚洲电影| 亚洲精品天堂成人片?V在线播放| 四虎国产精品永免费| 国产亚洲美日韩AV中文字幕无码成人| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 亚洲无人区码一二三码区别图片| 嫖丰满老熟妇AAAA片免费看| 亚洲天堂免费在线| 成人永久免费高清| 久久免费99精品国产自在现线| 国产亚洲人成无码网在线观看 | 国产成人无码区免费A∨视频网站 国产成人涩涩涩视频在线观看免费 | 亚洲一区二区在线免费观看| h片在线免费观看| 亚洲国产成人久久综合| 免费吃奶摸下激烈视频| 在线观看免费黄网站| 亚洲日本乱码一区二区在线二产线| 一二三四免费观看在线视频中文版| 精品亚洲av无码一区二区柚蜜| 亚洲精品国产高清嫩草影院 | 中文字幕a∨在线乱码免费看| 亚洲国产精品久久久久婷婷软件 | 亚洲最大中文字幕无码网站 | 一二三四免费观看在线视频中文版 | 亚洲精品自拍视频| 国产成人精品男人免费| 99精品视频在线观看免费| 亚洲精品午夜久久久伊人| 国产乱弄免费视频| 国内少妇偷人精品视频免费| 亚洲日本国产综合高清| 亚洲深深色噜噜狠狠爱网站| 亚洲天堂免费在线| 又大又硬又粗又黄的视频免费看| 亚洲综合小说久久另类区|