043Spring的缓存机制

本文介绍了Spring的缓存管理,包括如何配置SimpleCacheManager和EhCacheCacheManager,详细讲解了@Cacheable和@CacheEvict的使用,以及提供了一个完整的EhCache实战示例。
摘要由CSDN通过智能技术生成

Spring是否使用缓存中的数据是通过方法参数(缓存的key)来判断的,往下看

在xml配置文件中导入cache命名空间

在xml配置文件中启用Spring缓存(一行)

<cache:annotation-driven cache-manager="缓存管理器的id"/>

所以接下来重要的任务就是配置缓存管理器(两种)

1.配置缓存管理器

1.1.Spring内置的缓存管理器配置(SimpleCacheManager)

SimpleCacheManager只是一种内存中的缓存,并非真正的缓存实现,不推荐在实际项目中使用

<!--使用SimpleCacheManager配置Spring内置的缓存管理器-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <!--使用ConcurrentMapCacheFactoryBean配置多个缓存区-->
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
            p:name="default"/>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
            p:name="users"/>
        </set>
    </property>
</bean>
1.2.EhCache缓存实现的配置(EhCacheCacheManager)

需要的jar包
ehcache-2.10.5.jar
slf4j-api-1.7.25.jar
下载地址:http://d2zwv9pap9ylyd.cloudfront.net/ehcache-2.10.5-distribution.tar.gz

<!--缓存管理器需要配置CacheManager-->
<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
     p:cacheManager-ref="ehCacheManager"/>
<!--利用工厂来创建EhCacheManager-->
<!--p:configLocation指定缓存配置文件-->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.Eh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值