強大的Spring緩存技術(上)

緩存是實際工作中非常常用的一種提高性能的方法, 我們會在許多場景下來使用緩存。

本文通過一個簡單的例子進行展開,通過對比我們原來的自定義緩存和 spring 的基於註釋的 cache 配置方法,展現了 spring cache 的強大之處,然後介紹了其基本的原理,擴展點和使用場景的限制。通過閱讀本文,你應該可以短時間內掌握 spring 帶來的強大緩存技術,在很少的配置下即可給既有代碼提供緩存能力。

概述

Spring 3.1 引入了激動人心的基於註釋(annotation)的緩存(cache)技術,它本質上不是一個具體的緩存實現方案(例如EHCache 或者 OSCache),而是一個對緩存使用的抽象,通過在既有代碼中添加少量它定義的各種 annotation,即能夠達到緩存方法的返回對象的效果。

Spring 的緩存技術還具備相當的靈活性,不僅能夠使用 SpEL(Spring Expression Language)來定義緩存的 key 和各種 condition,還提供開箱即用的緩存臨時存儲方案,也支持和主流的專業緩存例如 EHCache 集成。

其特點總結如下:

  • 通過少量的配置 annotation 註釋即可使得既有代碼支持緩存

  • 支持開箱即用 Out-Of-The-Box,即不用安裝和部署額外第三方組件即可使用緩存

  • 支持 Spring Express Language,能使用對象的任何屬性或者方法來定義緩存的 key 和 condition

  • 支持 AspectJ,並通過其實現任何方法的緩存支持

  • 支持自定義 key 和自定義緩存管理者,具有相當的靈活性和擴展性

本文將針對上述特點對 Spring cache 進行詳細的介紹,主要通過一個簡單的例子和原理介紹展開,然後我們將一起看一個比較實際的緩存例子,最後會介紹 spring cache 的使用限制和注意事項。好吧,讓我們開始吧

我們以前如何自己實現緩存的呢

這裏先展示一個完全自定義的緩存實現,即不用任何第三方的組件來實現某種對象的內存緩存。

場景如下:

對一個賬號查詢方法做緩存,以賬號名稱爲 key,賬號對象爲 value,當以相同的賬號名稱查詢賬號的時候,直接從緩存中返回結果,否則更新緩存。賬號查詢服務還支持 reload 緩存(即清空緩存)

首先定義一個實體類:賬號類,具備基本的 id 和 name 屬性,且具備 getter 和 setter 方法

  1. public class Account {  
  2.     private int id;  
  3.     private String name;  
  4.     public Account(String name) {  
  5.         this.name = name;  
  6.     }  
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19. }  
save_snippets.png
public class Account {
    private int id;
    private String name;
    public Account(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

然後定義一個緩存管理器,這個管理器負責實現緩存邏輯,支持對象的增加、修改和刪除,支持值對象的泛型。如下:

  1. import com.google.common.collect.Maps;  
  2. import java.util.Map;  
  3. /** 
  4.  * @author wenchao.ren 
  5.  *         2015/1/5. 
  6.  */  
  7. public class CacheContext<T> {  
  8.     private Map<String, T> cache = Maps.newConcurrentMap();  
  9.     public T get(String key){  
  10.         return  cache.get(key);  
  11.     }  
  12.     public void addOrUpdateCache(String key,T value) {  
  13.         cache.put(key, value);  
  14.     }  
  15.     // 根據 key 來刪除緩存中的一條記錄  
  16.     public void evictCache(String key) {  
  17.         if(cache.containsKey(key)) {  
  18.             cache.remove(key);  
  19.         }  
  20.     }  
  21.     // 清空緩存中的所有記錄  
  22.     public void evictCache() {  
  23.         cache.clear();  
  24.     }  
  25. }  
save_snippets.png
import com.google.common.collect.Maps;
import java.util.Map;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
public class CacheContext<T> {
    private Map<String, T> cache = Maps.newConcurrentMap();
    public T get(String key){
        return  cache.get(key);
    }
    public void addOrUpdateCache(String key,T value) {
        cache.put(key, value);
    }
    // 根據 key 來刪除緩存中的一條記錄
    public void evictCache(String key) {
        if(cache.containsKey(key)) {
            cache.remove(key);
        }
    }
    // 清空緩存中的所有記錄
    public void evictCache() {
        cache.clear();
    }
}

好,現在我們有了實體類和一個緩存管理器,還需要一個提供賬號查詢的服務類,此服務類使用緩存管理器來支持賬號查詢緩存,如下:

  1. import com.google.common.base.Optional;  
  2. import org.slf4j.Logger;  
  3. import org.slf4j.LoggerFactory;  
  4. import org.springframework.stereotype.Service;  
  5. import javax.annotation.Resource;  
  6. /** 
  7.  * @author wenchao.ren 
  8.  *         2015/1/5. 
  9.  */  
  10. @Service  
  11. public class AccountService1 {  
  12.     private final Logger logger = LoggerFactory.getLogger(AccountService1.class);  
  13.     @Resource  
  14.     private CacheContext<Account> accountCacheContext;  
  15.     public Account getAccountByName(String accountName) {  
  16.         Account result = accountCacheContext.get(accountName);  
  17.         if (result != null) {  
  18.             logger.info("get from cache... {}", accountName);  
  19.             return result;  
  20.         }  
  21.         Optional<Account> accountOptional = getFromDB(accountName);  
  22.         if (!accountOptional.isPresent()) {  
  23.             throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));  
  24.         }  
  25.         Account account = accountOptional.get();  
  26.         accountCacheContext.addOrUpdateCache(accountName, account);  
  27.         return account;  
  28.     }  
  29.     public void reload() {  
  30.         accountCacheContext.evictCache();  
  31.     }  
  32.     private Optional<Account> getFromDB(String accountName) {  
  33.         logger.info("real querying db... {}", accountName);  
  34.         //Todo query data from database  
  35.         return Optional.fromNullable(new Account(accountName));  
  36.     }  
  37. }  
save_snippets.png
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
@Service
public class AccountService1 {
    private final Logger logger = LoggerFactory.getLogger(AccountService1.class);
    @Resource
    private CacheContext<Account> accountCacheContext;
    public Account getAccountByName(String accountName) {
        Account result = accountCacheContext.get(accountName);
        if (result != null) {
            logger.info("get from cache... {}", accountName);
            return result;
        }
        Optional<Account> accountOptional = getFromDB(accountName);
        if (!accountOptional.isPresent()) {
            throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
        }
        Account account = accountOptional.get();
        accountCacheContext.addOrUpdateCache(accountName, account);
        return account;
    }
    public void reload() {
        accountCacheContext.evictCache();
    }
    private Optional<Account> getFromDB(String accountName) {
        logger.info("real querying db... {}", accountName);
        //Todo query data from database
        return Optional.fromNullable(new Account(accountName));
    }
}

現在我們開始寫一個測試類,用於測試剛纔的緩存是否有效

  1. import org.junit.Before;  
  2. import org.junit.Test;  
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import static org.junit.Assert.*;  
  7. public class AccountService1Test {  
  8.     private AccountService1 accountService1;  
  9.     private final Logger logger = LoggerFactory.getLogger(AccountService1Test.class);  
  10.     @Before  
  11.     public void setUp() throws Exception {  
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");  
  13.         accountService1 = context.getBean("accountService1", AccountService1.class);  
  14.     }  
  15.     @Test  
  16.     public void testInject(){  
  17.         assertNotNull(accountService1);  
  18.     }  
  19.     @Test  
  20.     public void testGetAccountByName() throws Exception {  
  21.         accountService1.getAccountByName("accountName");  
  22.         accountService1.getAccountByName("accountName");  
  23.         accountService1.reload();  
  24.         logger.info("after reload ....");  
  25.         accountService1.getAccountByName("accountName");  
  26.         accountService1.getAccountByName("accountName");  
  27.     }  
  28. }  
save_snippets.png
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class AccountService1Test {
    private AccountService1 accountService1;
    private final Logger logger = LoggerFactory.getLogger(AccountService1Test.class);
    @Before
    public void setUp() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
        accountService1 = context.getBean("accountService1", AccountService1.class);
    }
    @Test
    public void testInject(){
        assertNotNull(accountService1);
    }
    @Test
    public void testGetAccountByName() throws Exception {
        accountService1.getAccountByName("accountName");
        accountService1.getAccountByName("accountName");
        accountService1.reload();
        logger.info("after reload ....");
        accountService1.getAccountByName("accountName");
        accountService1.getAccountByName("accountName");
    }
}

按照分析,執行結果應該是:首先從數據庫查詢,然後直接返回緩存中的結果,重置緩存後,應該先從數據庫查詢,然後返回緩存中的結果. 查看程序運行的日誌如下:

00:53:17.166 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName

00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName

00:53:17.168 [main] INFO  c.r.s.c.example1.AccountServiceTest - after reload ....

00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName

00:53:17.169 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName

可以看出我們的緩存起效了,但是這種自定義的緩存方案有如下劣勢:

  • 緩存代碼和業務代碼耦合度太高,如上面的例子,AccountService 中的 getAccountByName()方法中有了太多緩存的邏輯,不便於維護和變更

  • 不靈活,這種緩存方案不支持按照某種條件的緩存,比如只有某種類型的賬號才需要緩存,這種需求會導致代碼的變更

  • 緩存的存儲這塊寫的比較死,不能靈活的切換爲使用第三方的緩存模塊

如果你的代碼中有上述代碼的影子,那麼你可以考慮按照下面的介紹來優化一下你的代碼結構了,也可以說是簡化,你會發現,你的代碼會變得優雅的多!

Spring cache是如何做的呢

我們對AccountService1 進行修改,創建AccountService2:

  1. import com.google.common.base.Optional;  
  2. import com.rollenholt.spring.cache.example1.Account;  
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.cache.annotation.Cacheable;  
  6. import org.springframework.stereotype.Service;  
  7. /** 
  8.  * @author wenchao.ren 
  9.  *         2015/1/5. 
  10.  */  
  11. @Service  
  12. public class AccountService2 {  
  13.     private final Logger logger = LoggerFactory.getLogger(AccountService2.class);  
  14.     // 使用了一個緩存名叫 accountCache  
  15.     @Cacheable(value="accountCache")  
  16.     public Account getAccountByName(String accountName) {  
  17.         // 方法內部實現不考慮緩存邏輯,直接實現業務  
  18.         logger.info("real querying account... {}", accountName);  
  19.         Optional<Account> accountOptional = getFromDB(accountName);  
  20.         if (!accountOptional.isPresent()) {  
  21.             throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));  
  22.         }  
  23.         return accountOptional.get();  
  24.     }  
  25.     private Optional<Account> getFromDB(String accountName) {  
  26.         logger.info("real querying db... {}", accountName);  
  27.         //Todo query data from database  
  28.         return Optional.fromNullable(new Account(accountName));  
  29.     }  
  30. }  
save_snippets.png
import com.google.common.base.Optional;
import com.rollenholt.spring.cache.example1.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
@Service
public class AccountService2 {
    private final Logger logger = LoggerFactory.getLogger(AccountService2.class);
    // 使用了一個緩存名叫 accountCache
    @Cacheable(value="accountCache")
    public Account getAccountByName(String accountName) {
        // 方法內部實現不考慮緩存邏輯,直接實現業務
        logger.info("real querying account... {}", accountName);
        Optional<Account> accountOptional = getFromDB(accountName);
        if (!accountOptional.isPresent()) {
            throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
        }
        return accountOptional.get();
    }
    private Optional<Account> getFromDB(String accountName) {
        logger.info("real querying db... {}", accountName);
        //Todo query data from database
        return Optional.fromNullable(new Account(accountName));
    }
}

我們注意到在上面的代碼中有一行:

@Cacheable(value="accountCache")

這個註釋的意思是,當調用這個方法的時候,會從一個名叫 accountCache 的緩存中查詢,如果沒有,則執行實際的方法(即查詢數據庫),並將執行的結果存入緩存中,否則返回緩存中的對象。這裏的緩存中的 key 就是參數 accountName,value 就是 Account 對象。“accountCache”緩存是在 spring*.xml 中定義的名稱。我們還需要一個 spring 的配置文件來支持基於註釋的緩存

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:cache="http://www.springframework.org/schema/cache"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans.xsd  
  7. http://www.springframework.org/schema/context  
  8. http://www.springframework.org/schema/context/spring-context.xsd  
  9. http://www.springframework.org/schema/cache  
  10. http://www.springframework.org/schema/cache/spring-cache.xsd">  
  11.     <context:component-scan base-package="com.rollenholt.spring.cache"/>  
  12.     <context:annotation-config/>  
  13.     <cache:annotation-driven/>  
  14.     <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
  15.         <property name="caches">  
  16.             <set>  
  17.                 <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
  18.                     <property name="name" value="default"/>  
  19.                 </bean>  
  20.                 <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
  21.                     <property name="name" value="accountCache"/>  
  22.                 </bean>  
  23.             </set>  
  24.         </property>  
  25.     </bean>   
  26. </beans>  
save_snippets.png
<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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
    <context:component-scan base-package="com.rollenholt.spring.cache"/>
    <context:annotation-config/>
    <cache:annotation-driven/>
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"/>
                </bean>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="accountCache"/>
                </bean>
            </set>
        </property>
    </bean> 
</beans>

注意這個 spring 配置文件有一個關鍵的支持緩存的配置項:

<cache:annotation-driven />

這個配置項缺省使用了一個名字叫 cacheManager 的緩存管理器,這個緩存管理器有一個 spring 的缺省實現,即 org.springframework.cache.support.SimpleCacheManager,這個緩存管理器實現了我們剛剛自定義的緩存管理器的邏輯,它需要配置一個屬性 caches,即此緩存管理器管理的緩存集合,除了缺省的名字叫 default 的緩存,我們還自定義了一個名字叫 accountCache 的緩存,使用了缺省的內存存儲方案 ConcurrentMapCacheFactoryBean,它是基於 Java.util.concurrent.ConcurrentHashMap 的一個內存緩存實現方案。

然後我們編寫測試程序:

  1. import org.junit.Before;  
  2. import org.junit.Test;  
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import static org.junit.Assert.*;  
  7. public class AccountService2Test {  
  8.     private AccountService2 accountService2;  
  9.     private final Logger logger = LoggerFactory.getLogger(AccountService2Test.class);  
  10.     @Before  
  11.     public void setUp() throws Exception {  
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");  
  13.         accountService2 = context.getBean("accountService2", AccountService2.class);  
  14.     }  
  15.     @Test  
  16.     public void testInject(){  
  17.         assertNotNull(accountService2);  
  18.     }  
  19.     @Test  
  20.     public void testGetAccountByName() throws Exception {  
  21.         logger.info("first query...");  
  22.         accountService2.getAccountByName("accountName");  
  23.         logger.info("second query...");  
  24.         accountService2.getAccountByName("accountName");  
  25.     }  
  26. }  
save_snippets.png
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class AccountService2Test {
    private AccountService2 accountService2;
    private final Logger logger = LoggerFactory.getLogger(AccountService2Test.class);
    @Before
    public void setUp() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        accountService2 = context.getBean("accountService2", AccountService2.class);
    }
    @Test
    public void testInject(){
        assertNotNull(accountService2);
    }
    @Test
    public void testGetAccountByName() throws Exception {
        logger.info("first query...");
        accountService2.getAccountByName("accountName");
        logger.info("second query...");
        accountService2.getAccountByName("accountName");
    }
}

上面的測試代碼主要進行了兩次查詢,第一次應該會查詢數據庫,第二次應該返回緩存,不再查數據庫,我們執行一下,看看結果

01:10:32.435 [main] INFO  c.r.s.c.example2.AccountService2Test - first query...

01:10:32.456 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying account... accountName

01:10:32.457 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying db... accountName

01:10:32.458 [main] INFO  c.r.s.c.example2.AccountService2Test - second query...

可以看出我們設置的基於註釋的緩存起作用了,而在 AccountService.java 的代碼中,我們沒有看到任何的緩存邏輯代碼,只有一行註釋:@Cacheable(value=”accountCache”),就實現了基本的緩存方案,是不是很強大?
轉載自:http://blog.csdn.net/a494303877/article/details/53780597

發佈了46 篇原創文章 · 獲贊 48 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章