当前位置:天才代写 > tutorial > JAVA 教程 > Spring缓存概述

Spring缓存概述

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:732

Spring的Cache用于Java要领,淘汰要领的执行。在每一次一个target要领挪用时,Spring就会按照要领、参数值来查抄是否这个要领被执行过。假如之前执行过,就直接取得上次执行的功效。假如没有则执行,并缓存功效。

Spring Cache只是一个抽象,没有相应的实现。它的实现利用了两种存储计策。1、JDK的java.util.concurrent.ConcurrentMap;2、Ehcache

对付Spring 3.1 以上版本(包罗Spring 3.1)Spring cache Abastract相存眷解

一般利用三个注解:@Cacheable 、@CacheEvict

@Cacheable 指定某个要领利用并读写缓存。

@CacheEvict指定某个要领利用并从缓存中移除数据。

@CachePut 指定某个要领利用并只写缓存。

上面三种注解设置要领时,一个要领只能利用三者之一。假如要利用多个,则需要利用@Caching 。

Spring 利用Cache,需要在applicationContext.xml做相应的设置,以支持缓存。

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

Spring Cache的两种存储计策

之前已经说了,Spring Cache并没有提供的详细实现,需要别的两种存储计策的支持。

下面就说说着两种存储计策。

1、ConcurrentMap

利用ConcurrentMap作为存储计策时,只需要指定一个SimpleCacheManager作为CacheManager的实例,并设置N个定名的ConcurrentMapCacheFactoryBean作为缓存。譬喻:

<!-- generic cache manager -->
<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="books" />
    </set>
    </property>
</bean>

上面的p:name就是缓存区间的名字,譬喻p:name="books",就是在注解@Cacheable(value="books")利用。

2、Ehcache

假如是利用Ehcache作为Spring Cache的详细存储计策,那么做如下的设置即可。

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
      
<!-- EhCache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache.xml" />

ehcache.xml是Ehcache设置Cache的。

Spring 2.5-Spring3.1 版本利用Cache

在Spring 3.1以前的版本中没有上面说的注解。如何让Spring利用cache呢?

Ehcache提供了对Spring的早期版本的支持。

在pom.xml中添加依赖如下:

<dependency>
     <groupId>com.googlecode.ehcache-spring-annotations</groupId>
     <artifactId>ehcache-spring-annotations</artifactId>
     <version>1.2.0</version>
</dependency>

这个jar包中有两个注解@Cacheable、@TriggersRemove

@Cacheabele:指定要领是利用缓存。

@TriggersRemove:从缓存中移除工具。、

相存眷解的利用,拜见:https://code.google.com/p/ehcache-spring-annotations/

在applicationContext.xml中添加Ehcache的支持:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
     
     <ehcache:annotation-driven  />
     <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60"  />
    </ehcache:config>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation"  value="/WEB-INF/ehcache.xml" />
     
</bean>
     
    <!-- rest of the file omitted -->
 </beans>

上面的设置中,也同样指定了ehcache文件的位置。

 

    关键字:

天才代写-代写联系方式