Spring + Cache/Ehcache


Spring + Cache

http://haohaoxuexi.iteye.com/blog/2123030


@Cacheable

可以標記方法/類,表示該方法/類支持緩存。

該標籤有三個屬性:

value: 必須指定,方法返回時返回到哪個cache上

   @Cacheable("cache1")//Cache是發生在cache1上的
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable({"cache1", "cache2"})//Cache是發生在cache1和cache2上的
   public User find(Integer id) {
      return null;
   }

key: 指定方法返回結果對應的key(key = "#參數名" / key = "#p參數index"),不指定時使用默認政策生成key

   @Cacheable(value="users", key="#id")
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable(value="users", key="#p0")
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable(value="users", key="#user.id")
   public User find(User user) {
      return null;
   }
 
   @Cacheable(value="users", key="#p0.id")
   public User find(User user) {
      return null;
   }

       key默認生成策略:

1、若方法沒有參數,適用0作爲key

2、若方法只有一個參數,則作爲key

3、若方法有多於一個參數,則所有參數的hashcode作爲key

除了方法參數,還可以通過一個root對象生成key

   @Cacheable(value={"users", "xxx"}, key="caches[1].name")  //省略了#root
   public User find(User user) {
      return null;
   }

condition: 指定發生的條件,條件爲true/false,默認爲空即緩存方法所有的返回結果

   @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
   public User find(User user) {
      System.out.println("find user by user " + user);
      return user;
   }


@CachePut

使用@Cacheable標註的方法,每次都會從緩存中查找相同key的緩存元素,若存在就不執行該方法,直接返回緩存元素,否則纔會執行將結果存入緩存。

@CachePut每次都會執行並將結果存入緩存。


@CacheEvict

標註在需要清楚緩存元素的類或方法上,可以指定的屬性有value,key,condition,allEntries和beforeInnovation

allEntries: 是否需要清楚緩存中所有元素,默認false

beforeInnovation: 清除操作默認在對應方法執行之後,該屬性爲true時在方法執行前清除指定緩存


@Caching

可以在一個類或方法上標註多個Spring Cache註解,三個屬性cacheable、put和evict分別指定前三種註解。

   @Caching(cacheable = @Cacheable("users"), 
	evict = { @CacheEvict("cache2"), @CacheEvict(value = "cache3", allEntries = true) })
   public User find(Integer id) {
      return null;
   }

</pre>自定義註解</p><p><pre name="code" class="java">@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Cacheable(value="users")
public @interface MyCacheable {
 
}

  @MyCacheable
   public User findById(Integer id) {
      System.out.println("find user by id: " + id);
      User user = new User();
      user.setId(id);
      user.setName("Name" + id);
      return user;
   }



Spring對Cache的支持

1、基於註解

    <cache:annotation-driven/>
cache標籤有一個cache-manager屬性指定當前所指定的Cache Manager的bean的名稱,默認時cacheManager。

還有一個mode屬性,可選proxy和aspectj,默認proxy

proxy:只有緩存方法被外部調用時才起作用;只有public方法上的cache註解才起作用

aspectj:沒有以上兩種限制


2、基於XML




配置CacheManager

CacheManager是Spring管理Cache的接口,spring本身提供了兩種實現:一種時基於Java的CurrentHashMap,一種時基於第三方cache實現Ehcache

1、基於CurrentHashMap

 <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
      <property name="caches">
         <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="xxx"/>
         </set>
      </property>
   </bean>


2、基於Ehcache

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

ehcacheManager的默認位置是根路徑下的ehcache.xml

   <!-- 定義CacheManager -->
   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
      <!-- 指定配置文件的位置 -->
      <property name="configLocation" value="/WEB-INF/config/ehcache.xml"/>
      <!-- 指定新建的CacheManager的名稱 -->
      <property name="cacheManagerName" value="cacheManagerName"/>
   </bean>


   <!-- 定義一個Ehcache -->
   <bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
      <property name="cacheName" value="user"/>
      <property name="cacheManager" ref="cacheManager"/>
   </bean>



Ehcache

http://haohaoxuexi.iteye.com/blog/2112170

Ehcache是一個管理緩存的工具,可以緩存在內存裏和硬盤上。核心是CacheManager,一個應用可以有多個CacheManager,一個CacheManager可以管理多個Cache,Cache內部保存一組Element,每個Element是一個<K, V>鍵值對。

特性: http://raychase.iteye.com/blog/1545906

1、快速輕量

6、應用持久化

7、監聽器

8、開啓JMX

9、分佈式緩存


CacheManager負責Cache的創建、移除和訪問。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章