在spring中使用Ehcache

前提:

使用环境:详细页面

依赖包:

        <dependency>

            <groupId>com.googlecode.ehcache-spring-annotations</groupId>

            <artifactId>ehcache-spring-annotations</artifactId>

            <version>1.2.0</version>

            <type>jar</type>

            <scope>compile</scope>

        </dependency>

        <dependency>

            <groupId>net.sf.ehcache</groupId>

            <artifactId>ehcache-core</artifactId>

            <version>2.6.6</version>

            <type>jar</type>

            <scope>compile</scope>

        </dependency>

        <dependency>

            <groupId>com.google.guava</groupId>

            <artifactId>guava</artifactId>

            <version>r09</version>

            <type>jar</type>

            <scope>compile</scope>

        </dependency>

 

使用方法:

    在applicationContext中:

    <cache:annotation-driven cache-manager="cacheManager"

        proxy-target-class="false" mode="proxy" />

    <!-- <aop:config proxy-target-class="true"></aop:config> -->

    <!-- cacheManager工厂类 -->

    <bean id="cacheManagerFactory"

        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

        <property name="configLocation">

            <value>classpath:ehcache.xml</value>

        </property>

        <property name="shared">

            <value>false</value>

        </property>

    </bean>

    <!-- 声明cacheManager -->

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">

        <property name="cacheManager" ref="cacheManagerFactory"></property>

    </bean>

2:在dao或者service中需要缓存的方法上加上

@Cacheable("items")

3:在ehcache.xml中加上2注解的cache

    <!-- 详细页面 -->

    <cache name="items" maxElementsInMemory="100" maxElementsOnDisk="0"

        eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="100">

    </cache>

4:需要改进的地方

        4.1:不放在内存中,而是放在硬盘上。前提是被缓存的内容以及相关类需要实现Serializable接口

    4.2:如果相关类中使用了list.subList则需要重新包装subList因为subList没有实现Serializable接口。不能序列化  .

    4.3:正常情况下,不能缓存所有内容。因为比如库存等,在更新的时候需要及时更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值