码迷,mamicode.com
首页 > 编程语言 > 详细

spring的缓存机制

时间:2014-10-31 19:19:45      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:spring   ehcache   缓存机制   

    在Spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果;2.在某个方法执行前或后清空缓存。

    spring是怎么进行缓存的,白话点讲就是:一个map来进行缓存,当调用aop时访问缓存,判断是否有对应数据存在。具体如下:

1.EHCache

Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能。其中,spring对EHCache提供了很好的支持。下面我们以EHCache为例来介绍spring的缓存配置。

在介绍Spring的缓存配置之前,我们先看一下EHCache是如何配置。

<?xml version="1.0" encoding="UTF-8" ?><ehcache>
    <!-- 定义默认的缓存区,如果在未指定缓存区时,默认使用该缓存区 -->
    <defaultCache maxElementsInMemory="500" eternal="true"
        overflowToDisk="false" memoryStoreEvictionPolicy="LFU">
    </defaultCache>
    <!-- 定义名字为"dao.select"的缓存区 -->
    <cache name="dao.select" maxElementsInMemory="500" eternal="true"
        overflowToDisk="false" memoryStoreEvictionPolicy="LFU" /></ehcache>

2.Spring缓存机制中的Advice

由于Spring的缓存机制是基于Spring的AOP,那么在Spring Cache中应该存在着一个Advice。没错,在Spring Cache中的Advice是存在的,它就是org.springframework.cache.Cache,但 spring并不是直接使用org.springframework.cache.Cache,spring把Cache对象交给 org.springframework.cache.CacheManager来管理。

在spring对EHCache的支持中,org.springframework.cache.ehcache.EhCacheManager就是org.springframework.cache.CacheManager的一个实现

<!-- 
        该Bean是一个org.springframework.cache.CacheManager对象
        属性cacheManager是一个net.sf.ehcache.CacheManager对象     -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager">
            <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation" value="classpath:ehcache-config.xml"></property>
            </bean>
        </property>
    </bean>

3.基于xml配置方式配置缓存

在上一节中,我们得到了cacheManagr,那么我们就可以对某些方法配置缓存了。下面是基于xml方式的缓存配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     ">

    <context:component-scan base-package="com.sin90lzc"></context:component-scan>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager">
            <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation" value="classpath:ehcache-config.xml"></property>
            </bean>
        </property>
    </bean>

    <cache:advice id="cacheAdvice" cache-manager="cacheManager">
        <cache:caching>
            <cache:cacheable cache="dao.select" method="select"
                key="#id" />
            <cache:cache-evict cache="dao.select" method="save"
                key="#obj" />
        </cache:caching>
    </cache:advice>

    <aop:config>
        <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.sin90lzc.train.spring_cache.simulation.DaoImpl.*(..))"/>
    </aop:config></beans>


4.注解驱动的缓存配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <context:component-scan base-package="com.sin90lzc"></context:component-scan>

    <!-- 
        该Bean是一个org.springframework.cache.CacheManager对象
        属性cacheManager是一个net.sf.ehcache.CacheManager对象     -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager">
            <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation" value="classpath:ehcache-config.xml"></property>
            </bean>
        </property>
    </bean>
    
    <cache:annotation-driven /></beans>


package com.sin90lzc.train.spring_cache.simulation;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;


@Componentpublic class DaoImpl implements Dao {    /**
     * value定义了缓存区(缓存区的名字),每个缓存区可以看作是一个Map对象
     * key作为该方法结果缓存的唯一标识,     */
    @Cacheable(value = { "dao.select" },key="#id")
    @Override    public Object select(int id) {
        System.out.println("do in function select()");        return new Object();
    }

    @CacheEvict(value = { "dao.select" }, key="#obj")
    @Override    public void save(Object obj) {
        System.out.println("do in function save(obj)");
    }

}

bubuko.com,布布扣


本文出自 “流星雨的IT路程” 博客,请务必保留此出处http://lxy2020.blog.51cto.com/2528961/1570432

spring的缓存机制

标签:spring   ehcache   缓存机制   

原文地址:http://lxy2020.blog.51cto.com/2528961/1570432

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!