zoukankan      html  css  js  c++  java
  • ehcache注解全面解析

    通过ehcache以编程方式使用缓存:
    跟上面的方式相同,但是缓存通过ehcache去管理,当然比使用map有N多种好处,比如缓存太大了快达到上限之后,将哪一部分缓存清除出去。这种方式完全是通过代码的方式使用ehcache缓存,虽然自由,却也很麻烦;有些比如单纯的场景下,不需要如此麻烦,直接通过注解就行了。
    以前在Spring项目中要通过注解的方式使用缓存,比如借助一个jar包:ehcache-spring-annotations.jar,可以在googlecode上面下载,不过已经很久没有更新过了。
     
     
      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <beans xmlns="http://www.springframework.org/schema/beans"
      3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:osgi="http://www.springframework.org/schema/osgi"
      5     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
      6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      8         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      9         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
     10         http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
     11 
     12     <context:component-scan
     13         base-package="com.mpr.mprsp.mcrc.service.mcrs.precedencecode" />
     14     <!-- ehcache config Start -->
     15     <ehcache:annotation-driven />
     16     <ehcache:config cache-manager="cacheManager">
     17         <ehcache:evict-expired-elements
     18             interval="60" />
     19     </ehcache:config>
     20     <bean id="cacheManager"
     21         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
     22         <property name="configLocation" value="osgibundlejar:/META-INF/ehcache/ehcache.xml" />
     23     </bean>
     24     <!-- ehcache config End -->
     25 
     26     <tx:annotation-driven transaction-manager="txManager" />
     27 
     28     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
     29         <constructor-arg ref="sqlSessionFactory" />
     30     </bean>
     31 
     32     <!-- 定时任务开始 -->
     33     <!-- 线程执行器配置,用于任务注册 -->
     34     <bean id="executor"
     35         class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
     36         <property name="corePoolSize" value="10" />
     37         <property name="maxPoolSize" value="100" />
     38         <property name="queueCapacity" value="500" />
     39     </bean>
     40 
     41     <!-- 业务对象 -->
     42     <bean id="timerTask"
     43         class="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.internal.TimerTask" />
     44 
     45     <!-- 调度业务 update by wangtao 20130715 -->
     46     <bean id="jobDetail"
     47         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     48         <property name="targetObject" ref="timerTask" />
     49         <property name="targetMethod" value="serviceTimerTask" />
     50     </bean>
     51 
     52     <!-- 测试 -->
     53     <bean id="taskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
     54         <property name="jobDetail" ref="jobDetail" />
     55         <!-- 延迟10秒启动 -->
     56         <property name="startDelay" value="10000" />
     57         <!-- 每隔20秒执行一次 -->
     58         <property name="repeatInterval" value="20000" />
     59     </bean>
     60 
     61     <!-- 增加调度触发器 -->
     62     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
     63         <property name="jobDetail" ref="jobDetail" />
     64         <!-- 每天晚上11点触发 -->
     65         <property name="cronExpression" value="0 0 23 * * ?" />
     66     </bean>
     67 
     68     <!-- 增加调度触发器 add by wangtao 20130715 -->
     69     <bean id="autoApplyCodeCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
     70         <property name="jobDetail" ref="jobDetail" />
     71         <!-- 每天中午12点触发 -->
     72         <property name="cronExpression" value="0 0 12 * * ?" />
     73     </bean>
     74 
     75     <!-- 设置调度 update by wangtao 20130715 -->
     76     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     77         <property name="triggers">
     78             <list>
     79                 <!-- <ref bean="cronTrigger" /> -->
     80                 <ref bean="cronTrigger" />
     81             </list>
     82         </property>
     83         <property name="taskExecutor" ref="executor" />
     84     </bean>
     85     <!-- 定时任务结束 -->
     86 
     87     <!-- 引入服务 -->
     88     <!-- 引入文件服务器同步 -->
     89     <osgi:reference id="fileSyncManager"
     90         interface="com.mpr.mprsp.mcrc.service.mcrs.upload.service.IFileSyncManager" />
     91     <!-- 引入系统配置项 -->
     92     <osgi:reference id="sysConfigManager"
     93         interface="com.mpr.mprsp.mcrc.service.mcrs.configuration.service.ISysConfigManager" />
     94     <!-- 引入邮件管理 -->
     95     <osgi:reference id="mailSendManager"
     96         interface="com.mpr.mprsp.mcrc.email.service.IMailSendManager" />
     97     <!-- 引入SqlSessionFactory -->
     98     <osgi:reference id="sqlSessionFactory"
     99         interface="org.apache.ibatis.session.SqlSessionFactory" bean-name="sqlSessionFactory" />
    100     <!-- 引入DataSourceTransactionManager -->
    101     <osgi:reference id="txManager"
    102         interface="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    103         bean-name="txManager" />
    104     <!-- 引入ParametersMapBuilder -->
    105     <osgi:reference id="parametersMapBuilder"
    106         interface="com.isoftstone.agiledev.core.query.mybatis.ParametersMapBuilder"
    107         bean-name="parametersMapBuilder" />
    108 
    109     <!-- 暴露服务 -->
    110     <!-- 暴露MPR出版物内容类型 -->
    111     <osgi:service ref="publicationContentTypeManager"
    112         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IPublicationContentTypeManager" />
    113     <!-- 暴露语种管理 -->
    114     <osgi:service ref="languageManager"
    115         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.ILanguageManager" />
    116     <!-- 暴露码段管理 -->
    117     <osgi:service ref="mprCodeSegmentManager"
    118         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IMprCodeSegmentManager" />
    119     <!-- 暴露预警阀值 -->
    120     <osgi:service ref="mprCodeThresholdManager"
    121         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IMprCodeThresholdManager" />
    122     <!-- 暴露MPR前置码生效信息管理 -->
    123     <osgi:service ref="caroperInfoSyncManager"
    124         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.ICaroperInfoSyncManager" />
    125     <!-- 暴露MPR前置码文件管理 -->
    126     <osgi:service ref="mprFileManager"
    127         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IMprFileManager" />
    128     <!-- 暴露撤销记录管理 -->
    129     <osgi:service ref="revocationStatsManager"
    130         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IRevocationStatsManager" />
    131     <!-- 暴露废除记录管理 -->
    132     <osgi:service ref="abolishStatsManager"
    133         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IAbolishStatsManager" />
    134     <!-- 暴露图书业务管理 -->
    135     <osgi:service ref="publicationManager"
    136         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IPublicationManager" />
    137     <!-- 暴露 期刊业务管理 -->
    138     <osgi:service ref="periodicalManager"
    139         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IPeriodicalManager" />
    140     <!-- 暴露报纸业务管理 -->
    141     <osgi:service ref="newsPaperManager"
    142         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.INewsPaperManager" />
    143     <!-- 暴露音像业务管理 -->
    144     <osgi:service ref="audioManager"
    145         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IAudioManager" />
    146     <!-- 暴露(组区)MPR出版物管理 -->
    147     <osgi:service ref="publicationAreaManager"
    148         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IPublicationAreaManager" />
    149     <!-- MPR出版物视图服务类 -->
    150     <osgi:service ref="publicationViewManager"
    151         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IPublicationViewManager" />
    152     <!-- MPR 资源类 -->
    153     <osgi:service ref="mprCodeResourceManager"
    154         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IMprCodeResourceManager" />
    155     <!-- 关联关系管理 -->
    156     <osgi:service ref="isliMprCodeRelationManager"
    157         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IIsliMprCodeRelationManager" />
    158         
    159      <!-- option 下拉选框数据 -->
    160     <osgi:service ref="optionManager"
    161         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IOptionManager" />
    162     
    163     <osgi:service ref="apiPublicationService"
    164         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IApiPublicationService" />
    165         
    166         
    167         
    168     <!-- openservice 暴露前置编码服务接口 -->
    169     <osgi:service ref="openService"
    170         interface="com.mpr.mprsp.mcrc.service.mcrs.precedencecode.service.IOpenService" />
    171         
    172     <osgi:reference id="sysLogManager" 
    173         interface="com.mpr.mprsp.mcrc.service.mcrs.system.service.ISysLogManager"/>
    174         
    175     <osgi:reference id="statsMprManager"
    176         interface="com.mpr.mprsp.mcrc.service.mcrs.statistics.service.IStatsMpr" />
    177     <osgi:reference id="statsPublication"
    178         interface="com.mpr.mprsp.mcrc.service.mcrs.statistics.service.IStatsPublication" />
    179     <osgi:reference id="statsPublisher"
    180         interface="com.mpr.mprsp.mcrc.service.mcrs.statistics.service.IStatsPublisher" />
    181     <osgi:reference id="publisherGroupManager"
    182         interface="com.mpr.mprsp.mcrc.service.mcrs.publisher.service.IPublisherGroupManager" />
    183 </beans>
    View Code
    共有两个注解:
    • @Cacheabele      :指定方法使用缓存
    • @TriggersRemove  :从缓存中移除对象
    在查询的方法上使用缓存:
        @SuppressWarnings("unchecked")
        @Cacheable(cacheName = "publicationsCache")
        public List<PublicationViewInfo> query(PublicationViewInfoParams params)
            throws Exception
     
    在删除或更新的方法上清空缓存:
    @Override
        @TriggersRemove(cacheName = {"publicationsCache"},
                        when = When.AFTER_METHOD_INVOCATION,
                        removeAll = true)
        public int hiddenResource(Long resourceId, String publicationType,
                Boolean isHidden) throws Exception {
  • 相关阅读:
    IFS二次开发03——Item
    TFS二次开发01——TeamProjectsPicher
    我的八年程序之路(二)三月方便面换来800月薪
    我的八年程序之路(一)求职的艰辛
    重温CSS之背景、文本样式
    重温CSS之文档结构
    重温CSS之基础
    不使用第三方软件、使用IE11自带功能来屏蔽浏览器广告
    Windwos8.1下配置PHP环境
    Python学习系列之(二)图解Windows8.1下安装Django
  • 原文地址:https://www.cnblogs.com/wanghongye/p/5549797.html
Copyright © 2011-2022 走看看