Spring 3.1 版本的缓存实现

Spring3.1 已加缓存的实现,今天主要介绍一下Spring Cache的基本使用,要想使你的程序具体缓存的功能,首先须要在配置文件中增长如下配置:css

一、若是使用Annotation 方式配置缓存,须要有如下配置:html

<cache:annotation-driven />
 
默认的缓存管理器的名字为cacheManager,若是须要指定缓存管理器名称,须要指定其名称
如:
<cache:annotation-driven cache-manager="cacheManager"/>
 

设置好注解配置事后,须要设置缓存管理器。spring

<!-- 缓存管理器声明-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
    <set>
        <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>     
        <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="base"/>          
    </set>
    </property>
</bean>

当前配置的是用ConcurrentMap做为缓存容器,固然,也能够自定义缓存容器,具体参见srping 文档,上述配置指定的两个缓存管理器,分别为:default,base。缓存

完整配置以下:spa

 1: <beans xmlns="http://www.springframework.org/schema/beans"
 2:     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3:     xmlns:cache="http://www.springframework.org/schema/cache"
 4:     xmlns:p="http://www.springframework.org/schema/p"
 5:     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6:         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
 7: 
 8: 
 9:     <cache:annotation-driven cache-manager="cacheManager"/>
 10: 
 11:  
 12:     <!-- 缓存管理器声明-->
 13:     <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
 14:         <property name="caches">
 15:         <set>
 16:             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
 17:             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="base"/>
 18:         </set>
 19:         </property>
 20:     </bean>
 21:  
 22: </beans>

二、XML配置好事后,如何在程序中使用,请看下面介绍code

若是须要缓存某个业务的字典数据,可直接在方法增长Cacheable标
xml

@Cacheable(value = "base", key = "#root.methodName")
    public List<String> getCredits()
    {
        logger.info("开始查询证件类型");
        List<String> credits = new ArrayList<String>();
        credits.add("身份证");
        credits.add("军官证");
        credits.add("学生证");
        return credits;
    }

在该方法上增长@Cacheable注解,Spring会自动把返回的数据加到缓存中,而且把数据缓存在base容器中,同时设定的key的名称为: 方法名。
key的名称定能够采用SPEL表达式。htm

 

@Cacheable(value = "base", key = "#type")
    public List<String> findSeats(String type) {
        
        List<String> seats = new ArrayList<String>();
        if ("seat".equals(type)) {
            logger.info("开始查询席位类型");
            int base = 97;
            // 构建Seat记录
            for (int i = 0; i < 26; i++) {
                int tmp = base + i;
                seats.add(String.valueOf((char) tmp));
            }
        }

        else if ("ticket".equals(type)) {
            logger.info("开始查询票种类型");
            int base = 65;
            // 构建Seat记录
            for (int i = 0; i < 26; i++) {
                int tmp = base + i;
                seats.add(String.valueOf((char) tmp));
            }
        }
        return seats;
    }

该示例演示了,缓存的数据以type为key。rem

三、缓存数据更新文档

示例:

 

/**
 * 当发生席位记录变动,须要清除现有的缓存。
 */
    @CacheEvict(value = "base", key = "#type")
    public void updateSeats(String type) {

        logger.info("开始更新席位记录");

    }

在更新的方法中,增长@CacheEvict 注解,而且要指定须要更新的是哪一个容器和key,若是没有指定key,那该容器的全部数据都会更新。

相关文章
相关标签/搜索