程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring緩存概述

Spring緩存概述

編輯:關於JAVA

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文件的位置。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved