springCache使用

spring自带缓存springCache使用举例:
<!--缓存-->
在applicationContext_biz中加入下列代码
<bean id="ordersBiz" class="cn.su.erp.biz.impl.OrdersBiz">
        <property name="ordersDao" ref="ordersDao"></property>
        <property name="cacheManager" ref="cacheManager"></property>
    </bean>


    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <!--设置多个cache分工做缓存-->
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" >
                    <property name="name" value="empCache"></property>
                </bean>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" >
                    <property name="name" value="supplierCache"></property>
                </bean>
              
            </set>
        </property>
    </bean>
2.在ordersBiz中注入CacheManager/在父类中注入CacheManager
private CacheManager cacheManager;//加set方法
//从缓存中读取
        Cache Cahce = cacheManager.getCache(CacheName);
        String name = Cahce.get(uuid,String.class);
3.进行增删改的时候删除缓存
cacheManager.getCache("empCache").clear()//清除所有内容
cacheManager.getCache("empCache").evict(emp.getUuid())//清除该键的值









猜你喜欢

转载自blog.csdn.net/lianbancai/article/details/77839452