`
Inmethetiger
  • 浏览: 108462 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

在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:正常情况下,不能缓存所有内容。因为比如库存等,在更新的时候需要及时更新。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics